2

I am trying to make a simple website(without CSS3) to search for items in an array. My way of accomplishing this goal is to search in the 'title' or 'desc' properties of an item in the array. My expected result is to get titleOfItem + ' fizz' in the console if the title includes the keyword from the input. Instead, I get the following error: console image & error

Here is my HTML5 code:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>replit</title>
    <link href="style.css" rel="stylesheet" type="text/css" />
  </head>
  <body>
    <input id="keywordText" type="text">
    <button id="submit" onclick="search()">Search</button>
    <script src="script.js"></script>
  </body>
</html>

and Here is my JS code:

const items = {
  john:{title:'john', desc:"doe", elem:document.getElementById('john')},
  jane:{title:'jane', desc:"doe", elem:document.getElementById('jane')}
}

let allItems = []
for (var key in items) {
  allItems.push(items[key])
}



function search() {
  let keyword = document.getElementById('keywordText').value;
  for (let count = 0; allItems.length; count++) {
    let titleOfItem = allItems[count].title
    if (titleOfItem.includes(keyword)) {
      console.log(titleOfItem + ' fizz')
    } else {
      console.log(titleOfItem + ' buzz')
    }
  }
}

Is there something that I am doing wrong in this code? Also, for organization purposes, is there some way to get this information straight from the first array?

khairul alam
  • 138
  • 13
  • Does this answer your question? [How for...of Loop Statement Works in Javascript?(ECMAScript 6)](https://stackoverflow.com/questions/31091263/how-for-of-loop-statement-works-in-javascriptecmascript-6) – Heartbit Dec 19 '21 at 00:52
  • That's the reason you should use `for..Each`, `for..of` so that you don't have to care about unimportant things like the `count < allItems.length`, just write the logic and done. You can also do this using forEach as `allItems.forEach(({ title }) => { if(title.includes(keyword)) console.log( title + ' fizz' ) else console.log( title + ' buzz' ) })` – DecPK Dec 19 '21 at 00:53
  • You've marked an answer below as accepted. This indicates that the problem has been solved. You don't also need to also add that information into your question body. – Henry Ecker Dec 19 '21 at 01:12

3 Answers3

1
for (let count = 0; count < allItems.length; count++) {

}

You are getting the error because you did not specify how long the count value will continue in the for loop. You can fix the problem by updating the code like this.

1

You probably missed the condition in the for loop. Change your code to this:

for (let count = 0; count < allItems.length; count++)

Muhammed Jaseem
  • 782
  • 6
  • 18
0

The reason is because title is undefined. That could be because title was not copied correctly in your code or it is because you traversed the array to outOfBounds where title is not accessible and is undefined.

Fix this part:

for (let count = 0; count < allItems.length; count++) {
    let titleOfItem = allItems[count].title
    if (titleOfItem.includes(keyword)) {
      console.log(titleOfItem + ' fizz')
    } else {
      console.log(titleOfItem + ' buzz')
    }
  }

That should do it but if not check the value of allItems to see if it has properties all the time

Rhett Harrison
  • 430
  • 4
  • 19