JavaScript JavaScript Loops, Arrays and Objects Tracking Multiple Items with Arrays Useful Array Methods

How to translate indexOf in pseudo code?

I am unable to understand how Dave uses indexOf(), in pseudo code how can we write it or better how to understand it while writing?

Kenichi Beveridge
Kenichi Beveridge
8,734 Points

Hi,

I think I understand you question.

According to W3 Schools definition "The indexOf() method returns the position of the first occurrence of a specified value in a string." So essentially the method takes what you have entered into the search variable and compares it with what is in the inStock array and return the first instance it appears.

Hope this helps.

Justin Blalock
Justin Blalock
8,206 Points

Kenichi is correct. The key to remember is that indexOf() is a method of an already defined array (list) used to search for a specific item (element value / list item) and print out or return that item's index number / place on list .

Psuedo Code could look something like:

Create list (insert array declaration here) Ask user to enter an item ( prompt()) Check list for user entered item ( search list items (indexOf()) ....(more code if needed)