DEV Community

Discussion on: Daily Coding Puzzles - Oct 29th - Nov 2nd

Collapse
 
clandau profile image
Courtney

probably not the best solution but it works. (unless you want leading zeros...but I'm assuming that's not the case since it wants a number).

function reverseNum(n) {
    const divisor = 10;
    let reversed = 0;
    let holder = [];
    while(n > 0) {
        holder.push(n % divisor);
        n = (Math.floor(n / divisor));
    }
    let place = Math.pow(10, holder.length-1);
    for(let i = 0; i < holder.length; i++) {
        reversed += (holder[i] * place);
        place /= 10;
    }
    return reversed;
}