# Word in array counter

**URL:** <https://discourse.processing.org/t/word-in-array-counter/37238>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [May 30, 2022, 8:51pm UTC](https://discourse.processing.org/t/word-in-array-counter/37238 "2022-05-30T20:51:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![luciaromancanivell](https://avatars.discourse-cdn.com/v4/letter/l/8edcca/32.png) [@luciaromancanivell](https://discourse.processing.org/u/luciaromancanivell)\
**Post date:** [May 30, 2022, 8:51pm UTC](https://discourse.processing.org/t/word-in-array-counter/37238/1 "2022-05-30T20:51:25Z")

</div>

> please format code with \</\> button \* [homework policy](https://discourse.processing.org/faq/#homework) \* [asking questions](https://discourse.processing.org/t/2147)

Hello, I am getting a bit confused here… I need to count how many times the word imagine shows up in the array but I do not know how to create the counter so it works and is printed… thank you! Any guidance is very welcomed.

```auto
var parrafo = "Imagine there's no heaven | It's easy if you try | No hell below us | Above us only sky | Imagine all the people | Living for today | Imagine there's no countries | It isn't hard to do | Nothing to kill or die for | And no religion too | Imagine all the people | Living life in peace";
var parrafo_array;
var contador;

function setup() {
  createCanvas(1024, 768);
  background("white");
  fill(50);
  textSize(20);
  contador = 0;

 for (var i=0; i< 61; i++) {
   if (parrafo [i] = true) {
   contador = contador + 1;
   } if else {
   contador = contador;
   }
 text(contador, 10, 10); 
}

```

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [May 30, 2022, 9:10pm UTC](https://discourse.processing.org/t/word-in-array-counter/37238/2 "2022-05-30T21:10:41Z")

</div>

> [@luciaromancanivell](#):
>
> `if (parrafo [i] = true) {`

You’re using the [assignment operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Assignment) `=` instead of the [equality operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Equality) `==`:

Also you still need to **[splitTokens()](https://p5js.org/reference/#/p5/splitTokens)** your lyrics string so it becomes an array of words.

Only then you can count each word inside that array which matches string “imagine”.

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [May 30, 2022, 9:19pm UTC](https://discourse.processing.org/t/word-in-array-counter/37238/3 "2022-05-30T21:19:11Z")

</div>

Hi

[![](https://img.youtube.com/vi/unm0BLor8aE/hqdefault.jpg "Coding Challenge #40.1: Word Counter in JavaScript") ](https://www.youtube.com/watch?v=unm0BLor8aE&t=840s)

> **[p5.js Web Editor](https://editor.p5js.org/bestesaylar/sketches/osd9z9PoK)**
>
> A web editor for p5.js, a JavaScript library with the goal of making coding accessible to artists, designers, educators, and beginners.

---

<div class="post-metadata">

**Author:** ![mnse](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mnse/32/16520_2.png) [@mnse](https://discourse.processing.org/u/mnse)\
**Post date:** [May 31, 2022, 12:41pm UTC](https://discourse.processing.org/t/word-in-array-counter/37238/4 "2022-05-31T12:41:52Z")

</div>

Why not just using RegEx and do something like (just as example):

```auto
const wordCounter = (myString,mySearchString) => ((myString).match(new RegExp('\\b'+mySearchString+'\\b',"g")) || []).length;

let pinfo;
let itext;
let isearch;

function setup() {
  noCanvas();
  createElement("label").html("Search String: ");    
  isearch = createInput("Imagine");
  createElement("br");
  createElement("label").html("Text To Search: ");
  itext = createInput("Imagine there's no heaven | It's easy if you try | No hell below us | Above us, only sky | Imagine all the people | Livin' for today | Ah | Imagine there's no countries | It isn't hard to do | Nothing to kill or die for | And no religion, too | Imagine all the people | Livin' life in peace | You | You may say I'm a dreamer | But I'm not the only one | I hope someday you'll join us | And the world will be as one | Imagine no possessions | I wonder if you can | No need for greed or hunger | A brotherhood of man | Imagine all the people | Sharing all the world | You | You may say I'm a dreamer | But I'm not the only one | I hope someday you'll join us | And the world will live as one");  
  itext.input(function() {
      pinfo.html("The word " + isearch.value() + " was found " + wordCounter(this.value(),isearch.value()) + " time(s)");  
  });  
  pinfo = createP();  
}

```

![dummy2](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/c/0/c0be9162a20ae21fd42edc1a4eee6cba41aa57ea.png)

Cheers  
— mnse
