+ 5

[SOLVED] I need help with JS code

I trying to replace few letter and print changed sentuence. var input = "java script is awesome"; but finished output need to be this: var output = "j4v4 5cr1pt 15 4w350m3" ; I try to change letter using indexes but it dont work. It look like it is not posible to do in that way. Here is code. https://code.sololearn.com/WX4qDDOcrChd/?ref=app

23rd Jun 2019, 2:35 PM
PanicS
PanicS - avatar
8 Answers
+ 18
https://code.sololearn.com/W22Tz6qaZw8p/?ref=app var input = "javascript is awesome"; var output = ""; for (var x = 0; x < input.length; x++){ if (input[x] === "a") { output += "4"; } else if (input[x] === "e") { output += "3"; } else if (input[x] === "i") { output += "1"; } else if (input[x] === "o") { output += "0"; } else if (input[x] === "s") { output += "5"; } else { output += input[x]; } } console.log(input); console.log(""); console.log(output);
23rd Jun 2019, 3:13 PM
r8w9
r8w9 - avatar
+ 11
Sanja Panic always declare variables :)
23rd Jun 2019, 5:32 PM
r8w9
r8w9 - avatar
+ 6
var input = "javascript is awesome"; var arrrpl = { 'a':4, 'e':3, 'i':1, 'o':0, 's':5 }; var output = ""; for (let x = 0; x < input.length; x++){ if(arrrpl[input[x]]!==undefined) output += arrrpl[input[x]]; else output += input[x]; }; console.log(input+"\n"+output); https://code.sololearn.com/Wdq9ix70FqAJ/?ref=app
23rd Jun 2019, 4:08 PM
Michail Getmanskiy
Michail Getmanskiy - avatar
+ 6
So i need to add not asign. Thanks r8w9 and Michail Getmanskiy.
23rd Jun 2019, 5:06 PM
PanicS
PanicS - avatar
+ 6
r8w9 Michail Getmanskiy Do i need to place var or let before variable inside for loop? It work without it but what is correct?
23rd Jun 2019, 5:26 PM
PanicS
PanicS - avatar
+ 6
Ok, thank you both 😊
23rd Jun 2019, 7:06 PM
PanicS
PanicS - avatar
+ 5
IMHO: the variable x is a variable need that is only inside the for loop. To limit the scope of its existence and visibility only to the circle, I used 'Let' instead of 'Var'.
23rd Jun 2019, 5:30 PM
Michail Getmanskiy
Michail Getmanskiy - avatar
+ 1
console.log("javascript is awesome!") Why doesn't it post as text?
14th Apr 2021, 7:03 PM
Keefer
Keefer - avatar