Cant seem to get this code right please help :)) [closed] - javascript

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 days ago.
Improve this question
confirm("Ready to play???!!!");
var age = prompt("How old are you?");
console.log(age);
if (age >= 21) {
console.log("you can go in and have drinks :)");
}
if (age >= 19) {
console.log("go in but you cant drink");
} else if (age < 19) {
console.log("sorry cant let you in!");
}
When I typed in 23, it will both log the first one (age >= 21) and the second one (age >= 19).
I want it only show the first one, how could I do that?

Related

SyntaxError: expected expression, got '||' [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I've been trying to figure out what is wrong with my code. In the console I keep on getting "expected expression, got '||'" but I've got no idea why. Can anyone enlighten me?
Thanks
if (isNaN(value)) || value < 0 || value > 9 {
result.innerHTML = `<p class="result">${text[0]}</p>`;
}
You have misplaced parentheses on your if statement.
Replace this:
if (isNaN(value)) || value < 0 || value > 9
With this:
if (isNaN(value) || value < 0 || value > 9)

I'm completely baffled by this, because the else if statement seems to break the program? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
So I'm writing a piece of coed for my brother and I think it is really odd because the first console log outputs undefined, which is expected, and the second one outputs '1229/4096', which is what I would expect too, but the third one outputs undefined which I think is pretty odd. If anyone can tell me why or help me fix it, or even ask for any other bits of code, I would love some help!
let catchRate = document.getElementById('catchRate').value;
let x;
console.log(x)
if (catchRate >= 0 && catchRate <= 30) {
let x = (1229/4096);
console.log(x);
} else if (catchRate >= 31 && catchRate <= 150) {
let x = (2048/4096);
} else if (catchRate < 150) {
let x = (2867/4096);
}
console.log(x)
You're declaring a new variable x in your if/else block by using let. So therefore you're NOT reassigning the value of your original x, declared on line 2. If you want to re-assign the value of that SAME x variable, don't use let x = 123 in your if/else block. Just use x = 123.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 min ago by Pointy, A.J. Uppal, Mark Baijens.
(Viewable by the post author and users with the close/reopen votes privilege)
Edit question
So I'm writing a piece of coed for my brother and I think it is really odd because the first console log outputs undefined, which is expected, and the second one outputs '1229/4096', which is what I would expect too, but the third one outputs undefined which I think is pretty odd. If anyone can tell me why or help me fix it, or even ask for any other bits of code, I would love some help!
let catchRate = document.getElementById('catchRate').value;
let x;
console.log(x)
if (catchRate >= 0 && catchRate <= 30) {
x = (1229/4096);
console.log(x);
} else if (catchRate >= 31 && catchRate <= 150) {
x = (2048/4096);
} else if (catchRate < 150) {
x = (2867/4096);
}
console.log(x)

Javascript if and else if statements [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
if (isNaN(food)){
isRunning = false;
break;
}
if (food === 'apple') {
size='1 small (4 oz.)';
calories='80 kcl';
}else if (food ==='banana') {
size='1 medium (6 oz.)';
calories='101 kcl';
}else if (food ==='grape') {
size='each';
calories='2 kcl';
}
Anyone spot the mistake of this loop?
Thanks for the people who answered my enquries
food = 'apple' assigns the value apple to the variable food. What you want to use for comparison, is the == operator, which compares food and 'apple'.
Change the if condition expression with “==“ i.e comparison operator instead of “=“ i.e. assignment operator.

javascript for loop does not work [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I'm creating a site and i needed to do some js. I'm not that good with it but tought i would figer it out. Not. I created a for loop but it does not run.
function order(user,product){
var index;
for(var i = 0; i<users.lenght; i++){
if(user == users[i]){
index = i;
break;
}
}
var budget = budgets[index];
alert(budget);
}
the creation of the users and budgets arrays are done with php and after checking with alert() it was how it should be.
Can anyone help me please?
lenght is spelt length. The misspelt property does not exist, so it undefined, which is equivalent to 0.

JavaScript "if"-statement not working [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm having the strangest problem right now; This if-statement below should run the console.log("It's night!") when the currentHour is between 22 and 07, but for some reason it doesn't!
console.log("hello");
console.log(currentHour);
if (currentHour >= 22 && currentHour <= 7) {console.log("Its night!");};
Console output:
hello 22
So the if-statement is not run.
If current hour is HIGHER/equal than 22 AND LOWER/equal than seven will never be true.
What you probably want:
if(currentHour >= 22 || currentHour <=7)

Categories