Return number for each letter in a string - Javascript Array Operation

Javascript examples for Array Operation:Associative Array

Description

Return number for each letter in a string

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript">
    window.onload=( function() {//w ww .ja  v a2  s  . c om
var alphabet = {
    a: 1,
    b: 2,
    c: 3
}
var word = "aab";
var total = 0;
for (var i = 0; i < word.length; i++)
    total += alphabet[word[i]];
console.log(total);
    });

      </script> 
   </head> 
   <body>  
   </body>
</html>

Related Tutorials