After adding break, my code no longer works.
alert("Warning: Game is made for the age of 18 and over.");
var a = prompt("Are you ready to enter this game?");
if (a == "yes") {alert("Entering");}
else if (a == "no") {while(true)
var a = prompt("Are you ready to enter this game?");
if(a == "yes") {break; alert("Entering");} }
You have a few syntax errors in your JavaScript. You are missing out on a curly brace. The break statement needs to be after the alert statement. See the following snippet after the fixes and try to learn your mistakes.
alert("Warning: Game is made for the age of 18 and over.");
var a = prompt("Are you ready to enter this game?");
if (a == "yes") {alert("Entering");}
else if (a == "no") {
while(true) {
//var b = prompt("Are you ready to enter this game?");
if(a == "yes") {
alert("Entering");
break;
}
}
}
break; alert("Entering");
It's to late to alert after breaking as it immediately exits the loop and doesn't execute instruction after itself there.
I believe you intend to prompt the user repeatedly until he answers yes. In that case, use this code:
alert("Warning: Game is made for the age of 18 and over.");
let response = "";
while(response !== "yes") {
response = prompt("Are you ready to enter this game?");
}
alert("Entering");
Related
I am a begginer to javascript. I was writing some code for my program in which mainly it's loop. It's about you enter your age and it says you should drive or not a simple program but , I wanted to make it repeating itself until the user didn't enters n or N. I wrote a if statement with break and another else - if statement with continue. When i press n it stops but , when I type y it dosen't continue. Plz try to help me Here is the code below:
while(true){
let age = prompt("Enter your age")
if (age>18){
console.log("You can drive")
}
else if (age<0){
console.log("Invalid age")
}
else if (age>100){
console.log("Invalid age")
}
else if (age==18){
console.log("Come to our office")
}
else{
console.log("You cannot drive")
}
let choice = prompt("Type y or Y to run again type n or N to exit")
if(choice == "n" || "N"){
continue
}
if (choice == "y" || "Y")
break
}
Your if conditions are strange, you say if(choice == "n" || "N"), which means, in words "go in the next block if the variabe choice is 'n' or if 'N'". I'm guessing saying if("N") gets interpreted as true, since it's not null.
You should write, explicitly, if(choice === "n" || choice === "N").
Also, as phuzi said in the comments, continue makes the code restart the loop with the next iteration, while break makes the code go out of the loop. It seems you have them backwards
Hi I'm new to javascript and was wondering if the community could help me in re-writing more logically the following snippet:
var userAnswer = prompt("Are we there yet?")
while ((userAnswer != "yes" && userAnswer != "yeah") && (userAnswer.indexOf("yes") === -1)) {
var userAnswer = prompt ("Are we there yet?")
}
alert ("Yuppie we made it!")
the part that I think could be improved is in the while loop when I have to repet the entire string of var userAnswer = prompt....
there must be a dry way of doing it. Any help would be much appreciated!
Since you want to run ask for a value at least once you could use do-while instead. You could define variable within the do block, it still will be hoisted.
do {
var userAnswer = prompt("Are we there yet?")
} while ((userAnswer != "yes" && userAnswer != "yeah") && (userAnswer.indexOf("yes") === -1))
alert("Yuppie we made it!")
PS. I kept the condition assuming it works for you.
Instead of prompt to enter free text use confirm that will return true or false:
while (!confirm("Are we there yet?"));
alert("Yuppie we made it!")
A way of doing it without using a while loop.
var answer = prompt ("Are we there yet?", "YES or NO")
answer != "yes" ? prompt ("Are we there yet?", "YES or NO"): alert("We made it !!");
I am currently new to JavaScript and am learning in school! There is an assignment that I am doing by creating a game but the alert keeps popping up with the wrong one. Every time it alerts as "You found a match!" for every card, however this is not supposed to happen. I have been trying to figure this out for the last hour. Thanks
var cards = ["queen", "king", "queen", "king"];
var cardsInPlay = [];
var cardOne = cards[0];
cardsInPlay.push(cardOne);
console.log("User flipped " + cardOne);
var cardTwo = cards[1];
cardsInPlay.push(cardTwo);
console.log("User flipped " + cardTwo);
if (cardsInPlay.length === 2){
cardsInPlay[0] === cardsInPlay[1];
alert("You found a match!");
} else {
alert("Sorry, try again");
}
You have a simple syntax error.
if (cardsInPlay.length === 2){
cardsInPlay[0] === cardsInPlay[1];
By putting your second conditional inside the bracket {, you've made it ineffective. Try this:
if (cardsInPly.length === 2 && cardsInPlay[0] === cardsInPlay[1]) {
The condition always goes inside the parenthesis ( ). If it's outside of it, it won't work.
Typing cardsInPlay[0] === cardsInPlay[1]; when they aren't equal is effectively like typing false;. It's technically valid, but doesn't do anything.
I think you ment to put the condition like below:
if (cardsInPlay.length === 2 && cardsInPlay[0] === cardsInPlay[1]) {
alert("You found a match!");
}
// Check if the user is ready to play!
confirm("Are you ready to play?");
var age = prompt("What's your age");
if ( age is less than 13)
{
console.log("You are allowed to play,but we take no responsibility");
}
else {
console.log("Go on! you can play");
}
I've got a syntax error while executing this JavaScript code, the first two lines (confirm and variable) are correct, this error is somewhere in the if / else satements.
Use < operator instead of is less than
// Check if the user is ready to play!
confirm("Are you ready to play?");
var age = prompt("What's your age");
if (age < 13) {
alert("You are allowed to play, but we take no responsibility");
} else {
alert("Go on! you can play");
}
You can reduce line of code also by using ternary operator
age < 13 ? console.log("You are allowed to play, but we take no responsibility"):console.log("Go on! you can play");
For the life of me, I can't figure out what's wrong with it. When I run this script, the first 'STAY' case works just fine. Running any of the other cases gives me an error (undefined is not a function).
Ran it thru my JS checker and it looked ok on there. Googled it and everyone else I'm seeing with the same problem has been told their breaks are either used incorrectly or not there. All of the syntax looks right to me from what I've learned. Compared it against the sample 'game' and it looks very similar to how they did it. What am I doing wrong?! Thank you for any help
var user = prompt("You see God. Do you want to STAY, PUNCH HIM, or CRY?").toUpperCase();
switch(user) {
case 'STAY':
var curious = prompt("Are you a curious person?").toUpperCase();
var insight = prompt("Are you an insightful person?").toUpperCase();
if (curious === "YES" && insight === "YES") {
console.log("Maybe it was a good idea to stay and speak to him");
} else if (curious === "YES" || insight === "YES") {
console.log("Well, maybe you can scrunge up something to say");
} else {
console.log("Why would you stay if you have nothing intelligent to say?");
}
break;
case 'PUNCH HIM':
var strong = prompt("Are you ridiculously stronger than God?").toUpperCase();
var fast = prompt("Are you faster than a minute man?").toUpperCase();
if (strong === "YES" && fast === "YES") {
console.prompt("You still dead, but not as dead as you would've been");
} else if (strong === "YES" || fast === "YES") {
console.prompt("One ain't good enough, homie");
} else {
console.prompt("Slow and weak? Bad choice, dag");
}
break;
case 'CRY':
var convincing = prompt("Are you superbly convincing with crying?").toUpperCase();
var female = prompt("Are you a female?").toUpperCase();
if (convincing === "YES" && female === "YES") {
console.prompt("You'll prolly be ok, boo");
} else if (convincing === "YES" || female === "YES") {
console.prompt("Hope you're a female");
} else {
console.prompt("You dead!");
}
break;
default:
console.prompt("Answer the question with the supplied answers");
}
Use console.log instead of console.prompt.