21 Day Coding Challenge

Challenge 20 help

#1

Hi,

I tried the below two methods. Seems like both are not working, saying that my code is not correct. Please help, thanks

1st method
function decodeMessage(message){
var newMessage = message.split();
var decode = (“o”, “i”,“u”, “e”, “a”,“y”);
for (var i=0; i<newMessage.length; i++){
if (typeof(newMessage[i]) === ‘number’){
newMessage[i] = decode[newMessage[i]];
}
}
return newMessage.join();
}

2nd method
function decodeMessage(message){
message = message.replace(“0”,“o”);
message = message.replace(“1”,“i”);
message = message.replace(“2”,“u”);
message = message.replace(“3”,“e”);
message = message.replace(“4”,“a”);
message = message.replace(“5”,“y”);
return message;
}

#2

Is this supposed to be an array? If so, you’re missing something important :slight_smile:

Sorry for the delay, kept on looking at your code…

Splitting with this will not return what you feel
var newMessage = message.split();

If you do that, your loop will only run once :slight_smile:

This:
typeof(newMessage[i]

Will always evaluate to a string :wink:

That should give you a few pointers for the first one…

For the second one… You’re much closer to a solution, but replace, the way you use it at the moment, only replace the first occurrence it encounters. :sunny:

#3

i’d use the 2nd method like so:
message = message.replace(/5/g, "y"); //First param is a string global regex

1 Like
#4

Try Merging your two codes together.

I am not sure if i understand exactly what you are trying to do but here are my thoughts in pseudo code based on your code. I tried placing a sample of what the output/input resembles at the end of the lines.

decodeMessage (message) “th1s Is 4 …”
Array newMessage = message.split();// Split every words in the array at every space and stores it at an index[“this”, “1s”,…]
NewArray;//declaring end array
i=0;i<arraylength;i++{//iterating over the array and working with 1 word of the sentence at a time NewMessage[0]=“th1s”
convert the numbers to words " this"
store words in NewArray//array.push( the decoded word) “this”

}

return newArray.join(" ") //this joins all the element of the array separated by a space