Edabit

You will be given a string of characters containing only the following characters: ():

Create a function returns a number based on the number of sad and smiley faces there is.

  • The happy faces :) and (: are worth 1.
  • The sad faces :( and ): are worth -1.

Working Example

 happinessNumber(":):(") ➞ -1 // The first 2 characters are 🙂        +1      Total: 1 // The next 2 are ):                    -1      Total: 0 // The last 2 are 😦                    -1       Total: -1 

Examples

 happinessNumber(":):(") ➞ -1  happinessNumber("(:)") ➞ 2  happinessNumber("::::") ➞ 0 

Notes

All test cases will be valid.

Solution:

 1  2  3  4  5  6  7  8  9 10 11 12 13 14
function happinessNumber(s) {   let score = {     ":)": 0,     "(:": 0,     ":(": 0,     "):": 0,   };   for (let i = 0; i < s.length - 1; i++) {     score[s[i] + s[i + 1]] += 1;   }   //   console.log(score[":)"] + score["(:"]);   //   console.log(score[":("] + score["):"]);   return score[":)"] + score["(:"] - (score[":("] + score["):"]); } 

This free site is ad-supported. Learn more