so here is the scenario
if ( ( a >= b) && ( (c < d) && (d != '') ) )
cannot get this logic on this to work correctly
so if d = '' it would cause that to be false. which would mean that the whole thing would equate to false. Problem is I need it to trigger when a >= b but also needs to include the and for c < d but only if d != '', in other words ignore the c < d part if d = '', otherwise used the c < d part to prevent a >= b from triggering.
hope this is making sense. I am trying to avoid doing and if/else or switch.
One comment would be if you are not absolutely avoid using if/else or switch then you could define funcitons use them and use thiss functions in your statement above.
Apart from that, changing the order of some expressions might do what you want. c < d would not be evaluated if d = '' is not true. a >= b would not be evaluated if the expresion before second or, ||, is true or d == '', empty string, is true.
if ((d != '' && c < d) || (d != '' && c >= d && a >= b))
which is;
if (d != '' && (c < d || a >= b))
thanks for your suggestions, I found a workaround though
I just put
if (d == '') { d = Infinity; }
then
if ( (a >= b) && (c < d) )
and that solved it
thanks for everyone's help
I have a question, my code worked but in the case that it's a bigger number and I don't have an idea of where to put the limit in my i position... How can I optimize this code, so I don't need to calculate the limit and not put so many zeros in my i? Or there's no way and this is as it should be?
function prob5(){
for(var i =1;i<10000000000;i++){
if((i%1)==0 && (i%2)==0 && (i%3)==0 && (i%4)==0 && (i%5)==0 && (i%6)==0 && (i%7)==0 && (i%8)==0 && (i%9)==0 && (i%10)==0 && (i%11)==0 && (i%12)==0 && (i%13)==0 && (i%14)==0 && (i%15)==0 && (i%16)==0 && (i%17)==0 && (i%18)==0 && (i%19)==0 && (i%20)==0) {
break;
}
}
console.log(i);
}
function getLowestNumDivisibleByAllUpTo(limit) {
const numMax = factorial(limit);
for (let num = 1; num < numMax; num++) {
let divisibleByAll = true;
for (let divisor = 2; divisor <= limit; divisor++) {
if (num % divisor !== 0) {
divisibleByAll = false;
break;
}
}
if (divisibleByAll) {
return num;
}
}
}
function factorial(n) {
if (n < 2) return 1;
return n * factorial(n - 1);
}
console.log(getLowestNumDivisibleByAllUpTo(20));
/*
the number you are looking for is called the least common multiple (lcm)
to find the least common multiple of all numbers in an array:
compute the lcm of the first two, then the lcm of the fist two with the third and so on
lcm(2,3)=6, lcm(6,4)=12, lcm(12,5)=60, etc
the following is much more efficient because it doesn't search billions of numbers
to check if they are divisible by 2 and 3 and 4 and ... 20
it just applies the same function, lcm, 18 times
*/
const limit = 20;
const divisors = [...Array(limit - 1)].map((v, i) => 2 + i);
// greatest common divisor of two numbers
const gcd = (a, b) => b ? gcd(b, a % b) : Math.abs(a);
// least common multiple of two numbers
const lcm = (a, b) => a * b / gcd(a, b);
// least common multiple of all numbers in an array
const lcma = array => array.reduce(lcm);
console.log(lcma(divisors));
I want to check if a value is a multiple of a certain number, for example, multiples of 10, but I also want to be able to change it to whatever I want.
if (directWinner == 10){
}
You'd use the modulus operator for that :
if (directWinner % 10 === 0){
directWinner = 20;
}
Added a small dose of jQuery for no good reason at all ?
$.modu = function(check, against) {
return check % against === 0;
}
if ( $.modu(directWinner, 10) ) {
directWinner = 20;
}
You'd use the modulo operator % for that:
var certainNumber = 10;
if (directWinner % certainNumber === 0) {
// directWinner is a multiple of certainNumber
}
Use the modulo operator (assuming positive integers) :
if (directWinner % 10 === 0) {
...
}
In JavaScript, I'm telling the browser to do something if the window size is greater than 500px. I do it like so:
if (windowsize > 500) {
// do this
}
This works great, but I would like to apply this same method, but with a range of numbers. So I would like to tell my browser to do stuff if the window size is between 500px and 600px. I know this wouldn't work, but here is how I imagined it:
if (windowsize > 500-600) {
// do this
}
Is this even possible, within JavaScript?
Tests whether windowsize is greater than 500 and lesser than 600 meaning that neither values 500 or 600 itself will result in the condition becoming true.
if (windowsize > 500 && windowsize < 600) {
// ...
}
I had a moment, so, although you've already accepted an answer, I thought I'd contribute the following:
Number.prototype.between = function(a, b) {
var min = Math.min.apply(Math, [a, b]),
max = Math.max.apply(Math, [a, b]);
return this > min && this < max;
};
var windowSize = 550;
console.log(windowSize.between(500, 600));
JS Fiddle demo.
Or, if you'd prefer to have the option to check a number is in the defined range including the end-points:
Number.prototype.between = function(a, b, inclusive) {
var min = Math.min.apply(Math, [a, b]),
max = Math.max.apply(Math, [a, b]);
return inclusive ? this >= min && this <= max : this > min && this < max;
};
var windowSize = 500;
console.log(windowSize.between(500, 603, true));
JS Fiddle demo.
Edited to add a minor amendment to the above, given that – as noted in the comments –
…Function.prototype.apply() is slow! Besides calling it when you have a fixed amount of arguments is pointless…
it was worth removing the use of Function.prototype.apply(), which yields the amended versions of the above methods, firstly without the 'inclusive' option:
Number.prototype.between = function(a, b) {
var min = Math.min(a, b),
max = Math.max(a, b);
return this > min && this < max;
};
var windowSize = 550;
console.log(windowSize.between(500, 600));
JS Fiddle demo.
And with the 'inclusive' option:
Number.prototype.between = function(a, b, inclusive) {
var min = Math.min(a, b),
max = Math.max(a, b);
return inclusive ? this >= min && this <= max : this > min && this < max;
}
var windowSize = 500;
console.log(windowSize.between(500, 603, true));
JS Fiddle demo.
References:
Function.prototype.apply().
Math.max().
Math.min().
I prefer to put the variable on the inside to give an extra hint that the code is validating my variable is between a range values
if (500 < size && size < 600) { doStuff(); }
It's an old question, however might be useful for someone like me.
lodash has _.inRange() function https://lodash.com/docs/4.17.4#inRange
Example:
_.inRange(3, 2, 4);
// => true
Please note that this method utilizes the Lodash utility library, and requires access to an installed version of Lodash.
You can do it simply
if (windowsize > 500 && windowsize < 600) {
//your logic
}
Or if you want clean code You can write your own function in your main js file of any common place.
Number.prototype.between = function(a, b) {
var min = Math.min.apply(Math, [a, b]),
max = Math.max.apply(Math, [a, b]);
return this > min && this < max;
};
Then you can call it like this
if(windowsize.between(500, 600)){
//your logic
}
You can use Multiple clause in if condition instead of writing
if (windowsize > 500-600) {
// do this
}
because this really makes no sense
logically JavaScript will read your if condition like
windowSize > -100
because it calculates 500-600 to -100
You should use && for strict checking both cases for example which will look like this
if( windowSize > 500 && windowSize < 600 ){
// Then doo something
}
this is a generic method, you can use everywhere
const isBetween = (num1,num2,value) => value > num1 && value < num2
Here is the shortest method possible:
if (Math.abs(v-550)<50) console.log('short')
if ((v-500)*(v-600)<0) console.log('short')
Parametrized:
if (Math.abs(v-max+v-min)<max+min) console.log('short')
if ((v-min)*(v-max)<0) console.log('short')
You can divide both sides by 2 if you don't understand how the first one works;)
I know there's an accepted answer, but I want to provide some ways that can be useful in certain scenarios. If we want to check whether n is between x and y, we can do:
x <= n && n <= y
(x - n) * (y - n) <= 0
The second one is very useful when you're trying to get the same result even if you swap x and y.
I just implemented this bit of jQuery to show and hide bootstrap modal values. Different fields are displayed based on the value range of a users textbox entry.
$(document).ready(function () {
jQuery.noConflict();
var Ammount = document.getElementById('Ammount');
$("#addtocart").click(function () {
if ($(Ammount).val() >= 250 && $(Ammount).val() <= 499) {
{
$('#myModal').modal();
$("#myModalLabelbronze").show();
$("#myModalLabelsilver").hide();
$("#myModalLabelgold").hide();
$("#myModalPbronze").show();
$("#myModalPSilver").hide();
$("#myModalPGold").hide();
}
}
});
just like David answer but i needed inclusive for either a or b. so my sol:
export const between = (num: number, a: number, b: number, inclusiveA = true, inclusiveB = true): boolean => {
if (a > b) [a, b, inclusiveA, inclusiveB] = [b, a, inclusiveB, inclusiveA];
if (a == b && (inclusiveA || inclusiveB)) [inclusiveA, inclusiveB] = [true, true];
return (inclusiveA ? num >= a : num > a) && (inclusiveB ? num <= b : num < b);
};
if (require.main === module) {
console.log(between(12, 15, 10)); //true
console.log(between(15, 15, 10)); //true
console.log(between(15, 10, 15)); //true
console.log(between(10, 10, 15, false)); //false
console.log(between(15, 10, 10, true, false)); //false
//edge case: if a==b then enough that either of the edges is inclusive
console.log(between(10, 10, 10, true, false)); //true
}
its also typescript and not javascript
function handleBetween(number, calc) {
let [a, b] = calc;
let min = Math.min(a, b), max = Math.max(a, b);
return number > min && number < max;
}
if(510 >500 && 510 <600){
console.log(`510 >500 && 510 <600 is true`)
}
if(610 >500 && 610 <600){
// false
console.log(`610 >500 && 610 <600 is true`)
} else console.log(`610 >500 && 610 <600 is false`)
console.log(handleBetween(510, [500, 600])); // true
console.log(handleBetween(610, [500, 600])); // false
I want to be able to test whether a value is within a number range. This is my current code:
if ((year < 2099) && (year > 1990)){
return 'good stuff';
}
Is there a simpler way to do this? For example, is there something like this?
if (1990 < year < 2099){
return 'good stuff';
}
In many languages, the second way will be evaluated from left to right incorrectly with regard to what you want.
In C, for instance, 1990 < year will evaluate to 0 or 1, which then becomes 1 < 2099, which is always true, of course.
Javascript is a quite similar to C: 1990 < year returns true or false, and those boolean expressions seem to numerically compare equal to 0 and 1 respectively.
But in C#, it won't even compile, giving you the error:
error CS0019: Operator '<' cannot be applied to operands of type 'bool' and 'int'
You get a similar error from Ruby, while Haskell tells you that you cannot use < twice in the same infix expression.
Off the top of my head, Python is the only language that I'm sure handles the "between" setup that way:
>>> year = 5
>>> 1990 < year < 2099
False
>>> year = 2000
>>> 1990 < year < 2099
True
The bottom line is that the first way (x < y && y < z) is always your safest bet.
You could make your own method:
// jquery
$(function() {
var myNumber = 100;
try {
if (myNumber.isBetween(50, 150))
alert(myNumber + " is between 50 and 100.");
else
alert(myNumber + " is not between 50 and 100.");
} catch (e) {
alert(e.message());
}
});
// js prototype
if (typeof(Number.prototype.isBetween) === "undefined") {
Number.prototype.isBetween = function(min, max, notBoundaries) {
var between = false;
if (notBoundaries) {
if ((this < max) && (this > min)) between = true;
alert('notBoundaries');
} else {
if ((this <= max) && (this >= min)) between = true;
alert('Boundaries');
}
alert('here');
return between;
}
}
hope this helps.
Max
The fast and simple way to make this is to create a function like this:
function inRange(n, nStart, nEnd)
{
if(n>=nStart && n<=nEnd) return true;
else return false;
}
Then use that as follows:
inRange(500, 200, 1000) => this return true;
Or like this:
inRange(199, 200, 1000) => this return false;
If you don't like the boolean operator, you could always use nested if statements:
if (1990 < year)
{
if( year < 2099)
return 'good stuff';
}
From a similar solution here: http://indisnip.wordpress.com/2010/08/26/quicktip-check-if-a-number-is-between-two-numbers/
$.fn.between = function(a,b){
return (a < b ? this[0] >= a && this[0] <= b : this[0] >= b && this[0] <= a);
}