I'm completely baffled by this, because the else if statement seems to break the program? [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 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)

Related

Number prediction from 1 to 10 from 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 1 year ago.
Improve this question
This is my code but when i run this code in console always giving me Congratulations.
Help me for solve this problem.
var val=Math.floor(Math.random() * 10) + 1;
console.log(val);
var Predict = Number(prompt("Prediction ?"));
for(var i=1 ; i <= 3; i++){
if(Predict<val){console.log("Up")};
if(Predict=val){console.log("Congratulations") };
if(Predict>val){console.log("Down")}
}
Equal operator assigns the right hand to the left hand and so the result is always true! To compare two values use double equals like this:
if (Predict==val){console.log("Congratulations") };

Unexpected number length value 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 1 year ago.
Improve this question
Why does this if statement always log true no matter how long pin actually is?
const pin = 1
if (pin.toString().length = 4 || 6) {
console.log(true);
}
//logs true
Edit: If a mod sees this, should I delete this question? I was very bad at javascript when I asked this.
Both statements in your || (or) statement will resolve to true, so the log will always be called.
pin.toString().length = 4
resolves to true because you're SETTING the length to 4 and then the check becomes 'is there a length' which is only falsy if the length === 0.
The second part of the equality is simply '6'. Any number that's not 0 is truthy, so will resolve to true.
You probably mean something like this:
const pin = 1;
if (pin.toString().length === 4 || pin.toString().length === 6) {
console.log(true);
}
This will never log true, because '1'.length === 1.

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)

Javascript compiler exception: missing ) after for-loop control [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 am getting an error "Javascript compiler exception: missing ) after for-loop control (null.null.script; line 29)" which would be the instantiation of the loop. My syntax looks correct.
a little help please...
var ciArray = [];
var i;
var check;
for (i = 0; check = false; i < ciArray.length; i++) {
if(ciValue == ciArray[i,0]){
ciArray[i,1] += timer;
check =True;
}
if(i == mciArray.length-1 && !check) {
ciArray[i+1,0] = ciValue;
ciArray[i+1,1] = timer;
}
}
You have too many arguments for your for loop - looks more like a while loop with a check clause right now.
inside of condition the loop for you have (i = 0; check = false; <--- in this options it your error.

Why do I have an infinite loop? [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 learning javascript and I don't understand why this code is creating an infinite loop.
for (var i = 8 ; i < 120; i+12) {
console.log(i * 1);
}
If I make an assignment of i like i = i + 12 then everything works like it should. Please explain what is going on here.
You'll learn that JavaScript does some wonky and questionable things...
I've never seen a use-case with the parameters you've set with a for loop, but I'm assuming you know the defaults.
Anyway, what you are doing is not increment i by 12, so it will continue to loop, given i never actually increases.
The operator += should do the trick, as it will take the previous value of i, and add 12 to it.
You need i += 12 to do assignment instead of just i + 12 (which is a plain expression).
because i+12 does not increment and store the incremented value in i. So the value of i is always 8. Wheras i=i+12 increments and stores the value in i and the loop terminates when i >= 120.
you need to use i+=12 Check out the operators of JavaScript
for (var i = 8 ; i < 120; i +=12) {
console.log(i * 1);
}

Categories