rhondamuse.com

How to Determine the Highest Scoring Word Using JavaScript

Written on

Chapter 1: Understanding the Problem

In today's discussion, we delve into a straightforward coding challenge involving arrays, strings, and character encoding in JavaScript. This task also incorporates some basic math, particularly the summation of values. Let's begin by outlining the problem.

The Challenge: Finding the Word with the Highest Score

Given a string of words, your goal is to identify the word that scores the highest. Each letter corresponds to a score based on its position in the alphabet: for instance, 'a' is 1, 'b' is 2, and so forth.

For example, the word "abad" has a score of 8 (1 + 2 + 1 + 4). Your task is to return the highest-scoring word as a string. In cases where two words have the same score, return the one that appears first in the original string. All letters will be in lowercase, and the inputs will be valid.

Chapter 1.1: Decomposing the Problem

To tackle this challenge, we can break it down into smaller, more manageable tasks:

  1. Split the sentence into individual words.
  2. Calculate the score for each word.
  3. Identify the highest score.
  4. Return the first word that has the highest score.

To convert a sentence into an array of words, we can use the String.split() method, specifying a space as the separator.

const words = "hello world";

const listWords = words.split(" ");

console.log(listWords); // ["hello", "world"]

Chapter 1.2: Scoring Each Word

To compute the score of each word, there are a couple of methods available. One way is to create a dictionary that assigns values to the letters of the alphabet. However, a more efficient approach involves using the String.charCodeAt() method to obtain the ASCII code of each character. By summing these values, we can easily calculate the score for each word.

const word = "hello";

const value = [...word].reduce((a, c) => a + c.charCodeAt(0), 0);

console.log(value); // 532

However, the ASCII value for 'a' is 97, and for 'b' it is 98, which presents a challenge. To rectify this, we subtract 96 from each value to align the scores correctly: 'a' becomes 1, 'b' becomes 2, and so on.

const word = "hello";

const value = [...word].reduce((a, c) => a + (c.charCodeAt(0) - 96), 0);

console.log(value); // 52

Next, we need to find the maximum score from the computed values. We can utilize the Math.max() function to achieve this.

const values = [1, 2, 3, 4, 5];

const maxValue = Math.max(...values);

console.log(maxValue); // 5

However, we need the word associated with this maximum score, not just the score itself. To determine the corresponding word, we can use the Array.indexOf() method.

const values = [1, 2, 3, 4, 5];

const maxValue = Math.max(...values);

const indexMaxValue = values.indexOf(maxValue);

console.log(indexMaxValue); // 4

By combining these elements, we can formulate a complete solution.

const getValues = (w) =>

w

.split(" ")

.map((w) => [...w].reduce((a, c) => a + (c.charCodeAt(0) - 96), 0));

const indexMax = (x) => x.indexOf(Math.max(...x));

const high = (x) => {

const listWords = x.split(" ");

const listValue = getValues(x);

const maxValue = indexMax(listValue);

return listWords[maxValue];

};

Chapter 2: An Alternative Approach

There's an alternative method to solve the same problem. Instead of calculating the score for each word and then identifying the maximum value, we can simply sort the words based on their scores. The word with the highest score will naturally appear first in the sorted list.

To implement this, we can employ the Array.sort() method.

const value = (w) => [...w].reduce((a, c) => a + (c.charCodeAt(0) - 96), 0);

const high = (x) => {

const order = x.split(" ").sort((a, b) => value(b) - value(a));

return order[0];

};

We can streamline the solution further by converting everything into arrow functions, reducing the code to just two lines.

const value = (w) => [...w].reduce((a, c) => a + (c.charCodeAt(0) - 96), 0);

const high = (x) => x.split(" ").sort((a, b) => value(b) - value(a))[0];

For an even more compact solution, we can combine both lines into a single function.

const high = (x) =>

x

.split(" ")

.sort(

(a, b) =>

[...b].reduce((z, c) => z + (c.charCodeAt(0) - 96), 0) -

[...a].reduce((z, c) => z + (c.charCodeAt(0) - 96), 0)

)[0];

Thank you for reading! Stay tuned for further insights, and don’t miss out on my next article—subscribe to my Medium email list.

Share the page:

Twitter Facebook Reddit LinkIn

-----------------------

Recent Post:

Unlocking the Secrets to Earning $1,000 a Day

Explore key strategies and mindset shifts to help you achieve daily income goals in your writing and freelancing career.

Boosting Cancer Immunotherapy with Ultrasound-Guided Microbubbles

New research shows how ultrasound-guided microbubbles can enhance immunotherapy effectiveness against cancer.

A Humble Potato's Journey: From School Lunch to Gourmet Delight

Exploring the nostalgic journey of jacket potatoes from childhood to modern gourmet cuisine.