Truncate string - understanding why this works

So this must be a very simple explanation but I would like to understand why my second “if” statement returns “true” even though I have a string with the length of 1 and a num with a value of 1. I tried marking up my code using ` but for some reason it’s not working :confused:

Anyways, the question is why does my second if statement return true and executes the command when 1 clearly isn’t > than 1.

Thanks

function truncateString(str, num) {
  if (str.length > num && num >= 3){
    return str.slice(0, num - 3) + "...";
  } 
  
  if (str.length > num && num <= 3){
    return str.slice(0, num) + "...";
  } 
  else {
    return str.slice(0);
  } 
}

truncateString("A", 1);

I cleaned up your code.
You need to use triple backticks to post code to the forum.
See this post for details.

The condition in the second if-block is false. What gets executed is the last return line.

In your example of

truncate("A",1);

The second if statement does not execute. The else statement executes instead. Do you have an example of when the 2nd if statement returns true when you do not expect it?

@kevcomedia I think our replies were 1ms apart.

Whoops! I can’t believe I didn’t catch that. Haha, thanks for the help.

Here is my code.


function truncateString(str, num) {

if(str.length >  num ){
return ( str.substr (0,num)  + '...');
  }
return ( str.substr (0,num));
}


4 Likes

This is how i did it :slight_smile:

function truncateString(str, num) {
  if (str.length > num) {
     return str.slice(0, num) + "...";  
  } else {
     return str.slice(0, num);
  }
}

truncateString("A-tisket a-tasket A green and yellow basket", 8);
2 Likes
function truncateString(str, num) {
  var newStr = str.substring(0, num);
  var res = (num >= str.length ? newStr : newStr + "...");
  return res;
}

1 Like

The fast way. Loving ternary operator!

function truncateString(str, num) {
  // Clear out that junk in your trunk
  return (str.length > num ? (str.substring(0, num) + '...'):str);
}
3 Likes

Pretty sure I’m doing it wrong but it passes all the tests :sweat_smile:

function truncateString(str, num) {
  // Clear out that junk in your trunk
  if (str.length > num) {
    return str.slice(0, num) + '...'
  }
  return str;
}

This is absolutely right isn’t it? I did it like this aswell, I pass all the tests but I didn’t do the whoel num - 3 if ternary thing.

This thread comes from a time that the challenge was slightly different

1 Like

Look like the answer on Help page is wrong right now,

May I ask one question ? Why so many guys post about it long time ago but no one admin/mod/contributor change the answer page ?

New FCC version is coming and all the old section will be deleted or not ?

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

If you want to compare your solution to others, use the Get a hint button on the challenge and there are alternative solutions you can compare yours to. Also, you can probably search older posts using the forum search feature or google the challenge name and find more there.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

Thank you for understanding.

1 Like

With next update of the website - which has been delayed a bit - this will be fixed.

1 Like