21 Day Coding Challenge

Challege 20: message.join is not a function

#1

why is message.join not a function? I see it in the instructions and on the w3schools website, but it doesnt work on my code

function decodeMessage(message){
  message.split("");
  for(var i = 0; i <message.length; i++){
    if(message[i] == "1"){
      message[i] = "i";
    }else if(message[i] == "4"){
      message[i] = "a";
    }else if(message[i] == "3"){
      message[i] = "e";
    }else if(message[i] == "0"){
      message[i] = "o";
    }else if(message[i] == "2"){
      message[i] = "u";
    }
  }
  message.join("");
}
#2

Hi Asir,
Because when you do the .split, the string remains unchanged and is still a string. The method .join exists for arrays but doesn’t exist for strings.

1 Like
#3

When you use .split(), you’re not changing the variable that it is being used on. Instead, you get a new array, which you need to save in a variable–even if it’s the same variable.

Try:
message = message.split(''); // save the result of .split() to a variable of the same name

1 Like
#4

i updated my code to this but to no avail, it says my code didnt produce the correct result

function decodeMessage(message){
  message = message.split("");
  for(var i = 0; i <message.length; i++){
    if(message[i] == "1"){
      message[i] = "i";
    }else if(message[i] === "4"){
      message[i] = "a";
    }else if(message[i] === "3"){
      message[i] = "e";
    }else if(message[i] === "0"){
      message[i] = "o";
    }else if(message[i] === "2"){
      message[i] = "u";
    }else if(message[i] === "5"){
      message[i] = "y";
    }
  }
  return message.join(" ");
}
#5

nevermind, i didnt need the " " in the join i forgot the space was already there

1 Like