skip to Main Content

The function sumAscii should take an array of names and calculate each name’s score based on the total of each character’s lowercase ASCII value. It should return the name with the highest score. E.g. The name ‘John’ would get the score 431 because ‘j’ has the ASCII code 106, ‘o’ has the ASCII code 111, ‘h’ has the ASCII code 104 and ‘n’ has the ASCII code 110.

function sumAscii(arrayOfNames) {
  function getAsciiScore() {
    let sum = 0;
    let arrayOfAsciiCodes = [];
    for (const name of arrayOfNames) {
      name.split("").forEach((letter) => {
        sum += letter.toLowerCase().charCodeAt(0);
      });
    }
    return sum;
  }

  let highestScore = "";
  for (const name of arrayOfNames) {
    for (const letter of name) {
      return getAsciiScore(letter);
    }
  }
}

My function returns the total Ascii code score of all the names

2

Answers


  1. Your code is almost ready. You just need to fix some logic errors:

    function sumAscii(arrayOfNames) {
      function getAsciiScore(name) {
        let sum = 0;
        name.split("").forEach((letter) => {
            sum += letter.toLowerCase().charCodeAt(0);
        });
        return sum;
      }
    
      let highestScore = -1;
      let highestScoreName = null;
      for (const name of arrayOfNames) {
        let score = getAsciiScore(name);
        if (score > highestScore) {
          highestScore = score;
          highestScoreName = name;
        }
      }
      return highestScoreName;
    }
    
    console.log(sumAscii(["John", "Paul", "George", "Ringo"]));

    Here’s a shorter method:

    const sumAscii = (arrayOfNames) => {
        const getWordScore = (word) => [...word].reduce((acc, cur) => acc + cur.toLowerCase().charCodeAt(0), 0);
        return arrayOfNames.sort((a, b) => getWordScore(b) - getWordScore(a))[0];
    };
    
    // Test
    console.log(sumAscii(["John", "Paul", "George", "Ringo"]));

    But this is more inefficient than your code since it calculates each word’s score multiple times. But this way is ok for small lists of small names.

    This can be fixed with by storing the words with their score.

    const sumAscii = (arrayOfNames) => {
        const getWordScore = (word) => [...word].reduce((acc, cur) => acc + cur.toLowerCase().charCodeAt(0), 0);
        let combined = arrayOfNames.map(n => {return {name: n, score: getWordScore(n)}});
        return combined.sort((a, b) => b.score - a.score)[0].name;
    };
    
    // Test
    console.log(sumAscii(["John", "Paul", "George", "Ringo"]));

    This is still less efficient, but the code is cleaner.

    Login or Signup to reply.
  2. I just wanted to give it a shot and show another solution. I think you can use the map, reduce and Math.max function (the latter gives you the highest number in array). You can get the desired result as follow where I am returning an object with the index of the name with the highest sum, that name and the highest sum

    let myArray = ['john', '123450', 'johnDoe',  '123', '12', '1']
    
    
    function sumAscii(arrayOfNames) {
        let array2
           let SumArray = arrayOfNames.map(elem=>{
                let array1 =  Array.from(elem)
                return  array2 = array1.reduce((sum, letter)=>{return sum + letter.toLowerCase().charCodeAt(0)}, 0)
            })
            let maxScore = Math.max(...SumArray)
            let wantedIndex = SumArray.indexOf(maxScore)
            return  {'index' : wantedIndex, 'name' : myArray[wantedIndex], 'maxscore': maxScore}
      }
    
    
    
      console.log(sumAscii(myArray))
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search