i want to print the integer always subtacted by 3

Viewed 41

i want to print like this 20 17 14 11 8. it should always subtract with 3

here is my code

let cv= 10
let sum=0

for(let i=20;i>5;i--){
    sum=i-3
    console.log(sum)
}

5 Answers

Maybe this can help:

for(let i = 20; i > 5; i = i-3){
    console.log(i)
}

Instead of reducing i by 1 in each iteration, reduce it by 3.

for(let i = 20; i > 5; i -= 3){
    console.log(i)
}

Prints if sum >= 0.

let sum = 20
const subBy = 3

while(sum >= subBy){
  sum -= subBy
  console.log(sum)
}

try this - here is your answer

input value x = 20 or anything

let x = 20;
let y =  Math.trunc(x/3);

for(let i=1; i<=y; i++){
    a = x - (3*i);
    console.log(a);
}

and you can also start loop from 0

let x = 20;
let y =  Math.trunc(x/3);

for(let i=0; i<=y; i++){
    a = x - (3*i);
    console.log(a);
}
Related