How to add one more condition in this if block? - javascript

How to add one more condition in this if block ??
I want to run in the if block only if the quantity_traded value is greater than 800 and also less than 20000
This is my code
var difference = -3000;
var quantity_traded = 800 ;
var stockprice = 300;
if (difference >= -4000 && quantity_traded >= 800 && stockprice>=300)
{
alert('include');
}
else
{
alert('dont include');
}
https://jsfiddle.net/2ok0fcax/
Could you please let me know how to match the quantity_traded value is greater than 800 and also less than 20000 condition

just add quantity_traded < 20000 to your list of conditions
if (difference >= -4000 && quantity_traded >= 800 && stockprice>=300 quantity_traded < 20000) {
...
}
https://jsfiddle.net/2ok0fcax/1/

Related

Dry-er Way of Writing a Bunch of If Else Statements

I have a variable placement that can be set to the following values: top, top-left, top-right, bottom, bottom-left, bottom-right, right, right-top, right-bottom, left, left-top, left-bottom.
I have another variable const trigger = triggerRef.current.getBoundingClientRect(); so I can determine where the trigger element is, and based on that, set the placement variable accordingly.
I am currently using a lot of if else statements. For instance:
if (placement === "top" && trigger.top < 75 && windowWidth - trigger.right > 75 && trigger.left > 175)
{ placement = "bottom";
} else if ( placement === "top" && windowWidth - trigger.right < 75 && windowHeight - trigger.bottom > 75 && trigger.top < 75)
{ placement = "left-top"; }
...and the code goes on and on
What's a "DRY-er" way of doing this?
Consolidate the logic in these tests and only check once. Assign a variable if instead of repeating the same calculations:
if (placement === "top" && trigger.top < 75 ) {
const triggerFromWidth = windowWidth - trigger.right;
if (triggerFromWidth > 75 && trigger.left > 175) {
placement = "bottom";
} else if (triggerFromWidth < 75 && windowHeight - trigger.bottom > 75) {
placement = "left-top";
}
}

Make the code in a 'do - while' run once - Node.js

(Ik Its a stupid question) with the "do - while" the code runs multiple times at the same time (i putted a console.log('while') at the beginning, so it print 'while' when the code is executed) but I want to run it one time at a time. Here's an example:
Code:
let x = 100
let y = 100
do {
console.log('while')
/*
here there's a piece of code that let the user choose what want he to do
*/
let random = Math.floor(//random number between 1 and 2)
if (random == 1) {
y = y - 50
} else {
x = x - 50
} while (y >= 0 && x >= 0 )
Obviously the console prints 'while' until the process ends.
I tried to use for (let i; i < 100; i++) and in the for i putted a if ( y <= 0 && x <= 0) but I can't start-over the loop (I dont know how to use continue).
The main problem with your code is the brackets - your formatting is incorrect:
let x = 100
let y = 100
do {
//...
if (random == 1) {
y = y - 50
} else {
x = x - 50
} while (y >= 0 && x >= 0 );
You don't close off your "do" loop - you just go right into the while after the else. In order for your code to work properly, you need to insert a bracket after the else:
let x = 100
let y = 100
do {
//...
if (random == 1) {
y = y - 50
} else {
x = x - 50
}
} while (y >= 0 && x >= 0 );

Change font size depending on number of characters and resolution

I want some script for my app to change the font-size of a title depending on number of characters and the resolution, so to use different font-sizes in the script depending on window resolution and the number of characters of the title.
This is what I have right now:
$(document).ready(function () {
$(".boxes.concierto h2.nombreartista span a").each(function () {
var numChars = $(this).text().length;
if ((numChars >= 1) && (numChars < 20)) {
$(this).css("font-size", "20px");
}
else if ((numChars >= 20) && (numChars < 30)) {
$(this).css("font-size", "18px");
$(this).css("line-height", "20px");
}
else if ((numChars >= 30) && (numChars < 60)) {
$(this).css("font-size", "15px");
$(this).css("line-height", "18px");
}
else if ((numChars >= 100) && (numChars < 140)) {
$(this).css("font-size", "0.9em");
}
else {
$(this).css("font-size", "0.8em");
}
});
});
I have tried to add:
if($(window).width() >= 1300){
// do your stuff
}
at the begining and end of my script but is not working.
Any idea how can I join this two functions correctly?
Thank you so much
you probably want to create a condition where either the number of characters or the resolution change the text size.
use the or-operator || in your if condition
if ( ((numChars >= 20) && (numChars < 30)) || $(window).width() >= 1300 ) {
$(this).css("font-size", "20px");
}
a better way
the conditions might get quite complex. a better way would be to setup a formula like:
var textSize = 1/numChars * $(window).width() * factor;
if you want the text size to change in steps you can do something like this:
var stepSize = 3;
var steppedTextSize = Math.floor(textSize/stepSize) * stepSize;
the steppedTextSize will have values in increments of 3. like: 12, 15, 18, ...

javascript window.innerWidth conditional statement

I have a script which shows different content depending on the screen size, it looks like this:
if ((window.innerWidth < 1250 )) {
//Do something
}
I am trying to set a greater than value as well as a less than value. I thought the follwoing would work:
if ((window.innerWidth < 1250 && > 750)) {
//Do something
}
Can anyone help me out?
When using Boolean operators (&&, ||, etc), each side must be a completely valid Boolean expression on its own. && > 750 is not a valid expression, since > 750 cannot be evaluated as True or False.
What you want is :
if (window.innerWidth < 1250 && window.innerWidth > 750) {
As both window.innerWidth < 1250 and window.innerWidth > 750 are valid expressions and can be resolved independently.
Close:
if (window.innerWidth < 1250 && window.innerWidth > 750) {
You need to repeat the comparison argument after the && to check with new value.
Use like this:
if (window.innerWidth < 1250 && window.innerWidth > 750) {
function between(val, min, max)
{
return val >= min && val <= max;
}
if (between(window.innerWidth,750,1250)) {
//your code!!!
}

If variable matches 3 conditions

I need to have an if statement to check if a number is = 0, or between 5000 and 3600000. How can have it check all 3 conditions?
Number can be 0 or 5000 or above OR 3600000 or less. It can't be -1, 1.1 2.3 or anything between 0 and 5.
Here is what I have now and this work for checking if less than 5000 or greater than 3600000
var intervalCheck = interval * digit
if(isNaN(intervalCheck))
{
alert("Must be a number.");
}
else if((intervalCheck < 5000)||(intervalCheck>=3600000))
{
alert("Must be between 5 seconds and 1 hour.");
}
else
{
localStorage["interval_setting"] = interval * digit;
saveActions();
}
if ((intervalCheck == 0) || (intervalCheck >= 5000 && intervalCheck <= 3600000)) {
// put your code here
}
You can combine as many logic operations as you want into one if statement using any of the javascript logic and comparision operators.
You can see a demo work here: http://jsfiddle.net/jfriend00/y5tRK/
else if(intervalCheck === 0 || (intervalCheck >= 5000 && intervalCheck <= 3600000)) {
localStorage["interval_setting"] = interval * digit;
saveActions();
}
else {
alert('You broke something");
}
The === means to check both type and value. This makes sure that intervalCheck is both a number and equal to zero, instead of evaluating to true if intervalCheck is "" or false. Then we give our or condition. We wrap this in parenthesis becuase we need to ensure that the value falls between our two numbers. The parenthesis mean this will be evaluated as a whole with the or.
I need to have an if statement to check if a number is = 0, or between
5000 and 3600000.
if( intervalCheck == 0 || (intervalCheck >= 5000 && intervalCheck <= 3600000) ) {
//code here
}
if(intervalCheck == 0 || (intervalCheck >= 5000 && intervalCheck<=3600000))
if(!isNaN(intervalCheck) && (intervalCheck == 0 || (intervalCheck >= 5000 && intervalCheck <= 3600000)))

Categories