Reputation: 481
How do I loop back to the prompt question if the user enters the wrong answer and I want the question to repeat until they get the correct answer?
<html>
<head>
<script>
</script>
<title> Javascript program</title>
</head>
<body>
<script>
var company = (prompt("What the name of the company that developed the javascript language?", ""));
if (company == 'netscape') {
alert("correct answer!");
} else {
alert("wrong answer");
}
</script>
</body>
</html>
Upvotes: 3
Views: 14023
Reputation: 1
I hope this short function with a couple of conditions helps anyone who needs to check if a certain piece of data is a number and if it is within a given range. I used it for dates, but you can pretty much use it for any kind of date-related data.
const checkData = function (data, value1, value2) {
let x = (+prompt(`Choose the first date's ${data}. You can choose from ${value1} to ${value2}.`));
if ((x < value1 || x > value2) || Number.isFinite(x) === false) {
while ((x < value1 || x > value2) || Number.isFinite(x) === false) {
x = (+prompt(`Criteria have not been met. You must type a number between ${value1} and ${value2}`))
}
}
return x;};
checkData('month', 1, 12);
Upvotes: 0
Reputation: 515
you need to include the function in itself until it meets the requirement and return(exit the function);
ask();
function ask(){
var answer=prompt("Question..?");
if(answer=="netscape"){
alert("correct");
return;
}
alert("wrong answer");
ask();
}
Upvotes: 0
Reputation: 1847
showing prompt continuously can be irritating and user always have option in Chrome to prevent a page from creating more dialogues. If you use a loop with while(true)
and user opt to prevent dialogue out of irritation then your page can end up in a hang state. I would suggest to use a counter to ask questions for a specific number of time before assuming that answer is wrong as in below code
function askQuestion() {
return prompt("What the name of the company that developed the javascript language?", "");
}
var counter = 0;
var correctAnswer = false;
while(!correctAnswer && counter < 10) {
counter++;
correctAnswer = askQuestion() === 'netscape';
if (!correctAnswer) {
alert("wrong");
}
}
if (correctAnswer) {
alert("correct");
} else {
alert("sorry after 10 attempts answer is still wrong");
}
You can also check this plnkr link https://plnkr.co/edit/IbUFRC0HepfSHlvNqMzM?p=preview
Upvotes: 0
Reputation: 386736
You could use a while
loop and loop forever until your condition is true
and then use a break
statement to exit the loop.
Basically, you could select loop which checks a condition first
while (condition) {
// code
}
loop which checks a condition later
do {
// code
} while (condition);
a solution inbetween
while (true) {
// code
if (condition) break;
// code
}
or a recursive solution, like
function fn() {
// code
if (condition) {
// code
return
}
fn(); // call fn again
}
But I suggest to use an iterative approach until a value met a condition.
var company;
while (true) {
company = prompt("What the name of the company that developed the javascript language?", "");
if (company === 'netscape') {
break;
}
alert("wrong answer");
}
alert("correct answer!");
Upvotes: 2
Reputation: 6561
You could use a loop like so:
let question = "What is the name of the company that developed the javascript language?",
defaultAnswer = "stackoverflow",
correctAnswer = "netscape";
while(prompt(question, defaultAnswer) !== correctAnswer) alert("Wrong answer")
alert("Correct answer");
Or you could use recursion, refer to Rajesh's answer.
Upvotes: 0
Reputation: 24945
You can wrap your code in a function and call self on incorrect value.
var max_count = 5;
function showConfirm() {
var company = (prompt("What the name of the company that developed the javascript language?", ""));
if (company == 'netscape') {
alert("correct answer!");
} else {
alert("wrong answer");
// to limit user for limited count
if (--max_count > 0)
showConfirm()
}
}
showConfirm();
Upvotes: 4