Skip to content

Commit e860589

Browse files
committed
Simplify cliche detection
- Use `Array.prototype.some` and `String.prototype.includes` to detect cliches. - Use `Array.prototype.forEach` instead if incrementing a counter.
1 parent 09e494c commit e860589

File tree

1 file changed

+6
-15
lines changed

1 file changed

+6
-15
lines changed

chapter13/cliche.html

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,18 @@
88
String.prototype.cliche= function() {
99
var cliche = ["lock and load","touch base", "open the kimono"];
1010

11-
for (var i = 0; i < cliche.length; i++) {
12-
var index = this.indexOf(cliche[i]);
13-
if (index >= 0) {
14-
return true;
15-
}
16-
}
17-
return false;
11+
return cliche.some(phrase => this.includes(phrase));
1812
};
1913

2014
var sentences = ["I'll send my car around to pick you up.",
21-
"Let's touch base in the morning and see where we are",
15+
"Let's touch base in the morning and see where we are",
2216
"We don't want to open the kimono, we just want to inform them."];
2317

24-
for (var i = 0; i < sentences.length; i++) {
25-
var phrase = sentences[i];
26-
if (phrase.cliche()) {
27-
console.log("CLICHE ALERT: " + phrase);
18+
sentences.forEach(sentence => {
19+
if (sentence.cliche()) {
20+
console.log(`CLICHE ALERT: ${sentence}`);
2821
}
29-
}
30-
31-
22+
});
3223
</script>
3324
</head>
3425
<body>

0 commit comments

Comments
 (0)