how to do a loop in JS with 2 breaks [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 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");
}
}

Related

How to write this if() statment in java [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 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.

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 I can return false? [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
How I can return false if user.name < 2?
https://jsfiddle.net/q6en1cd2/
const user = new Users();
user.name = 'Aa';
If you mean the length of the user.name:
if (user.name.length < 2) { return false; }
Each String object has a length property you can use:
if (user.name.length < 2) {
// go bananas
}
See the documentation.

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 ||.

javascript recursion rewriting with iterative array stack [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 6 years ago.
Improve this question
Need a template for rewriting a recursive javascript function as an iterative array stack. For hope that this approach is faster than standard recursion and would use less memory. I use object references as parameters. My function is negascout, but I'd love to reverse engineer the smaller and elegant Fibonacci_sequence.
A Simpler example would be to rewrite Fibonacci_sequence.
From rosettacode for Fibonacci_sequence
function fibonacci(n) {
if (n < 2){
return 1;
}else{
return fibonacci(n-2) + fibonacci(n-1);
}
}
console.log(fibonacci(7));
//Returns 21
An iterative way can be:
function iterativeFibonacci(n){
if (n < 2){
return 1;
}
var i;
var fibs = new Array();
fibs.push(0);
fibs.push(1);
for(i=0; i<=n; i++){
fibs.push(fibs[0] + fibs[1]);
fibs.shift();
}
return fibs[0];
}
document.write(iterativeFibonacci(7));

Categories

Resources