what's wrong in this condition prompt ("") if (prompt ("1")) { document.write("<h1>HTML Tutorial</h1>"); } else if (prompt ("2")) { document.write("<h1>CSS Tutorial</h1>"); } else { document.write("<h1>JavaScript Tutorial</h1>"); } I want when type 1 in output HTML tutorial shows, type 2 css tutorial shows else javascript tutorial | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

what's wrong in this condition prompt ("") if (prompt ("1")) { document.write("<h1>HTML Tutorial</h1>"); } else if (prompt ("2")) { document.write("<h1>CSS Tutorial</h1>"); } else { document.write("<h1>JavaScript Tutorial</h1>"); } I want when type 1 in output HTML tutorial shows, type 2 css tutorial shows else javascript tutorial

24th Aug 2016, 2:27 AM
Sandeep Sharma
Sandeep Sharma - avatar
13 Answers
0
var input = prompt("Please type 1, 2 or any other number"); if (input == 1) { document.write("<h1>HTML Tutorial</ h1>"); } else if (input == 2) { document.write("<h1>CSS Tutorial</h1>"); } else { document.write("<h1>Javascript Tutorial</h1>"); }
24th Aug 2016, 4:21 AM
John Gachina
John Gachina - avatar
0
Thanks John and Dick its working one thing I want to add in this I want only numeric value to be add. when someone type text it shows alert msg only numeric values
24th Aug 2016, 9:13 AM
Sandeep Sharma
Sandeep Sharma - avatar
0
Quick question, can the user type in a number with more than one digit?If so, the code is going to be much more complex.
24th Aug 2016, 12:33 PM
John Gachina
John Gachina - avatar
0
No john he can type multiple numeric values but cannot type any letter
24th Aug 2016, 12:46 PM
Sandeep Sharma
Sandeep Sharma - avatar
0
#Dick, thanks for your reply, but this alert is everytime showing when someone use different numeric values instead of 1,2,3. I want he can use all numeric values but can't type letters and other symbol
24th Aug 2016, 12:58 PM
Sandeep Sharma
Sandeep Sharma - avatar
0
Heres the correct code Sandeep, var input = prompt("Please type 1,2 or any other number"); if(input == null){ }else if (input == 1) { document.write("<h1>HTML Tutorial"); } else if (input == 2) { document.write("<h1>CSS Tutorial</h1>"); } else{ validate: for(var k=0; k<input.length;k++){ switch (input[k]){ case "0": case "1": case "2": case "3": case "4": case "5": case "6": case "7": case "8": case "9": if(k == input.length - 1){ document.write("<h1>Javascript Tutorial</h1>"); break validate; }else{ continue ; } default : alert ("Enter only numerical values!"); break validate; } } }
24th Aug 2016, 1:40 PM
John Gachina
John Gachina - avatar
0
I have programmed it to not do anything when the textbox is not filled
24th Aug 2016, 1:41 PM
John Gachina
John Gachina - avatar
0
and cancel is pressed:)
24th Aug 2016, 1:41 PM
John Gachina
John Gachina - avatar
0
Yes that's I wanted. Thanks John. Thumbs up!
24th Aug 2016, 2:56 PM
Sandeep Sharma
Sandeep Sharma - avatar
0
John last thing I want to know How I get back to prompt input field when any one type letter than alert box is showing after press ok again come back to promp box. How is that possible?
24th Aug 2016, 3:13 PM
Sandeep Sharma
Sandeep Sharma - avatar
0
I am not sure I get your question properly but I guess this is what you mean, function getInput(){ var input = prompt("Please type 1,2 or any other number"); if(input == null){ }else if (input == 1) { document.write("<h1>HTML Tutorial"); } else if (input == 2) { document.write("<h1>CSS Tutorial</h1>"); } else{ validate: for(var k=0; k<input.length;k++){ switch (input[k]){ case "0": case "1": case "2": case "3": case "4": case "5": case "6": case "7": case "8": case "9": if(k == input.length - 1){ document.write("<h1>Javascript Tutorial</h1>"); break validate; }else{ continue ; } default : alert ("Enter only numerical values!"); getInput(); break validate; } } } } getInput();
24th Aug 2016, 5:43 PM
John Gachina
John Gachina - avatar
0
Nice work John... great
24th Aug 2016, 6:14 PM
Sandeep Sharma
Sandeep Sharma - avatar
0
Feel free to ask mo
24th Aug 2016, 6:31 PM
John Gachina
John Gachina - avatar