How can I print a math calculation without decimal point numbers in Javascript | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 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

5th Jul 2021, 9:22 PM
Natanael
5 Answers
+ 3
Use: Math.floor() // or Math.ceil() // or Math.round() // or
5th Jul 2021, 9:30 PM
Apongpoh Gilbert
Apongpoh Gilbert - avatar
+ 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 ^^
5th Jul 2021, 9:31 PM
visph
visph - avatar
+ 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 !
5th Jul 2021, 9:44 PM
Abhay
Abhay - avatar
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); } }
5th Jul 2021, 9:47 PM
Apongpoh Gilbert
Apongpoh Gilbert - avatar
0
Thank you
6th Jul 2021, 10:33 PM
Natanael