How can I execute continue after break in javascript - javascript

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

Related

While loop not displaying correct result Javascript

I'm working on a project for one of my classes where I need to use the users input to redirect them to a website. It requires that the users choice be validated using a loop, so I've chosen to use a while loop to check for if the users input differs from what it should be and if it is, the user is prompted to re-enter their answer. Here's the code:
var websitechoice;
websitechoice = parseInt(prompt("Which website would you like to go to? \n 1. google \n 2. gmail \n 3. youtube"))
while (websitechoice != 1 || 2 || 3) {
alert("you input an incorrect value")
websitechoice = parseInt(prompt("Which website would you like to go to? \n 1. google \n 2. gmail \n 3. youtube"))
}
if (websitechoice = 1) {
alert("you chose 1")
}
else if (websitechoice = 2) {
alert("you chose 2")
}
else {
alert("you chose 3")
}
So far it was just a quick mock up I made to check if it would work, but every time I try and run it, I always get back "you input an incorrect value" even when inputting 1, 2, or 3, and so far nothing I've tried had differed the results. if anyone could help I'd really appreciate it, thanks
You're using a definition in the if statement, in that you're telling the code that websitechoice is now equal to that number. You should use a comparator like so
if (websitechoice == 1) {
alert("you chose 1")
}
else if (websitechoice == 2) {
alert("you chose 2")
}
else {
alert("you chose 3")
}
For your while statement, you should change it to and statement, because you only want to run the code when it's not equal to one and not eqaul to two and not equal to three
while (websitechoice != 1 && websitechoice != 2 && websitechoice != 3) {
alert("you input an incorrect value")
websitechoice = parseInt(prompt("Which website would you like to go to? \n 1. google \n 2. gmail \n 3. youtube"))
}
You could also just say, if websitechoice > 3 then do the alert process
There are many flaws in your code. First of all, you should use AND operator instead of OR operator. Also, you need to break the comparison as beloe demonstrated.
Secondly, you have used assignment operator ( = ) in the if else statement. You need to use comparison operator ( == or === ) to compare otherwise it will return 1 everytime.
var websitechoice;
websitechoice = parseInt(prompt("Which website would you like to go to? \n 1. google \n 2. gmail \n 3. youtube"))
while (websitechoice !== 1 && websitechoice !== 2 && websitechoice !== 3) {
alert("you input an incorrect value")
websitechoice = parseInt(prompt("Which website would you like to go to? \n 1. google \n 2. gmail \n 3. youtube"))
}
if (websitechoice === 1) {
alert("you chose 1")
}
else if (websitechoice === 2) {
alert("you chose 2")
}
else {
alert("you chose 3")
}
Also, it will be easy to debug if you put your code in the snippet. Seems like you are quite new to developer community. Welcome to coder community !
The != has precedence over the || operator 1 so:
websitechoice != 1 || 2 || 3
will always evaluate to:
(websitechoice != 1) || 2 || 3
which is not what you want...
another issue is that:
websitechoice = 1
is an assignment not a comparison.
To fix your code without changing its structure:
var websitechoice;
websitechoice = parseInt(prompt("Which website would you like to go to? \n 1. google \n 2. gmail \n 3. youtube"))
while (websitechoice !== 1 && websitechoice !== 2 && websitechoice !== 3) {
alert("you input an incorrect value")
websitechoice = parseInt(prompt("Which website would you like to go to? \n 1. google \n 2. gmail \n 3. youtube"))
}
if (websitechoice === 1) {
alert("you chose 1")
}
else if (websitechoice === 2) {
alert("you chose 2")
}
else {
alert("you chose 3")
}
A cleaner way to check if your input is one of a list of values would probably be:
[1,2,3].includes(websitechoice)
which is cleaner and can scale easily to more values.
I've also replaced the == operators with ===, don't use == in JS unless you know what you are doing. 2

Calling of functions not working properly

I am working on a computer science project, I need to make a Wheel of Fortune game - basically, get a random word, show blank spaces, and have the player(s) guess the letters to form the word. Right now, I've got the parts where the word is chosen, hidden (switching the letters for blank spaces), and shown. Now, I am on the part of enable player turns to start guessing the letters. I have set up a button to call a function which calls other functions (each turn is broken down into multiple parts, such as asking for a guess, checking what type of guess it is, and checking if that guess is in the word). For some reason, this bigger function is, when calling them, only calling the first function, and spamming that single function. Why is it not going down the list of functions and calling each in order?
Here is the code that relates to that part:
function player1() {
console.log("Player 1 is going.");
nowGoing = 1;
player1Elements.style.display = "block";
alert(player1Name + " it is your turn. What do you guess?");
prompt();
checkGuessType();
checkGuess();
}
function prompt() {
console.log("Prompting");
var playerGuess;
playerGuess = prompt("What letter would you like to guess?");
checkGuessType(playerGuess);
}
function checkGuessType(playerGuess) {
console.log("Checking");
if (playerGuess.length > 1) {
console.log(player1Name + " guessed a word.");
} else if (playerGuess == "a" || playerGuess == "e" || playerGuess == "i" || playerGuess == "o" || playerGuess == "u") {
console.log(player1Name + " guessed a vowel.");
} else {
console.log(player1Name + " guessed a consonant.");
}
}
All that appears in the console (the console.logs are exactly for this purpose) are 6000 lines saying "Prompting", not "Prompting" and "Checking". If there is any more code I should show, please let me know!
If anyone could help me out, that would be great!
Edit: I tried changing the code to the following (basically changing to a global variable), but it still didn't work. I'm not sure why, as the same thing happens.
function player1() {
console.log("Player 1 is going.");
nowGoing = 1;
player1Elements.style.display = "block";
alert(playerName1 + " it is your turn. What do you guess?");
prompt();
checkGuessType();
checkGuess();
}
function prompt() {
// console.log("Prompting");
playerGuess = prompt("What letter would you like to guess?");
}
function checkGuessType() {
console.log("Checking");
if (playerGuess.length > 1) {
console.log(playerName1 + " guessed a word.");
} else if (playerGuess == "a" || playerGuess == "e" || playerGuess == "i" || playerGuess == "o" || playerGuess == "u") {
console.log(playerName1 + " guessed a vowel.");
} else {
console.log(playerName1 + " guessed a consonant.");
}
}
The reason that "Prompting" is appearing over and over is that you provide playerGuess as the argument for checkGuessType in prompt but playerGuess is just another call to prompt so it starts recursing. However, prompt never returns any value under any condition, so the recursion will never resolve to a value causing the stack of function calls to collapse and execute the call to checkGuessType, so you never see a repetition of the console.log in checkGuessType.
EDIT
After your edit I just wanted to point at what I think the problem is directly:
function prompt() {
// console.log("Prompting");
playerGuess = prompt("What letter would you like to guess?");
}
Within the scope of your function, prompt just refers to the function itself. This is causing a recursion. That recursive stack will never end because nothing returns from prompt, thus eventually causing a stack overflow error. If you don't mean to be starting a recursive stack, you need to rename your prompt function to something other than what the built in prompt function is called.

Whats wrote with this break;

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");

Alert message is wrong

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!");
}

Syntax error resulting from invalid syntax in if / else statements

// 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");

Categories

Resources