Created
November 23, 2017 05:05
-
-
Save htkcodes/7ecdf11aff5a57349b4509c64657284e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*jshint esversion: 6 */ | |
/* | |
Create an algorithm to test if a note can be formed | |
from a magazine text | |
*/ | |
//Returns true or false if note can be formed from magazine text | |
function ransomeNote(noteText, magazineText) { | |
let magazineTextArr = magazineText.split(" "), noteTextArr = noteText.split(" "), b = {}; | |
magazineTextArr.forEach(function(word) { | |
b[word] || (b[word] = 0); | |
b[word]++; | |
}); | |
let isPossible = !0; | |
noteTextArr.forEach(function(word) { | |
b[word] ? (b[word]--, 0 > b[word] && (isPossible = !1)) : isPossible = !0; | |
}); | |
return isPossible; | |
} | |
console.log("Ransome note formation: "+ransomeNote("the France vision will share her new relationship", "The prime minister will urge her European counterparts to share her vision for building a successful new relationship between the UK and EU after Brexit, despite talks with the European commission negotiators having reached a stalemate after three rounds.")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment