Skip to content

Instantly share code, notes, and snippets.

@McLarenCollege
Last active August 19, 2021 10:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save McLarenCollege/d0679eea43d3255a8254b71bf7173b8f to your computer and use it in GitHub Desktop.
Save McLarenCollege/d0679eea43d3255a8254b71bf7173b8f to your computer and use it in GitHub Desktop.
Value Of Sentence

Each letter in a sentence is worth its position in the alphabet (i.e. a = 1, b = 2, c = 3, etc...). However, if a word is all in UPPERCASE, the value of that word is doubled.

Create a function which returns the value of a sentence.

HINT: you can create a variable like let alphabets = "abcdefghijklmnopqrstuvwxyz" and use alphabets.indexOf(char)+1 to calculate the value of a character.

Examples:

getSentenceValue("abc ABC Abc") ➞ 24 // a = 1, b = 2, c = 3

// abc = 1 + 2 + 3 = 6 // ABC = (1+2+3) * 2 = 12 (ALL letters are in uppercase) // Abc = 1 + 2 + 3 = 6 (NOT ALL letters are in uppercase)

// 6 + 12 + 6 = 24

function getSentenceValue(sentence){
let alphabets ="abcdefghijklmnopqrstuvwxyz";
// write your code here
}
console.log(getSentenceValue("aaa aaa")); // 6
console.log(getSentenceValue("aaa AAA")); // 9
console.log(getSentenceValue("aaa Aaa")); // 6
console.log(getSentenceValue("HELLO world")); // 176
console.log(getSentenceValue("Javascript is LEGENDARY"));//329
console.log(getSentenceValue("Her seaside sea-shelling business is really booming!")); //488

Notes:

Ignore spaces and punctuation.

Remember that the value of a word isn't doubled unless all the letters in it are uppercase.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment