Complex jQuery if statements including operators [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 8 years ago.
Improve this question
I would like to show a different div depending on what class my 3 span elems contain.
If all span hasClass up or up1 the code would show a div with class allUp . If it hasClass up up1 and down then it would show a div with class twoUp.
I wrote the following, but of course it doesn't work.
var $line1 = $(".line1")
var $line2 = $(".line2")
var $line3 = $(".line3")
if($line1.hasClass("up") || $line1.hasClass("up1")
&& $line2.hasClass("up") || $line2.hasClass("up1")
&& $line3.hasClass("up") || $line3.hasClass("up1")) {
$(".allUp").show();
}
else if ($line1.hasClass("up") || $line1.hasClass("up1")
&& $line2.hasClass("up") || $line2.hasClass("up1")
&& $line3.hasClass("down") || $line3.hasClass("down1")) {
$(".twoUp").show();
}
else if ($line1.hasClass("up") || $line1.hasClass("up1")
&& $line2.hasClass("down") || $line2.hasClass("down1")
&& $line3.hasClass("down") || $line3.hasClass("down1")) {
$(".oneUp").show();
}
else {
$(".down").show();
}
think I've fixed the syntax errors

This should work, and tidies your logic up quite a lot. I've also updated it with your class-names for lines 1, 2 and 3.
var $lines = $('.line1, .line2, .line3'),
numUp = $lines.filter('.up, .up1').length,
classes = [ 'allDown', 'oneUp', 'twoUp', 'allUp' ];
$( '.' + classes[numUp] ).show();

the var $('line') = line should be var line = $('line'), and elseif should be else if

Related

Why some parts of this code are red in VS Code? [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 3 months ago.
Improve this question
I just started to learn JavaScript. I have this script which should generate composite numbers in my separate HTML file, but I don't really understand why some parts are highlighted red when I moved script to .js file.
So my question is why there are some parts that are highlighted and how I'm suppose to write it correctly? I need to use strict in this script. I know that when using strict mode I need to declare variables, objects etc.
since you are working in a javascript file.
remove the script tags
you only need script tags in other file types like html etc
function checkComposite(num) {
var arr = [];
if( var num == 1 ) {
return false;
}
else if ( var num == 2) {
return false;
}
for (var x = 2; x < num; x++) {
if(num % x == 0) {
return true;
}
}
return false;
}
function compositeNumbers() {
num = Number(document.getElementsById('number').value);
for(var j = 1; j < num; j++) {
if(checkComposite(j)) {
arr.push(j);
}
}
document.getElementsById('result').innerHTML = arr;
}

Two conditions in if statement in javascript does not appear to work correctly [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 3 years ago.
Improve this question
I am unable to get a multiple if statement to work properly.
This is an extract of code from TileBoard for HomeAssistant.
Here's what I have:
title: function (item, entity) {
var start = entity.attributes.next_activity_start;
var hold_state = entity.attributes.hold_state;
var hold_until = entity.attributes.hold_until;
if ( hold_state == "on" && hold_until == "null" ); { return 'N/A - Hold'; } return timeAgo(start);
},
hold_state gets populated with "on" and hold_until gets populated with "null" which is exactly what I am expecting in this code. But, then the if clause seems to only really work properly by looking at the first condition, and not properly taking into account the second condition (hold_until == "null"). Am I using an incorrect syntax in this if statement? I'm wanting it to return "N/A - Hold" if hold_state is on AND hold_until is null.
I think its because, you are expecting a string null. Type expecting null without the string type, also remove the ; after if case
if ( hold_state == "on" && hold_until == null ) { return 'N/A - Hold'; } return timeAgo(start);
if ( hold_state == "on" && hold_until == "null" ); //this semicolon should be removed
{
return 'N/A - Hold';
}
return timeAgo(start)
Also look at your condition. I assume you want to check if your variable is not null:
if (hold_until != null && hold_state == "on")

Codes block has an syntax error [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
For some reason this code block is giving an error and i can't figure out what is the matter with it. Any help is appreciated.
function Submit(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = e.getActiveSheet();
var range = e.getActiveRange();
var cell = range.getActiveCell();
var cellValue = ss.getActiveCell(9,17);
if( cellValue == <0 ){
var report = s.getDataRange("A101:Q106");
var numColumns = s.getLastColumn();
var targetsheet = ss.getSheetByName("Report");
var date = s.getSheetName();
var target = targetsheet.getRange(targetsheet.getLastColumn() + 1 , 1);
e.getRange(report).copyTo(target);
}
}
This is the line that is causing the syntax error:
if( cellValue == <0 ){
If you just want to know if cellValue is equal to 0, then you should change it to:
if( cellValue == 0 ){
If you're trying to see if cellValue is less than 0, then the proper way is:
if( cellValue < 0 ){
See http://devdocs.io/javascript/operators/comparison_operators for more information.

Javascript for loop with if statement not reaching the else if statement [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
why wont this reach the else if and return (i + 0) / 2? Also, why wont the alert give me i + 0 for a 2 digit value? (ie: 10, 20, 30, 40, etc. Any help would be appreciated.
var key= "OSN0MSA9991UNAAM8ELDPBD9F57BD6PU6BVBN54CDLEGDSUSNS";
var x = 0;
if (key[20] != "P" || key[18] != "P") {
x = 0;
for (i=0;i<10;i++) {
if (key[26] == i) {
x = i + 0;
alert(x);
}
};
} else if (key[20] == "P") {
for (i=9;i>-1;i--) {
if (key[26] == i) {
x = (i + 0) / 2;
alert(x);
}
};
};
your value at key[18] is "L" so if condition is always true and you will get an alert with value 7
It's not hitting the "else if" I believe because your array starts at 0, and the key[20] is in fact a P, so it's going to always fall in to the first condition and not hit the else if. EDIT: My mistake, misread. You could alert out the key[20] and key[18] to see what it thinks those values are.
Your issue is with the key[18]. Since you have an OR and key[18] = L (hence not P)..

Javascript copy text script not working properly? [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 have this javascript for copying text but for some reason it isn't working and i for the life of me can't figure out what!
<script>
function copyText(field) {
var selectedText = document.selection;
if (selectedText.type = 'Text') {
var newRange = selectedText.createRange();
field.focus();
field.value = newRange.text;
} else {
alert('select a text in the page and then press this button');
}
}
</script>
if (selectedText.type = 'Text') {
should be
if (selectedText.type == 'Text') {
= is for setting
== is for comparing
You have a typo in your if conditional:
if (selectedText.type = 'Text')
should be:
if (selectedText.type == 'Text')

Categories

Resources