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?
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.
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?
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?