Basic Algorithm Scripting - Truncate a String

Tell us what’s happening:
Please help me with this one!!
I was not able to add " . . . " at the end of the str1
Wouldn’t it be possible in the above manner without using slice??```

Your code so far


function truncateString(str, num) {
let str1= “”;
for (let i=0; i <= num; i++)
str1 += str[i];
return str1;
}

console.log(truncateString(“A-tisket a-tasket A green and yellow basket”, 8));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36

Challenge: Basic Algorithm Scripting - Truncate a String

Link to the challenge:

Why not use + ? That should add it just fine

can you please tell me particularly where where I should place it ??

Because I’ve tried using it every where!
it is adding extra space before the “. . .”

Where have you tried? It needs to go at the end of the string.

function truncateString(str,  num) {
let str1= “”;
for (let i=0; i <= num; i++) {
str1 = str1+str[i] {“here also”};
}
return str1+“…”;
}

i tried it here

I’m not able to pass the test,Please help me out

I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

What is this line of code supposed to be doing?

it will add the letters (taken from str) to the variable str1

—>>>
here also means
I’ve tried adding " . . . " at the end of that line!!

If you run this code without the {here also}

It does add the … at the end

function truncateString(str, num) {
  let str1 = "";
  for (let i = 0; i <= num; i++) {
    str1 = str1 + str[i] ;
  }
  
  return str1 + "...";
}
console.log(truncateString("A-tisket a-tasket A green and yellow basket", 8))

If you look at the console you should see a space between the words and the …

But also, the directions say to truncate the string only if it is longer than the given maximum string length.

So you will need to consider that

1 Like

Yes, Exactly!!

Ok! I will try

Adding {"here also"} to that line of code just breaks your code because it is bad syntax. If you want to comment a line of code you use // comment.

There are three things which you need to consider:

  1. Zero-based indexing. Your for loop needs to stop at the correct string index.
  2. You only concatenate “…” onto a truncated string. If number supplied to the function matches the length of the original string, you would be returning the whole string.
  3. You need to also consider what happens when you supply a number to the function which is greater than the length of the input string.
1 Like

I’ve tried it!!

Here is the code

function truncateString(str, num) {
  let str1= "";
  for (let i = 0; i <= num; i++) {
  if (str.length > num) {
  str1 += str[i];
  } else {
    return str;
  }
  }
  return str1;
}

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

But, I was unable to pass the other tests
where should I add this // " . . . "

You’re getting there.

You still need to modify the for loop to account for zero-based indexing, specifically this bit: i <= num.

You’re now not concatenating “…” onto a truncated string, so you need to do this (in your return statement perhaps)?

The reason I mentioned // comment is that you had {“here also”} tagged onto the end of a line originally, and I guessed that perhaps you were trying to comment your code? You should always feel free to add comments to your code if it helps you (or others) understand your code better, but certainly not a requirement!

1 Like

Yes, I figured it out!!
Due to extra index in the for loop, I’ve got error…

Thanks a lot!! :smiling_face_with_tear:

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