What is the best way to check >= and <= in Javascript? - javascript

I have a number.
I then have an if statement that I want to go like this:
if (5 <= variable <= 10)
So if the number is between 5 and 10, the statement should be true.
What's the best way to go about this?
Thanks.

it is
if (5 <= variable && variable <= 10)

if ((variable >= 5) && (variable <= 10)) works.
If you do this frequently, consider defining a bounds function:
function inBounds(value, min, max){
return ((value >= min) && (value <= max));
}

Actually, if ( 5>= variable >= 10 ) means if(false)
if you means variable between 5, 10 it should be 5 <= variable <=10, and in javascript, the best convention is const value at left, which is from c language if (v=1), if (1=v) problem. so the best is:
if (5 <= variable && 10 >= variable) {

It is my understanding that at the first conditional false, it stops the checking and moves to the next statement.
Given that, you should examine the most often false condition and put it first.
if ((variable >= 5) && (variable <= 10))
if it is more likely to be less than 4 or
if ((variable <= 10) && (variable >= 5))
if the failure of 10 or greater is more likely to occur.

Related

Javascript - Calculate and compare

I'm learning JS, and in my practice I can't get the second part of my script to work for me.
I have 3 inputs (#ancho - #fondo - #alto), where I write integers number for a multiplication between them, followed by a division. This part works fine
Now the problem:
In the second part, I want to compare if the value "r =" is greater or less than the number written (por the user) in the input #peso, but it doesn't work for me.
What am I doing wrong here?
See the DEMO in JSFiddle
Thanks in advance!
HTML:
<input id="ancho"> <!-- Example value="22" -->
<input id="fondo"> <!-- Example value="20" -->
<input id="alto"> <!-- Example value="16" -->
<input id="peso"> <!-- Example value="3" -->
<div id="resultado"></div>
<div id="hacercalculo" onclick="calcular();comparar()">Calcular</div>
<!--css display none-->
<div id="uno">1</div>
<div id="dos">2</div>
<div id="tres">3</div>
jQuery:
https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js
Script:
// Calculate - This part works fine
function calcular(){
v1 = document.getElementById("ancho").value;
v2 = document.getElementById("fondo").value;
v3 = document.getElementById("alto").value;
r = v1*v2*v3/4000;
//document.getElementById("resultado").value = r;
document.getElementById("resultado").innerHTML = r;
};
//=============
// Compare - This, It does not work for me
function comparar() {
var camp1= document.getElementById("resultado");
var camp2= document.getElementById("peso");
//if (camp1.value >= 0 && camp2.value <= 3) {
//if (camp1.innerText >= 0 && camp2.innerText <= 3) {
//if (camp1.innerHTML >= 0 && camp2.value <= 3) {
if (camp1.innerText >= 0 && camp2.value <= 3) {
$("#uno").show(); // This, It does not work for me
}
};
Just a small amendment to "if statement" in your comparar() function.
textContent has to be converted to number.
if (Number(camp1.textContent) >= 0 && camp2.value <= 3) {
$("#uno").show();
}
Please refer to this code pen example after this change,
https://codepen.io/SathishVel/pen/KKVMvqj
If r is greater than or less than peso then r != peso. The following comparar function should handle the second part.
function comparar() {
var camp1= document.getElementById("resultado").innerHTML;
var camp2= document.getElementById("peso").value;
if (Number(camp1) != Number(camp2)) {
$("#uno").show(); // condition met hence #uno rendered.
}
};
There was some issues with your code hence why i didn't work
first you should get the value using textContent
var camp1= document.getElementById("resultado").textContent;
second you are retrieving the value twice
var camp2= document.getElementById("peso").value;
if (camp1 >= 0 && camp2.value <= 3)
it should be (camp1 >= 0 && camp2 <= 3)
finally for your code to work your if statement has to return a value
if (camp1.innerText >= 0 && camp2.value <= 3) {
return console.log('..show one') // use the return keyword to return value
}
try this
function comparar() {
var camp1= document.getElementById("resultado").textContent;
console.log(camp1)
var camp2= document.getElementById("peso").value;
console.log(camp2)
if (camp1 >= 0 && camp2 <= 3) {
$("#uno").show();
}
}
</script>
From what you said you need to check if the values ​​are in a certain range and also want to check if it has decimal places or not, so I will create two functions that do these two checks.
Function to check if a value is in a certain range:
function interval(a, b, c) {
return ((b - a) * (b - c) <= 0)
}
Example of use:
interval(2, 5, 6) // true (2 < 5 < 6)
interval(2, 1, 6) // false (2 < 1 < 6), false because 2 < 1 is false
interval(2, 7, 6) // false (2 < 7 < 6), false because 7 < 6 is false
Font: Check if a value is within a range of numbers credits Alexander
Function to check if the number has decimal places:
function isDecimal(a) {
if(Number(a) == Math.floor(a)) {
return false
} else {
return true
}
}
Example of use:
isDecimal(2) // false
isDecimal(2.2) // true
Note: In your algorithm you are using the following variables:
var camp1= document.getElementById("resultado");
var camp2= document.getElementById("peso");
They are in string format, so you need to transform them into numbers before using them in these functions, so below them do:
camp1 = Number(camp1)
camp2 = Number(camp2)

If a<x<b and C<y<d and x>y return value

Can some one explain if this is correct in JavaScript?
Or how to achieve that?
X - constant imput
y - constant imput
X>Y
If ay return value
Option 1: if (100=<x=<500 & 1=<y=<7) return 5;
Or
Option 2: if (100<=x & x=<5000 & 12<=y & y<17) return 5;
This should work:
if (x >= 100 && x <= 5000 && y >= 12 && y<17) return 5;
To be able to return something, you need to use this statement within a function. Otherwise, you need to define what you want to do with the 5.

How to increment a conditional statement based on the five times table?

I am trying to achieve the following within my conditional statement:
if (changeCount == 5 || changeCount == 10 || changeCount == 15 || changeCount == 20) {
Do Something
}
I want to let the conditional statement pass if the value of the changeCount is any number in the five times table.
At the moment I have used the || however this is no good because I need it to be infinite.
I feel like a noob asking this question but any help would be great.
Thanks
Use the remainder operator (also known as the mod operator)
if (changeCount % 5 === 0) {
// do something
}
It works by dividing arg1 by arg2, and returning the remainder. If the remainder is 0, then it divides evenly.
If you wanted to limit the range of changeCount and still ensure that it is divisible by 5, you could do something like this:
if (changeCount >= lowerBound
&& changeCount <= upperBound
&& changeCount % 5 === 0) {

What's the difference between && and || in JavaScript? [duplicate]

This question already has answers here:
Logical operators in JavaScript — how do you use them?
(2 answers)
Closed 5 years ago.
Here's a perfect illustration I saw.
&&
if (num > 5 && num < 10) {
return "Yes";
}
return "No";
||
if (num > 10 || num < 5) {
return "No";
}
return "Yes";
What's the difference between these two?
They are AND (&&) and OR (||) operators.
In your first example the response will only be "Yes" if num is greater than 5 AND less than 10, so any value for num between 5 and 10 (exclusively) will return "Yes".
In your second example "No" will be returned if num is greater than 10 OR less then 5, so any value between 5 and 10 (inclusively) will return "Yes".
To make the expression
num > 5 && num < 10
negative,
!(num > 5 && num < 10)
you need to apply De Morgan's law
!(num > 5) || !(num < 10)
and reverse the condition inside of the parens
num <= 5 || num >= 10
In your question you have the expression with logical Or ||
num > 10 || num < 5
which is, despite the order, obviously not the same as the above expression. The result of comparing values is not the same.

Javascript if statement in RPG

I'm learning about canvas by making a simple RPG.
I'm trying to make it so that if you walk into a certain area, it runs a function. So I used an if:
if (x<150, x>50, y<150, y>50) {
(code I want to execute)
}
But even when the statement is false, it's still considered true. I want it so that all statements in the parentheses must be true for the code to execute. Any help?
Use &&:
if (x < 150 && x > 50 && y < 150 && y > 50) {
// (code I want to execute)
}
Or to separate each part for readability:
if ((x < 150) && (x > 50) && (y < 150) && (y > 50)) {
// (code I want to execute)
}
To learn more, check out:
Logical Operators
Use &&, not ,:
if (x<150 && x>50 && y<150 && y>50) {
Using && means you want all of the conditions between the brackets evaluated. The code below will be executed if x is less the 150 AND y is less than 150 AND x is greater than 50 AND y is greater than 50. If only 3 of the conditions are satisfied, the code will not run. As a result your new code will look like this:
if (x < 150 && x > 50 && y < 150 && y > 50) {
// (code I want to execute)
}
Using || basically means or, and the code gets executed if any of the conditions are valid. The code below will be executed if x is less the 150 OR y is less than 150 OR x is greater than 50 OR y is greater than 50. In effect, if even 1 of the conditions is met the code will run.
if (x < 150 || x > 50 || y < 150 || y > 50) {
// (code I want to execute)
}
You can find a simple tutorial here

Categories

Resources