🔀 .split versus .slice


Here is the background context for the following code (which works!): a number rounds up if the tenths digit is greater than or equal to 5, but rounds down if the number is less than 5.

I’m wondering if I can replace the .split method with .slice though, and how the code for using .slice instead would look? Any insight is appreciated. Thank you in advance!

function roundNum(n) { let startingNum = Math.random(); console.log(startingNum); let biggerNum = 100 * startingNum; console.log(biggerNum); let decimalDigits = (biggerNum + "").split(".")[1]; console.log(decimalDigits[0]) if (decimalDigits[0] >= 5) { return Math.ceil(biggerNum) } else { return Math.floor(biggerNum) } }; console.log(roundNum());

I think that you’d need to find the index of the decimal point in order for .slice to be useful.