repl.it
@mcuringa/

DreamWordCount

ES6

Traversing an array to count occurrences of a word.

fork
loading
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
let text = `I say to you today, my friends, so even
though we face the difficulties of today and tomorrow,
I still have a dream. It is a dream deeply rooted in
the American dream. I have a dream that one day this
nation will rise up and live out the true meaning of
its creed: “We hold these truths to be self-evident:
that all men are created equal.” I have a dream that
one day on the red hills of Georgia the sons of former
and the sons of former slave owners will be able to
sit down together at the table of brotherhood.`;

let words = text.split(/\s/);

let counter = 0;
for (let i = 0; i < words.length; i++) {
  if (words[i].includes("dream")) {
    counter++;
  }
}

console.log(`The speech has ${words.length} words.
We found "dream" ${counter} times.`);
Babel Compiler v6.4.4 Copyright (c) 2014-2015 Sebastian McKenzie
?