Skip to content

Instantly share code, notes, and snippets.

@darkcris1
Created January 3, 2021 11:17
Show Gist options
  • Save darkcris1/c69ace313bcad8a7fc3e98b0df1fc287 to your computer and use it in GitHub Desktop.
Save darkcris1/c69ace313bcad8a7fc3e98b0df1fc287 to your computer and use it in GitHub Desktop.
Scrabble points solution | Codewars
function scrabblePoints(str) {
const strs = str.split(' ')
let letterPoints = 0
const totalPoints = strs.reduce(
(acc, value) => {
if (value.length > 10 || !value) return acc
console.log(value)
value
.trim()
.toLowerCase()
.split('')
.forEach((val) => {
if (/[qz]/.test(val)) {
letterPoints += 10
} else if (/[dg]/.test(val)) {
letterPoints += 2
} else if (/[bcmp]/.test(val)) {
letterPoints += 3
} else if (/[fhvwy]/.test(val)) {
letterPoints += 4
} else if (/[k]/.test(val)) {
letterPoints += 5
} else if (/[jx]/.test(val)) {
letterPoints += 8
} else {
letterPoints += 1
}
})
if (letterPoints > acc[1]) {
acc = [value + ' with ', letterPoints, ' points is the highest']
}
letterPoints = 0
return acc
},
['Not Applicable', 0],
)
return totalPoints.join('')
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment