Home > database >  how do I have a descending loop divisible by 3
how do I have a descending loop divisible by 3

Time:04-21

d. Challenge - Print the numbers 50 - 20 in descending order, but only if the numbers are multiples of 3. (Your code should work even if you replace 50 or 20 with other numbers). */

CodePudding user response:

You Shouldn't be asking homework questions. Nevertheless here's how you do it.

var lowerlimit = 30
var upperlimit = 50
for (var i = lowerlimit;i<upperlimit;i  ){
    if (i%3==0){console.log(i)}
}

CodePudding user response:

Here just find the first number which is divisible by multiple by check each number. Once the number is found, just jump to next multiple value.

let startNumber = 50;
let endNumber = 20;

const multiple = 3;

while (startNumber >= endNumber) {
  if(startNumber % multiple === 0)  {
    console.log(startNumber);
    startNumber-=multiple;
  } else {
    startNumber--;
  } 
}

CodePudding user response:

for (let i = 50; i >= 20 ; i--) {
    if ( i % 3 === 0) console.log(i);
}

  • Related