Truncate a string in algorithm scripting

Hey all. I am currently on the algorithm scripting truncate of a string problem. I know I have to use slice in some form or another to cut off the string. My issue is I feel a little stuck on how to proceed here. I tried to psuedocode/explain reasoning for setting up each line of code to show my thinking process.

function truncateString(str, num) {
  //created an empty array to push the sliced array into
  let arr = []
  // This is looping through all indexes of the string length
    for(let i = 0; i < str.length; i++){
      //attempting to console.log if slicing at the length of num parameter would be appropriate or not
      console.log(str.slice(num.length) + '...');
    if(str >= num){
    } 
  }
     return str;
}

console.log(truncateString("Peter Piper picked a peck of pickled peppers", 4));

Thanks FCC team.

Pseudocode probably should be language agnostic.

Try to imagine that you know nothing about programming and I gave you a written sentence and asked to truncate it. How would you go about it? Write those steps and you’ll have a pseudocode.

What you’ve written so far is much more complex that it should be (also if(str >= num) inside you for loop makes no sense, because none of the parameters change).

1 Like

Thank you @jenovs. I reworded the code and am still getting confused on what I am attempting to do.

/* We need to create a function that takes in a string for the first parameter
and a string length(number that dictates such). To find out if the first parameter is longer
than the second we have to first loop through the entire string with a for loop. Then
after we loop through the entire string, we have to cut the string off with a '...' ending
and then return the cut off string.
*/ 



function truncateString(str, num) {
    for(let i = 0; i < str; i++){
    if(str > num.length){
      str.slice(num) + '...'; 
    } 
  }
  return str;
}

console.log(truncateString("Peter Piper picked a peck of pickled peppers", 4));

The way I understand it. I am trying to loop through the entire string. Write a condition statement that if the string is greater than the second parameter (num). However my code is not reaching to the str.slice(num) after the conditional.

I think you’ve misunderstood the assignment.
// Pseudocode
Given a string str and a number num,
if string length is less or equal than the number, return string as is,
else take first num characters of str, add ... and return this new string.

You could do it with a for loop, but it’s not necessary because I see that you already know about .slice() (you just need to check docs how to use it correctly: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice)

1 Like

Can you describe what this does?

1 Like
function truncateString(str, num) {

  if(str.length > num){

    return str.slice(0,num) + '...';

  } else {

    str

  }

    return str

}

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

I finally got it! Not sure if the code is perfect, but it passes through the console log examples! Feels so good to finally be able to complete one of these without looking at the hints.

Hey there,

awesome, glad you did it!

1 Like