0
helpz!
i want to replace a letter with integer, i mean 'a' = '1'; and 'b' = '2'; which i know how to do it using switch statement.. the problem is when i enter letter 'a' and 'b' the output is [nothing] instead that i should be expected that is 12.
12 Answers
+ 11
I got dis, boiiiiii. Works for all cases.
// Hatsy Rei doing JS, god has forsaken Earth.
var words = prompt("").split('');
var str_len = words.length;
for (var i = 0; i < str_len; i++)
{
switch(words[i])
{
case 'a':
words[i] = '1';
break;
case 'b':
words[i] = '2';
break;
}
}
words = words.join('');
document.write(words);
+ 9
Not proficient in web codes, but other members may be able to help if you posted a sample of your code here.
+ 2
var words = prompt("");
switch(words){
case 'a':
document.write('1');
break;
case 'b':
document.write('2');
break;
}
+ 2
var words = prompt("");
var i=0;
for(i=0;i<=words.length;i++)
{
switch(words[i]){
case 'a':
document.write('1');
break;
case 'b':
document.write('2');
break;
}
}
Try this @foundation
+ 2
@hatsy rei, not proficient in web eh? HAHAHA. Considering you're in level 16. (Y)
+ 2
Don't use an array for this kind of problem. If you use you have to write 26 cases for a to z. this is repeating the same code over and over.
as a web developer your job is to reduce the amount of code but still the code should work as expected and readable.
I didn't fully understand your question yet, but however I will suggest you something.
Use an object containing letter a to z with their respective values.
var letters = {
a: 1,
b: 2,
c: 3,
....
};
now you can simply use document.write(letters[word])
hope this make sense. if you have any question ask me
+ 1
Do you have codes so I can help?
+ 1
thnx Sabrina Aviles.. very helpful! <3 .. however.. why "switch(words[i])"? can someone explain to me?
+ 1
words[i] is an array. Which means
words[0] will be the first which is equivalent to a (if ab is your input)
Since it is in the for loop, it will execute again and
words[1] will be the second letter which is b
i is based on the for loop, i=0 (first number until words.length); i<=words.length(this is how many letters does the word have); i++(i will add 1 everytime it loops)
You ccan upvote my answer, thank you! Haha. :)
0
sure.. wait
0
ooohh.. ok, thnx @Sabrina .. after read it a couple times now i understand 😂.. thnx again xD
0
wow @Apel .. thnx for the additional information.. really helpful :) .. btw I learn javascript because its easy and fun to code, not to be web developer..



