Basic JavaScript - Profile Lookup

Tell us what’s happening:
Describe your issue in detail here.

One thing I really can’t understand about these functions is how does Javascript know to communicate [i] with the contacts list, I’m not sure I’m explaining my confusion. I can’t see how it connects to the list. I know i is arbitrary and declared as a variable starting at 0 and that it checks the contacts length and then does i++ if it hasn’t reached the end so then the i variable will = 1 then 2 etc but what actually tells Javascript that i++ means to iterate through the list?
I can’t complete any of these problems by myself because I don’t understand or know the syntax well enough and I don’t understand how some of these things actually work or function. I went through the course twice up until this point to check if I missed anything but some sections I understand and then one comes along that is totally out of my understanding.

  **Your code so far**
// Setup
const contacts = [
  firstName: "Akira",
  lastName: "Laine",
  number: "0543236543",
  likes: ["Pizza", "Coding", "Brownie Points"],
  firstName: "Harry",
  lastName: "Potter",
  number: "0994372684",
  likes: ["Hogwarts", "Magic", "Hagrid"],
  firstName: "Sherlock",
  lastName: "Holmes",
  number: "0487345643",
  likes: ["Intriguing Cases", "Violin"],
  firstName: "Kristian",
  lastName: "Vos",
  number: "unknown",
  likes: ["JavaScript", "Gaming", "Foxes"],

function lookUpProfile(name, prop) {
// Only change code below this line
for (var i = 0; i < contacts.length; i++) {
if(contacts[i].firstName === name) {
return contacts[i][prop] || "No Such Property";
return "No Such Contact"
// Only change code above this line

var data = lookUpProfile("Kristian", "likes");

  **Your browser information:**

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/ Safari/537.36

Challenge: Basic JavaScript - Profile Lookup

Link to the challenge:

you say that

an array is accessed using bracket notation and index number, so if you have arr = ['apple', 'blue', 'November'], arr[0] will be 'apple', arr[1] will be 'blue' and 'November' for arr[2]. The index goes from 0 to arr.length - 1.

A for loop written like this will iterate from 0 to arr.length - 1. Those are just numbers, JavaScript knows only that i is 0 at the start, and at each iteration increases by 1

You are telling it to use i to access the array when you write contacts[i]
when i equals 0 that eill be contacts[0], when i equals 1 that will be contacts[1] and so on

1 Like

Thank you Ilenia,
Makes perfect sense when you explain it so well.
I wish I could find more exercises/problems around the same concepts.
The course progresses a little quick from one exercise to the next. I’d need to do this kind of problem many times for the concepts to really sink in. Thanks for helping out!

you could try solving katas on codewars, maybe 8th or 7th kyu, so you get the level of challenge you want

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.