How to write this if() statment in java [closed] - javascript

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 1 year ago.
Improve this question
Hi I was wondering how do you write this in an if() statment in java?
"If either polonium dips below 0.75, spider_venom goes above 0.52, or perhaps sarin dips below 0.66, increase paracetamol by 0.01"
I wrote this but it is wrong.
if((polonium < 0.75 && spider_venom > 0.52) || (sarin < 0.66))
{
paracetamol += 0.01
}

This should work:
if(polonium < 0.75 || spider_venom > 0.52 || sarin < 0.66)
{
paracetamol += 0.01
}
The reason your code is incorrect, is that you have an AND in the condition statement. Your conditions could be translated as, IF any of those three conditions hold, THEN do increase paracetamol by 0.01, which is just OR-ing all three statements.

Related

How do I solve X is 7 or more greater than Y [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 months ago.
Improve this question
How do i solve this problem:
if X is 7 or more greater than Y in javascript show green.
if X if from 4-7 greater than Y show blue.
else show red
this is what i have tried so far:
function solve (x,y) {
if(x > (y +7) ){
return "green"
}
else {
return "red";
}
}
Probably because that function isn't closed
By the way you can't say something ISN'T WORKING if you don't provide the complete code that doesn't work and the error
Anyway
The right code would be
function solve (x,y) {
if(x >= (y +7) ){
return "green"
}
else {
return "blue";
}
}

What is an example of a "custom function" and "recursion" [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 2 years ago.
Improve this question
I have been asked to show I have used these processes in my program but from looking up the definitions, I don't know what they mean. I am confident my program is complicated enough that it uses these processes, but I don't know exactly what they are. What would be an example of these processes used in javascript?
Unsure what you mean by custom function - but recursion is just a function that calls itself.
Example of recursion
countNumTimesToZero = (myNum, count = 0) => {
if (myNum - 1 === 0) return count + 1;
return countNumTimesToZero(myNum - 1, count + 1);
}

how to do a loop in JS with 2 breaks [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 want to do a loop with JS that has two breaks. For example, 1 to 9, and 15 to 29. Maybe a double loop?
Its a noob question I know, but i am a beginner.
Thank you.
You can do it using a for loop like this:
for (var a = 0; a < 30; a++) {
if (a >= 1 && a <= 9) {
console.log("do this");
}
if (a >= 15 && a <= 29) {
console.log("do that");
}
}

For loop JS for a calculator [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 5 years ago.
Improve this question
I'm trying to create a calculator which goes through 18 units. I wanted to make my code shorter by using a for loop. I thought something like this would work:
var i=0;
for (i=0;i<=18;i++)
{
if (Unit[i] = "P" or Unit[i] == "p")
{
UnitTotal[i] = 70;
SetCookie('UnitAns'[i],UnitAns[i]);
}
}
This doesn't work what am I doing wrong or what do I need to do differently?
Unit[i] = "P"
Unless it throws an exception because Unit isn't defined, this will always be true. = is an assignment, not a comparison.
or
or is not a keyword in JavaScript. The OR operator is ||.

Using if statement inside if statement, good or bad practice? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
Is it OK to use if statement in this form, or should i need to use something else to get result. I want to get effect if filtration is on 100 % then check cX if is on 100 % to add sign-ok not before filtration is on 100 %.
Just need opinion is it a good coding or I should change the approach?
if (filtration === 100) {
$(".filtration").removeClass('sign-no');
$(".filtration").addClass('sign-ok');
if (cX === 100){
$(".cX").removeClass('sign-no');
$(".cX").addClass('sign-ok');
if (stripping === 100) {
$(".stripping").removeClass('sign-no');
$(".stripping").addClass('sign-ok');
if (precipitation === 100) {
$(".precipitation").removeClass('sign-no');
$(".precipitation").addClass('sign-ok');
}
}
}
}
No issues. You can use. But if only first condition is true, then second condition will be checked and if it is true only it check the third condition and likewise.
Its okay, you can do it. But if you set a value on code quality, you should use maximal 2 depth in a block.
Must Code Quality Tools has a configuration for it.
ESLint
JSLint
....

Categories

Resources