Basic Algorithm Scripting - Truncate a String

Tell us what’s happening:
I am trying my best to understand what is going on, following in my example of code, before checking typeof I was getting all test passed expect 2, by putting typeof all test failed expect 2.

I am confused because I do not want to use typeof because it if num is not a numeric, it should go straight to else which is to print string as it is.

What am I missing? One side I am addicted to doing these all day and one side I get stuck in these small things

Your code so far

function truncateString(str, num) {
  if(typeof num == 'Number' && num > 0){
    console.log(`${str.slice(0,num)}...`)
    return `${str.slice(0,num)}...`
  }else{
    console.log(num,"><")
    return str
  }
}

truncateString("A-tisket a-tasket A green and yellow basket", 8);

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36

Challenge: Basic Algorithm Scripting - Truncate a String

Link to the challenge:

Why do you need a typeof? The instructions tell you the input must be a number.

I wanted to check if it is a number as a extra extra test because it wasn’t passing into else statement.

That means that your condition is wrong, not that you mush use a typeof

But if I console.log(num) and when it comes to print

A-tisket a-tasket A green and yellow basket…

num is 43

but in the tests it says

truncateString("A-tisket a-tasket A green and yellow basket", "A-tisket a-tasket A green and yellow basket".length) should return the string A-tisket a-tasket A green and yellow basket .

I wonder why is wrong with my condition, can u tell me without pointing out why is it wrong?

This is a number. The second argument is always a number.

This doesn’t agree with the instructions. What part of the instructions should this be handling?

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.