- 1
How can I print a math calculation without decimal point numbers in Javascript
/* Snail in the well challenge, I think I'm close but can't figure out what I'm doing wrong, please read the comments at the end */ function main() { var depth = parseInt(readLine(), 10); //your code goes here var distance = 0; var days = 1; var t = 0; for (; distance <= depth; distance++) { t+=1; if (days == depth ) { t+=7; break; } distance +=4; } console.log(t); } /* I can also try this answer = depth / 5 but I will get a decimal point number like 8.4
5 Answers
+ 3
Use:
Math.floor() // or
Math.ceil() // or
Math.round() // or
+ 3
either convert your float value to int by using | (bitwise OR) 0, or rounding it (round, floor, ceil) or truncate it... or convert it to string using .toFixed(0) method ^^
+ 1
Or maybe you can just change your code to this ,
var distance = 0;
var days = 0;
while(true){
days+=1;
distance+=7;
if(distance>=depth){
break
}
else{
distance-=2;
}
}
console.log(days);
As others suggested floor or ceil , but you need to use floor if depth is 42 but ceil if depth is 126 !
0
function main() {
var depth = parseInt(readLine(), 10);
//your code goes here
let distaceCover, days;
distaceCover = depth % 5;
days = depth - distaceCover;
if (distaceCover > 2) {
console.log((days / 5) + 1);
} else {
console.log(days / 5);
}
}
0
Thank you