New course! Every coder should learn Generative AI!
Try a free lesson0
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
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>");
}
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
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.
0
No john he can type multiple numeric values but cannot type any letter
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
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;
}
}
}
0
I have programmed it to not do anything when the textbox is not filled
0
and cancel is pressed:)
0
Yes that's I wanted. Thanks John. Thumbs up!
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?
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();
0
Nice work John... great
0
Feel free to ask mo