-2

I'm trying to solve a problem in JavaScript i.e. to assign numbers to the alphabets so that when a user types a string, it returns the sum of the numbers assigned to the alphabets. How can I solve it? I've tried a bunch of different ways but I don't think I'm even close.

Rafia Zafar
  • 363
  • 1
  • 13
bfoley_teamug
  • 79
  • 1
  • 7

4 Answers4

1

[...] in which I can assign numbers to letters in the alphabet [...]

I suggest declaring a mapping between characters and their chosen values:

const numbers = {
  'a':  1, 'b':  2, 'c':  3, 'd':  4, 'e':  5, 'f':  6, 'g':  7, 'h': 8,  'i':  9,
  'j': 10, 'k': 11, 'l': 12, 'm': 13, 'n': 14, 'o': 15, 'p': 16, 'q': 17, 'r': 18,
  's': 19, 't': 20, 'u': 21, 'v': 22, 'w': 23, 'x': 24, 'y': 25, 'z': 26
};

[...] when a user types a word [...]

You need an <input> element and listen to its "input" events.

[...] it returns the sum of the letters' numerical values [...]

Use Array.map to convert letters to their numbers by looking up each letter's value given by numbers and compute the sum of the resulting array:

const numbers = {
  'a':  1, 'b':  2, 'c':  3, 'd':  4, 'e':  5, 'f':  6, 'g':  7, 'h': 8,  'i':  9,
  'j': 10, 'k': 11, 'l': 12, 'm': 13, 'n': 14, 'o': 15, 'p': 16, 'q': 17, 'r': 18,
  's': 19, 't': 20, 'u': 21, 'v': 22, 'w': 23, 'x': 24, 'y': 25, 'z': 26
};

document.getElementById("text").addEventListener("input", function() {
  let str = this.value.toLowerCase();
  let sum = [...str].map(c => numbers[c] || 0).reduce((a, b) => a + b, 0);
  console.log(sum);
});
<input id="text" type="text">
Community
  • 1
  • 1
le_m
  • 19,302
  • 9
  • 64
  • 74
0

For obtaining the ASCII value of a character, use String.charCodeAt(index).

(When you say "number," it's a little ambiguous. Do you want the ASCII value that corresponds to a character? Maybe edit your question for clarity.)

Erik Hermansen
  • 2,200
  • 3
  • 21
  • 41
0

If I understand your problem well, you are trying to do this:

var obj = {
  'a': 7,
  'b': 10,
  'c': 3,
};

var input = document.getElementsByTagName('input')[0];

input.addEventListener('input', handler, false);

function handler() {
  var val = input.value,
      sum = 0,
      count = document.getElementById('count');
      
  for (var i = 0; i < val.length; i++) {
    if (obj[val[i]]) {
      sum += obj[val[i]];
    }
  }
  
  count.innerHTML = sum;
}
<input type="text" placeholder="Enter some text...">
<div id="count">0</div>

For the sake of simplicity, this implementation works only with "a", "b" and "c"...

Badacadabra
  • 8,043
  • 7
  • 28
  • 49
0

var letters = [];
var input = document.getElementsByTagName('input')[0];
input.addEventListener('input', addWordLetters, false);

for(var code=65;code<91;code++)
{
     var letter = String.fromCharCode(code);
     letters.push(letter);
}

function addWordLetters(){
    var word = input.value.toUpperCase();
    var total = 0;
    totalText = document.getElementById('totalText');
    
    for (var i = 0; i < word.length; i++) {
        var letter = word.charAt(i);
        var letterVal = letters.indexOf(letter) + 1; 
        total +=  letterVal;
    } 
    
    console.log(total);
    
    totalText.innerHTML = total;  
}
  
ADD TEXT<br>
<input type="text" placeholder="TYPE TEXT HERE...">
<div id="totalText">0</div>

This code should do the trick, and has been tested. No need to manually type all those letters. Just put them in an array by looping over their character codes until you get to z. Then use indexOf to find the array index (plus 1 of course) of each letter and add them together in a function and return the result in the function or to a text input. Make sure to uppercase the letters.