Skip to content

Instantly share code, notes, and snippets.

@htkcodes
Created November 23, 2017 05:05
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 htkcodes/7ecdf11aff5a57349b4509c64657284e to your computer and use it in GitHub Desktop.
Save htkcodes/7ecdf11aff5a57349b4509c64657284e to your computer and use it in GitHub Desktop.
/*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