Basic JavaScript - Profile Lookup by using switch

I knew the recommended anwser using “hasOwnProperty” to solve this question; however,
I think my way using switch is workable too. But I can’t pass this task.

I though it can pass all the situation in this question, but it didn’t work. :S
Can someone help to look up where’s the bug in my codes? Thank you so much.

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 (let i = 0; i < contacts[i]; i++ ) {
if (contacts[i].firstName === name) {
  switch (prop) {
    case "lastName":
    return [contacts.firstName, contacts.lastName]
    break;
    case "number":
    return [contacts.firstName, contacts.number]
    break;
    case "likes":
    return [contacts.firstName, contacts.likes]
    break;
    default:
    return "No such property";}
}
else {
  return "No such contact";
}
  }

  // Only change code above this line
}
console.log(lookUpProfile(name, prop))
lookUpProfile("Akira", "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/113.0.0.0 Safari/537.36

Challenge: Basic JavaScript - Profile Lookup

Link to the challenge:

for (let i = 0; i < contacts[i]; i++ )
if // true
  return "No such property";
else // false
  return "No such contact";

I saw 2 issues to be firstly solved:

  1. your “for-loop” not iterating properly, i believe you looking for array.length to get the size.
  2. your “for-loop” so far only check 1 item in the array, RETURN statement immediatel stops your function and do not continue the loop. Please be careful on the break; usage.
  • In a switch (), it stops other condition checking.
  • In a for-loop, it stops and jumps out the loop.
    Then, you need rethink how to check item inside an array without early stopping.

Happy coding.

1 Like

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