+ 1

why i am not getting the output?

if i put the input box value as hello, it has to print 'svool' it will check the index of x in alpha and writtens - ve value, for ex : if i put 'a' it has to output 'z'. but it is not working. can anyone help me? https://code.sololearn.com/WM1oorXC0uOD/?ref=app

6th Sep 2020, 4:21 AM
Mr. 12
Mr. 12 - avatar
2 odpowiedzi
+ 2
There are a few things you probably don't expect. - d is always an empty string because you get the value from the input before the user has a chance to type anything. You probably want to get the value upon click or a call to myFun. - splice doesn't use the parameters the way you wrote them. You're passing negative start indexes which is weird. message += alpha.slice(-index-1,-index); - much more minor but you should use more descriptive variable names and indent better. It'll be easier for you and others to read your code if you do. Here is something closer to what you likely want: <!DOCTYPE html> <html> <head> <title>Page Title</title> </head> <body> <div id="container"> <div id="h"><b>Secret Message Generator</b></div> <div id="text">Enter Your Message:</div> <input id="demo" type="text" value=""> <button type="button" id="btn">Generate</button> <p id="o"></p> </div> <script> var alpha = " abcdefghijklmnopqrstuvwxyz "; var z = document.getElementById('btn'); function myFun() { var d = document.getElementById('demo').value; var message = ""; for (x of d.split("")){ if (x==" "){ message += x; } else{ var index = alpha.indexOf(x); message += alpha.charAt(index); } } document.getElementById('o').innerHTML=message; } z.addEventListener('click',myFun); </script> </body> </html>
6th Sep 2020, 4:33 PM
Josh Greig
Josh Greig - avatar
+ 1
Josh Greig thank you bro, i got it
6th Sep 2020, 5:31 PM
Mr. 12
Mr. 12 - avatar