Home > OS >  For loop float into integer
For loop float into integer

Time:08-01

For loop float to integer Hello guys, how can I change the this for loop on thanos that are float to integer?

thanos = 1
for(let day =1; day < 51; day){
console.log(day, thanos)  
day    
Math.floor(thanos)
if(day % 3 == 0){      
thanos /= 2
    }
else{
thanos *= 3

    }
}





The output is

1 1
2 3
3 1.5
4 4.5
5 13.5

How can i make thanos *= 3 that are 1.5 *= 3 into 1 *= 3 so the output

1 1
2 3
3 1
4 3
5 9

Thank you.

CodePudding user response:

Math.floor() returns a new number, doesn't modify the old one

thanos = Math.floor(thanos)

Also, remember to declare variables. I would write your code like that:

let thanos = 1
for(let day = 2; day < 51; day  = 1){
  if(day % 3 == 0){      
    thanos = Math.floor(thanos / 2)
  } else {
    thanos *= 3
  }
}

CodePudding user response:

This outputs your desired result

thanos = 1
for(let day =1; day < 51; day){
  console.log(day, thanos)  
  day    
  if(day % 3 == 0){      
    thanos /= 2
  } else {
    thanos *= 3
  }
  thanos = Math.floor(thanos)
}

  • Related