Syntax error resulting from invalid syntax in if / else statements - javascript

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

Related

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

Need some advice on a Javascript number guessing game

this is my first post ever so go easy on me haha! I'm working on a number game that prompts a user to guess a random number between 1 and 100. The part I'm having trouble with is telling a user if they previously already guessed a number. I've been playing around with it myself and for example if I submit 10, and then 10 again it will say that I repeated a guess (which is what it should do). If I say 10 for a third time it doesn't tell me that I've repeated a guess and rather that the number is too low (it should just say that I've repeated the guess again). Here's the jsfiddle for clarification: https://jsfiddle.net/k1d8awf6/2/.
var random = Math.ceil(Math.random()*100);
var guessList = new Array();
var guess = prompt("Hello user, I'm thinking of a number between 1-100. What is it?");
guessList.push(guess);
while (guess != random) {
for (i = 0; i < guessList.length-1; i++) {
if (guess == guessList[i]) {
guess = prompt("You already guessed this number. Try again.");
}
}
if (guess > random) {
guess = prompt("Your guess is too high! Try again.");
guessList.push(guess);
}
if (guess < random) {
guess = prompt("Your guess is too low! Try again.");
guessList.push(guess);
}
if (guess == random) {
alert("Nice job! You guessed the correct number! It took you " + guessList.length + " tries!");
}
}
You're doing if's, when you should be doing else if's. In addition, you can use the indexOf function instead of looping through elements. eg:
var random = Math.ceil(Math.random()*100);
var guessList = new Array();
var guess = prompt("Hello user, I'm thinking of a number between 1-100. What is it?");
while (guess != random) {
if (!guess) {
// The user cancelled or entered 0, just cancel the game
return;
} else if (guessList.indexOf(guess) >= 0) {
// User already guessed this number
guess = prompt("You already guessed this number. Try again.");
} else {
// User hasn't guessed this number, store their guess then display whether they're too high or too low
guessList.push(guess);
if (guess > random) {
guess = prompt("Your guess is too high! Try again.");
} else if (guess < random) {
guess = prompt("Your guess is too low! Try again.");
}
}
}
// The guess is correct when we exit the loop
alert("Nice job! You guessed the correct number! It took you " + (guessList.length + 1) + " tries!");
More info on indexOf: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf
You may also want to end the game if the user enters '0' or Cancels the dialog :)
Edit
Edited to fix some logic errors, updated fiddle here: https://jsfiddle.net/k1d8awf6/8/

Syntax Error: Unexpected token Else

I am learning Javascript via Codecademy and no have been stumped on this little piece here.
I have supposed to write an if else statement.
It shows me here in the following that there is a Syntac Error with a missing identifier:
var userAnswer = prompt("Are you feeling lucky, punk?");
if (userAnswer === "yes");
{
console.log("Batman hits you very hard. It's Batman and you're you! Of course Batman wins!");
}
else {
console.log("You did not say yes to feeling lucky. Good choice! You are a winner in the game of not getting beaten up by Batman.");
}
What is wrong with that.... There is no error in this example here:
if (age < 18)
{
console.log("We take no actions or responsibility. Play at your own risk!");
}
else
{
console.log("Enjoy the game");
}
if (userAnswer === "yes");
Remove the semicolon.
There's a semi-colon after the first conditional check. Also, you should always put the opening bracket of the conditional branch on the same line as the brackets
var age;
age = prompt('How old are you?');
if (age < 18)
{
alert("We take no actions or responsibility. Play at your own risk!");
}
else if(age > 18)
{
alert("Enjoy the game");
}
remove the semicolon after
if (userAnswer === "yes");
if you put the semicolon there, you are telling the script to stop there and not to render the next conditional statement that is "else"[SyntaxError: Unexpected token else]

How do I "restart" if statement if a condition is met?

I'm very new to javascript. I wanted to make a quick program that generates the youngest age possible a person is able to date given their age, using the formula my dad taught me. In my code I have a condition where if my var (dateage) isn't a number, the user is asked to please enter in a number. I want the program to then re-ask the variable assignment prompt until a number is given.
var dateage = prompt("How old are you?");
if(dateage >= 14){
dateage = dateage/2 + 7;
alert("The youngest you can date is " + dateage)
} else if(isNaN(dateage)){
alert("Please enter in a number");
} else
alert("You're too young to date.");
You can see that if dateage isn't a number, the user is alerted. At that point I want the prompt to appear again asking the user for their age. How can I do this?
Put it in a function so you can re-invoke
function checkAge() {
var dateage = prompt("How old are you?");
if(dateage >= 14){
dateage = dateage/2 + 7;
alert("The youngest you can date is " + dateage)
} else if(isNaN(dateage)){
if (confirm("Please enter in a number")) checkAge();
} else
alert("You're too young to date.");
}
checkAge();
I used a confirm for the re-check because this means you can more easily escape from an infinite loop situation. If you don't want to pollute the namespace, you can write this as a named IIFE, and if you don't want to carry the stack over, you can invoke through a setTimeout.
You can put this in a function and simply keep recalling the function:
function askAge(){
var dateage = prompt("How old are you?");
if(dateage >= 14){
dateage = dateage/2 + 7;
alert("The youngest you can date is " + dateage)
} else if(isNaN(dateage)){
alert("Please enter in a number");
askAge();
} else
alert("You're too young to date.");
}
askAge();
wrap the question and output in a while loop, breaking only when number has been entered
Personally, I like to have "restartable" functions look like:
(function() {
var restart = arguments.callee; // "magic" property refers to current function
if( somecondition) setTimeout(restart,1);
else {
// do actual stuff
}
})();
The setTimeout released the current call stack, otherwise you could potentially get a stack overflow error if you manage to infinitely loop restart.
function checkAge() {
var dateage = prompt("How old are you?");
if(dateage >= 14){
dateage = dateage/2 + 7;
alert("The youngest you can date is " + dateage)
} else if(isNaN(dateage)){
if (confirm("Please enter in a number")) checkAge();
} else
alert("You're too young to date.");
}
checkAge();

How can I set an empty alert

I have made a little game in Script. That's sort of arithmetic game. I asked 2+2-3=? and user have to give the answer. If the answer is right than alert will show congratulations and if answer is wrong then you are higher or lower than actual answer. But now I want to add another alert if someone not answering this question and leave the prompt empty and press enter than another alert will be display like You are looser. My question is; how can I set the value empty and alert. Here is what I tried and it didn't work. Please see the code below:
<script type="text/javascript">
var number = prompt("count these numbers: 2+5-10+8=?");
if (number == 5) {
alert("Congratulations Your Answer is Correct");
} else if (number > 5) {
alert("Your answer is a little higher.");
} else if (number < 5) {
alert("Your answer is little lower than actual answer.");
} else if (number == "null") {
alert("You are looser!");
}
</script>
I would parse the number so it's actually a Number type, instead of relying on the == to coerce the value for you. Then, you could check for NaN with the isNaN function, or better yet, leave off the if completely and just make it an else:
var number = parseInt(prompt("count these numbers: 2+5-10+8=?"), 10);
if (number === 5) {
alert("Congratulations! Your answer is correct.");
} else if (number > 5) {
alert("Your answer is a little higher.");
} else if (number < 5) {
alert("Your answer is little lower than actual answer.");
} else {
alert("You are a loser!");
}​
http://jsfiddle.net/KebNN/
Just compare it to an empty string:
if (number == '') {
// ...
Also, if you want to alert the same message on "Cancel" clicks, you can simply check whether number evaluates to false:
if (!number) {
// ...
Use (answer == null || answer == undefined || answer == '')
try:
} else if (number == "" || isNaN(number)) {
alert("You are looser!");
}
I'm not sure I understand the question, but is this what you're looking for?
alert("");
If the user presses cancel on a prompt, the value will be null. By comparing it with ===, we make sure the answer isn't 0 (0 == null is true).
<script type="text/javascript">
var number = prompt("count these numbers: 2+5-10+8=?");
if (number === null) {
alert("You are loser!");
} else if (number == 5) {
alert("Congratulations Your Answer is Correct");
} else if (number > 5) {
alert("Your answer is a little higher.");
} else if (number < 5) {
alert("Your answer is little lower than actual answer.");
}
</script>​
You have accounted for all of the possible results except for null you could just add a else at the end of the first if statement as a catch all.. I.E. if the user leaves it blank or enters an invalid character.
if( number == 5 ) {
alert( "Congratulations Your Answer is Correct" );
} else if ( number > 5 ) {
alert( "Your answer is a little higher." );
} else if ( number < 5 ) {
alert( "Your answer is little lower than actual answer." );
} else {
alert("You are looser!");
}
.....

Categories

Resources