Need help in Shifting string

function encode(str) {
  //let sp = str.split('')
  //let sub = str.substring(3)
  let words = "";
  for (let i = 0; i < str.length; i++) {
    if (i % 3 === 0) {
      words += str[i];
    }
  }
  return words;
}
// write your code here  substring(3))
let msg = "ICE CREAM";
console.log(encode(msg)); // "I ECCAERM"
console.log(encode("Today is Monday")); // "TaiMdoysoad  ny"

I want the above code to print every third element of the string. unfortunately, my code output is ‘‘I E’’ and ‘‘TaiMd’’ instead of “I ECCAERM” and “TaiMdoysoad ny”.
my code is not printing all the values. what am I missing here?

Please help…

So what are some of the values of i that are going to be true for this statement? 3, 6, 9, etc… Is str[3] really the third letter? Is str[6] really the sixth letter? Remember, this is array indexing.

Also, I would test what 0 % 3 equals. Or 0 % any number.

My hint would be to let the for loop do all the work for you.

I’ve tried using only for loop, but the output is still wrong.

You’ve got to show it to us. We speak the language of code here :slight_smile: I was able to solve this using just the for loop.

1 Like
function encode(str) {
   let words = "";
  for (let i = 0; i < str.length; i++) {
    //if (i % 3 === 0) {
      words += str[i];
    //}
  }
  return words;
}

still not getting it

So if the first letter you want is the third letter in the string, why do you need to start the for loop at 0. Are you ever going to do anything with str[0]? Is that ever going to be the third letter in the string? Why not start the for loop where it makes sense to start it, at the first letter you want?

And then why do you need to increment the for loop one number at a time? Do you want the fourth or fifth letter in the string? Why not just jump three places to the sixth letter?

1 Like

I assume this is some kind of challenge or problem given to you. What is the exact requirement of the function?

1 Like

Yes, I’m just trying to make the string easier for me to deal with. The requirement is to shift every third letter of the string including the space

What do you mean by “shift”?

1 Like

Moving/adding every third letter to the to the string

Not sure what this means.

1 Like

here is the instruction …

Write a function that encodes a given string by constructing a new string adding every third character.

Encoding process begins with the first character and every third character is added to the encoded message till we reach the end of the string. The encoding process then starts with the second character and we jump every two characters till we reach the end of the string. Then we start a similar process starting with the third character

I’m not sure these instructions make sense to me? Is this literally what the instructions say or is this you summarizing them? Are they available online somewhere?

1 Like

That is how it was given and I’m not sure its online. I actually understand the code
instruction

Excellent. Let us know if you need any more help.

1 Like

Yes sure. It feels like home here