I'm trying to format a string to show two sig figs after the decimal. I want to do this without using built-in methods like 'Math' and such. I have something where I can do that, but the issue comes when the number is just '4.5' for example, I need to add a 0 onto it, but I'm having trouble. Here's my code so far.
function toFixed(num){
let elem = "";
// let x = "";
for (let i = 0; i < num.length; i++) {
if(num[i - 2] === '.'){ // num[i] happens to be the third 8
break;
}
// elem += num[i];
elem += num[i];
elem += "0";
}
return elem
}
console.log(toFixed("7.88888888")) // "7.88"
console.log(toFixed("77645345.987654")) //77645345.88
console.log(toFixed("1")) // "1.00"
console.log(toFixed("4.5")) // "4.50"
Right now I'm getting 0's after each number it seems.