Unhappy chrome because inefficient function?

My (JavaScript) code is so bad, I think I broke my browser! I was working on a problem to get the last digit of a number and this is what I came up with:

function getLastDigit(num) {
            let newNum = num;
            while (newNum > 9) {
                newNum-=10;
                }    
            return newNum;
        }

It worked fine for my first several test cases, but when I put in something like getLastDigit(343243242342), the console gave me a blue arrow and wouldn’t do anything. It wouldn’t even refresh and it took a while for me to close it.

Was it just because the function was still running because it was so inefficient?

yes, inefficient code can freeze browser

hints for different approaches:

approach number 1

%

approach number 2

you can take the last character of a string

1 Like

Wow, those were really good hints. Here’s what I came up with now:

 function modDigit(num) {
           return num % 10;
        }

and 

        function stringDigit(num) {
            let temp = num.toString();
            return parseInt(temp[temp.length - 1]);
        }

awesome! good job!

Happy coding!