Created
July 30, 2018 15:10
-
-
Save sikanrong/ac5b37f21b905a02360286d328a1bea3 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| //Microsoft Interview Question | |
| function stricmp(a, b){ | |
| var i = 0; | |
| var aAscii, bAscii; | |
| while(!isNaN(a.charCodeAt(i)) && !isNaN(b.charCodeAt(i))){ | |
| aAscii = a.charCodeAt(i); | |
| bAscii = b.charCodeAt(i); | |
| i++; | |
| if(aAscii == bAscii || (aAscii ^ 32) == bAscii){ | |
| continue; | |
| }else{ | |
| break; | |
| } | |
| } | |
| i--; //rewind to the last index before loop broke; | |
| aAscii = a.charCodeAt(i); | |
| bAscii = b.charCodeAt(i); | |
| if(aAscii == bAscii) | |
| return 0; | |
| if((aAscii | 32) < (bAscii | 32)) | |
| return -1; | |
| return 1; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment