SyntaxError: expected expression, got '||' [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
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)

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.

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.

Return the first "n" count of characters of a string ( 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 7 years ago.
Improve this question
I've used this :
String.Prototype.left = function left(count){
return this.substr(0,count);
}
And apply it this way :
var string = "Hello Stack!";
console.log(string.left(5));
And the console tells me :
TypeError: example.left is not a function
console.log(example.left(5));
How can I fix it? And where is the problem?
String.prototype.left = function(index){
return this.substring(0,index);
}
var str = "Hello World";
console.log(str.left(5));

document.getElementByID not a function Quick fix [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'm studying for an exam and I'm doing some sample problems by myself. I've ran into the Uncaught TypeError: document.getElementByID is not a function problem.
Here is my code:
<script>
function checkValidity(){
//Create variable to check for errors
var input = document.getElementByID('myID').value;
if(input < 0){
document.getElementByID('errorCheck').textContent = 'The value must be a positive integer';
}else if(ifNaN(input)){
document.getElementByID('errorCheck').textContent = 'Not a number. The value must be a positive integer.';
}else if(input == null){
document.getElementByID('errorCheck').textContent = 'Please input a value.';
} else{
document.getElementByID('errorCheck').textContent = 'Valid number.';
}
}
document.getElementByID('validateid').onclick = checkValidity;
It yells at me at this line of code saying it is invalid.
document.getElementByID('validateid').onclick = checkValidity;
I know its a small mistake. I'd appreciate if someone pointed it out.
You're looking for document.getElementById, lowercase d.
Looks like a typo. You need to do:
document.getElementById('errorCheck')
with lowercase 'd'. Here is a reference link.

Categories