Skip to content

happiness Number :) problem #39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions 14. happinessNumber/happinessNumber.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
PROBLEM
You will be given a string of characters containing only the following characters: "(", ")", ":"
Create a function that returns a number based on the number of sad and happy smiley faces there is.
* The happy faces :) and (: are worth 1.
* The sad faces :( and ): are worth -1.
* Invalid faces are worth 0.
EXPLANATION :
happinessNumber(":):(") ➞ -1
The first 2 characters are :) +1 Total: 1
The next 2 are ): -1 Total: 0
The last 2 are :( -1 Total: -1
*/

function happinessNumber(smilies) {
let arr = smilies.split('');
let count = 0;
for (var i = 0; i < arr.length; i++) {
if (arr[i] + arr[i + 1] == ':)') count += 1;

if (arr[i] + arr[i + 1] == '(:') count += 1;

if (arr[i] + arr[i + 1] == '):') count -= 1;

if (arr[i] + arr[i + 1] == ':(') count -= 1;
}
return count;
}

// console.log(happinessNumber(":):(")) // -1
// console.log(happinessNumber("(:)")) // 2
// console.log(happinessNumber("::::")) // 0
58 changes: 57 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ __Algorithms practiced using JS__
10. Caesar Cipher
11. Lexicographically equal or not
12. Fizz Buzz
14. happinessNumber

## Explanation
<b>1. String reversing</b>
Expand Down Expand Up @@ -1532,4 +1533,59 @@ const dist = dijkstra(graph, 6);
console.log(dist);

```
</p>
</p>



</p>
<hr>
<hr>

<b>14. happinessNumber</b>

__The Challenge:__

Hpiness Number Problem :)

__Algorithmic Thinking:__
``` text
PROBLEM
You will be given a string of characters containing only the following characters: "(", ")", ":"
Create a function that returns a number based on the number of sad and happy smiley faces there is.
* The happy faces :) and (: are worth 1.
* The sad faces :( and ): are worth -1.
* Invalid faces are worth 0.
EXPLANATION :
happinessNumber(":):(") ➞ -1
The first 2 characters are :) +1 Total: 1
The next 2 are ): -1 Total: 0
The last 2 are :( -1 Total: -1

```
__Implementation:__

```js

function happinessNumber(smilies) {
let arr = smilies.split('');
let count = 0;
for (var i = 0; i < arr.length; i++) {
if (arr[i] + arr[i + 1] == ':)') count += 1;

if (arr[i] + arr[i + 1] == '(:') count += 1;

if (arr[i] + arr[i + 1] == '):') count -= 1;

if (arr[i] + arr[i + 1] == ':(') count -= 1;
}
return count;
}

console.log(happinessNumber(":):(")) // -1
console.log(happinessNumber("(:)")) // 2
console.log(happinessNumber("::::")) // 0

```