Skip to content

Instantly share code, notes, and snippets.

@McLarenCollege
Last active August 18, 2021 10:20
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/0a90df27eb45b46e3e99f027274379b5 to your computer and use it in GitHub Desktop.
Save McLarenCollege/0a90df27eb45b46e3e99f027274379b5 to your computer and use it in GitHub Desktop.
Balanced Word

We can assign a value to each character in a word, based on their position in the alphabet (a = 1, b = 2, ... , z = 26). A balanced word is one where the sum of values on the left-hand side of the word equals the sum of values on the right-hand side. For odd length words, the middle character (balance point) is ignored.

Write a function that returns true if the word is balanced, and false if it's not.

HINT : you are given a string str = "abcdefghijklmnopqrstuvwxyz"

Examples:

balanced("zips") ➞ true

// "zips" = "zi|ps" = 26+9|16+19 = 35|35 = true

balanced("brake") ➞ false

// "brake" = "br|ke" = 2+18|11+5 = 20|16 = false

function balanced(word){
let str = "abcdefghijklmnopqrstuvwxyz";
// write your code here
}
console.log(balanced("a");//true
console.log(balanced("aa");//true
console.log(balanced("zips"));// true
console.log(balanced("brake"));//false
console.log(balanced("azby"));//true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment