Created
August 25, 2015 04:38
-
-
Save Jiert/6d42d1322336b52c3e52 to your computer and use it in GitHub Desktop.
An interview test: Convert a string "1234" into an integer 1234 without using parseInt or Number
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function converter(st){ | |
var arrSt = st.split(''), | |
arrInt = []; | |
for (var i = 0; i < arrSt.length; i++){ | |
arrInt.push(arrSt[i].charCodeAt() - 48); | |
} | |
// loop over arry again | |
var counter = 0, num; | |
for (var j = 0; j < arrInt.length; j++ ){ | |
num = Math.pow(10, arrInt.length - j - 1 ); | |
counter += arrInt[j] * num; | |
} | |
return counter; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is just stupid