-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathharmlessransomnote.js
More file actions
29 lines (26 loc) · 1.44 KB
/
harmlessransomnote.js
File metadata and controls
29 lines (26 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
function harmlessRansomNote (noteText, magazineText) {
var noteArray = noteText.split(' ');
var magazineArray = magazineText.split(' ');
// Count the number of each word
var magazineObj = {};
magazineArray.forEach(word => { // Arrow function
if(!magazineObj[word]) {
magazineObj[word] = 0;
}
magazineObj[word]++;
});
// Check if the words of note exists in magazine
var noteIsPossible = true;
noteArray.forEach(word => {
if (magazineObj[word]){
magazineObj[word]--;
if (magazineObj[word] <0 ){
noteIsPossible = false;
}
}
else
noteIsPossible = false;
});
console.log (noteIsPossible);
}
harmlessRansomNote('this is a secret note for you from a secret admirer', 'puerto rico is a place of great wonder and excitement it has many secret waterfall locations that i am an admirer of you must hike quite a distance to find the secret places as they are far from populated areas but it is worth the effort a tip i have for you is to go early in the morning when it is not so hot out also note that you must wear hiking boots this is one of the best places i have ever visited');