알고리즘 102 - Highest Scoring Word

박진현·2021년 7월 31일
0

알고리즘 (Javascript / C)

목록 보기
102/125

Q.

Description:
Given a string of words, you need to find the highest scoring word.

Each letter of a word scores points according to its position in the alphabet: a = 1, b = 2, c = 3 etc.

You need to return the highest scoring word as a string.

If two words score the same, return the word that appears earliest in the original string.

All letters will be lowercase and all inputs will be valid.

A)

function high(x){
  // 'a' 의 charCode는 97 z는 122
  let toArr = x.split(' ')
  let resArr = []
  let sum = 0;
  let toArr2 = toArr.map(el => el.split(''))
  for (let arr of toArr2) {
    for (let i = 0; i < arr.length; i++) {
      sum += arr[i].charCodeAt() - 96
    }
    resArr.push(sum)
    sum = 0;
  }
  return toArr[resArr.indexOf(Math.max(...resArr))]
}  
profile
👨🏻‍💻 호기심이 많고 에러를 좋아하는 프론트엔드 개발자 박진현 입니다.

0개의 댓글