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?

3
  • Does this answer your question? How for...of Loop Statement Works in Javascript?(ECMAScript 6) Commented Dec 19, 2021 at 0: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' ) }) Commented Dec 19, 2021 at 0: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. Commented Dec 19, 2021 at 1:12

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Oh! I'm so used to Lua that I forgot this important step! Thank you!
1

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

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

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.