From e86058946b8edbe5d3f67f654298c5f427e33ede Mon Sep 17 00:00:00 2001 From: Chris Zuber Date: Fri, 14 Jul 2017 09:59:15 -0700 Subject: [PATCH] Simplify cliche detection - Use `Array.prototype.some` and `String.prototype.includes` to detect cliches. - Use `Array.prototype.forEach` instead if incrementing a counter. --- chapter13/cliche.html | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/chapter13/cliche.html b/chapter13/cliche.html index b826f17..8c1ae92 100644 --- a/chapter13/cliche.html +++ b/chapter13/cliche.html @@ -8,27 +8,18 @@ String.prototype.cliche= function() { var cliche = ["lock and load","touch base", "open the kimono"]; - for (var i = 0; i < cliche.length; i++) { - var index = this.indexOf(cliche[i]); - if (index >= 0) { - return true; - } - } - return false; + return cliche.some(phrase => this.includes(phrase)); }; var sentences = ["I'll send my car around to pick you up.", - "Let's touch base in the morning and see where we are", + "Let's touch base in the morning and see where we are", "We don't want to open the kimono, we just want to inform them."]; -for (var i = 0; i < sentences.length; i++) { - var phrase = sentences[i]; - if (phrase.cliche()) { - console.log("CLICHE ALERT: " + phrase); +sentences.forEach(sentence => { + if (sentence.cliche()) { + console.log(`CLICHE ALERT: ${sentence}`); } -} - - +});