Code always showing same output (Problem with If statements in JavaScript) [closed] - javascript

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I tried making a tool that calculates battle out comes but this part of the code always displays: "DRAW!"
function battle()
{
var rawpower = document.getElementById('rawpower').value;
var rawpoweropp = document.getElementById('rawpoweropp').value;
if(rawpower > rawpoweropp){
alert("You won!");
} else if(rawpower < rawpoweropp){
alert("You lose!");
} else{
alert("Draw!");
}
}

The element with id="rawpower" is a paragraph tag <p>. These elements do not have values. So document.getElementById('rawpower').value returns undefined, and same for the other line. undefined is not less than undefined, nor is it greater than undefined, so you're going into the third case.

Related

Why is 'Bring an umbrella' the correct answer? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I am using the Grasshopper app on my phone and I do not understand an example they gave me for if then statements.
They give you the solution because I answered incorrectly, but I do not understand why the solution given is correct.
var todayWeather = 'rainy';
var tommorrowWeather = 'cloudy';
if (todayWeather === 'rainy') {
print('Bring an umbrella');
}
if (todayWeather !== 'rainy') {
print('Maybe the sun will come out');
}
They say the correct answer is 'Bring an umbrella'. But why is this what this code will produce if it is run?
translating the code to english:
create a variable named todayWeather and set it to rainy
create a variable named tommorowWeather and set it to cloudy
if the variable todayWeather is rainy (true) then
print to the screen Bring an umbrella
(close if conditional)
if the variable todayWeather is not rainy (false, because it is set to rainy) then
print to the screen Maybe the sun will come out
(close code conditional)
Note the print statements only execute if the condition is met.
Also the variable values are never changed by this code after they are set.

If and else statement in Javascript is not working [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
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.
Improve this question
want to run this javascript when there the button is clicked, if errors on the form check if statement and stop, if no errors execute the else statement......
$(document).ready(function () {
$('#continueToAttachments').on('click', function() { //on click button
if ($("#cevent").valid() = false) { // if errors stop the user from moving forward
return;
} else { // expecting this to run if there are no errors in the form
$('#Event-Information').removeClass('w--tab-active');
$('#Attachments').addClass('w--tab-active');
$('#Event-Information-Tab').removeClass('w--current');
$('#Attachments-Tab').addClass('w--current');
}
});
});
To check if a value is equal another you should use == or === to check if the variables are the same type too. = is for attribution.
if ($("#cevent").valid() == false)

For loop skipping index in JavaScript [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I wrote a for loop in JavaScript which is simple and straight forward. But, it is skipping the 2nd index. I don't understand why?
Below is my code snippet :
if($scope.usersChoice.length == $scope.correctAnswers.length){
for(var p=0;p<$scope.usersChoice.length;p++) {
if($scope.usersChoice[p] == $scope.correctAnswers[p]){
$scope.score++;
}
}
}
Here the length is 10.
How do you know it is skipping the 2nd index? Because it should'nt.
Can you show us your arrays ?
By the way, you can use forEach instead of for loop in this case, since you don't seem to need to break your loop:
if($scope.usersChoice.length == $scope.correctAnswers.length){
$scope.usersChoics.forEach($choice,$index=>{
if($choice === $scope.correctAnswers[$index]) $scope.score++;
});
}

Checking if checkbox is checked, display both booleans [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I am trying to determine whether the checkbox is checked or not, but when I push the checkbox, the javascript displays:
false
true
And when I push it again it changes to
true
false
And then it continues as I push further...
How can I only display one of those?
Here is my javascript code:
function selected() {
const bg = document.getElementById("myCheck").checked;
console.log(bg);
if (bg == 'true') {
document.getElementById("changeBG").style.backgroundColor = 'red';
} else {
document.getElementById("changeBG").style.backgroundColor = 'green';
}
}
This is incorrect:
if (bg == 'true') {
The checked property gives you a boolean, not a string. true is a string.
Just use:
if (bg) {

Validation with If-Else statement [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am trying to put add a validation condition to a combobox. I have been able to get it to work on other combo boxes, but here I am trying to essentially add 2 validaitons on one combobox. I am not familiar with how the whole validation process works and the order of operation. My code has become convoluted and need help sorting it out.
This is the code on the validation that I am working with:
functionvalidateSLBox(v){
if(storeSpringLync.findExact('disp',
v)>-1)returntrue;elsereturn'Notvalid';else{
if(v=='DC'){
cbSLBox.enable();
}else{
cbSLBox.disable();
}
}
}
When you return from a function, you exit at that point. Nothing else after it in the function executes, so you will never reach the second half of your function.
Also, one else matches to just one if. You have two elses here for one if.
You probably want something like this:
functionvalidateSLBox(v){
if(v=='DC'){
cbSLBox.enable();
}else{
cbSLBox.disable();
}
if(storeSpringLync.findExact('disp',v) > -1){
return true;
}else{
return 'Not valid';
}
}
This will allow you to both enable cbSLBox (whatever that is) while also returning true or Not valid... If this isn't what you want, you could use a switch statement or just nest the if statement. It really depends on what you want to do precisely, which is hard to tell from your code sample and description.

Categories