Javascript if and else if statements [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 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.

Related

Javascript if statement: Which pair of characters in this code is optional? [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 was given this is a multiple choice JS question. I said [], I just wanted to check if this is correct and if not why?
Consider this if statement:
if (loggedIn) {
body_classes = ["user-active"];
}
Which pair of characters in this code is optional?
""
()
{}
[]
The optional characters are the braces {}
if (loggedIn) {
body_classes = ["user-active"];
}
is the same as
if (loggedIn)
body_classes = ["user-active"];
If you remove the square brackets then body_classes becomes a string rather than an array.

if and else if statements ignored, Javascript [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 have a purple button. It becomes green onclick, even if in the if statement instead of purple I wrote Yellow.
Then it remains green, although the else if statement should make it purple again onclick.
What's wrong with my code?
`var m = document.getElementById ("buttonanimation");
function changecolor(){
if (m.style.backgroundColor="yellow"){
m.style.backgroundColor="green";
} else if (m.style.backgroundColor="green"){
m.style.backgroundColor="purple";
}
}`
This symbol = is the assignment, not the comparison. if (m.style.backgroundColor="yellow") should be if (m.style.backgroundColor === "yellow").
use comparison operator if (m.style.backgroundColor==="yellow") instead of if (m.style.backgroundColor="yellow")

Regex Issue JavaScript [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
I'm trying to take a string and check whether or not it contains a lowercase letter or number, and then if so push that letter or number to an array.
for(let i = 0; i < datearg.length; i++)
{
log.info(datearg.charAt(i));
if(/[a-z]/.test(datearg.charAt(i))) letter_num++; letters.push(datearg.charAt(i));
if(/[0-9]/.test(datearg.charAt(i))) number_num++; numbers.push(datearg.charAt(i));
}
However, both if statements always evaluate to true and the arrays end up containing every single character in datearg. Anyone know why?
if(/[a-z]/.test(datearg.charAt(i))) letter_num++; letters.push(datearg.charAt(i));
is equivalent to
if(/[a-z]/.test(datearg.charAt(i))) { letter_num++; }
letters.push(datearg.charAt(i));
i.e. push is not conditional. This is the primary reason why many style guides heavily discourage control structures without braces (which only take a single statement).

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 "For loops" difference between i-5 and i=-5 [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 7 years ago.
Improve this question
I want a loop that starts with i=100, and decrements by 5 on each iteration. However, this produces an error:
for(var i=100;i>=1;i-5)
{
document.write(i+"<br />");
}
But 'i=i-5' works:
for(var i=100;i>=1;i=i-5)
{
document.write(i+"<br />");
}
Why?
The question is how we can assign a variable to a variable i=i-5.I go the question answered I taught "i=i-5" was a expression the value of i variable is i-5 and no calculation happen its just a stable variable.
The answer is that it is taking a the i value and subtracting it by 5 and not assigning.
If you just write i - 5, there is no left-hand variable, which means there is nothing which is taking the value of i and subtracting it by 5. If you write i = i - 5 then you will decrement 5 since you now have a left-hand variable.

Categories