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.
Related
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") };
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
I have a JS function which checks if a user entered string is zero.
if (str.legth = 0) {
alert('Provide at least 1 character to create a folder.');
return;
}
But this seems to let a user pass a zero entered string.
Is there anything missing?
THanks
You've misspelled "length" and you need "==" instead of "=" to check for equailty rather than assigning a value.
If your variable is called str, then length is used to return a boolean value. You need to use either == or === for an actual comparison instead of an assignment. Additionally, a return isn't necessary.
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)
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.
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
A little confused, I have the below code that loops through an array and if the value is not in another array push it in. But whether the indexOf statement evaluates to true or false it gets pushed into the array - not quite sure why that is.
function findUnique(fieldId) {
let uniques = [];
for (let i = 0; i < data.length; i++){
console.log(uniques.indexOf(data[i][fieldId]) === -1);
if (uniques.indexOf(data[i][fieldId] === -1)) {
uniques.push(data[i][fieldId]);
}
}
return uniques;
}
Say there are 2 items in data...
first pass through will log out -1 because it is not in the array, then pushes to array...
second pass logs out 0 because it is in the array but all pushes it into the array...
uniques logs out as ['a', 'a']
Just a typo. You're currently looking for false in your array.
Change
if (uniques.indexOf(data[i][fieldId] === -1)) {
to
if (uniques.indexOf(data[i][fieldId]) === -1) {