Basic JavaScript - Profile Lookup

Tell us what’s happening:
Describe your issue in detail here.
I am trying to use a switch statement to achieve the same with an if/else statement. Where am I going wrong. Its not working. It keeps on defaulting to No such contact when I run the code and if the return is left empty is states undefined.

  **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.length; i++) {
  switch (contacts[i]) {
    case contacts[i].firstName === name: 
    case contacts[i].hasOwnProperty(prop):
      return contacts[i][prop];
      break;
    case contacts[i].hasOwnProperty(name):
    case contacts[i].hasOwnProperty(prop) === false:
      return "No such property";
      break;
  }
return " No Such Contact"; 
}

// Only change code above this line
}

lookUpProfile("Akira", "likes");
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36

Challenge: Basic JavaScript - Profile Lookup

Link to the challenge:

This means contacts[i] === (contacts[i].firstName === name)
You may want to review how a switch statement works

Also, this

this is equivalent to

const case1 = contacts[i].firstName === name;
const case2 = constacts[i].hasOwnProperty(prop);
const case3 = contacts[i].hasOwnProperty(name),
const case4 = contacts[i].hasOwnProperty(prop) === false;

if (contacts[i] === case1 || contacts[i] === case2) {
    return contacts[i][prop];

} else if (contacts[i] === case3 || contacts[i] === case4) {
    return "No such property";

}

Your understanding of switch statement is not right. → switch - JavaScript | MDN

Switch executes statements under a case when it matches its expression, NOT when it evaluates to true

switch (expression) {
   case match1:
       statements
       break;
   case match2:
       statements
       break;
}
let expression = 'kel3'
switch (expression ) {
   case '3lek':
       // this won't execute
       console.log('expression '.split("").reverse().join(""))
       break;
   case 'kel3':
       // this part executes
       console.log(expression)
       break;
    default:
         // this would execute if expression is neither 'kel3' or '3lek'
         console.log('None of the cases matches the expression')
}

after evaluation, your switch statement would look like this:

let i = 0
let name = 'Akira'
let prop = 'likes'
// contacts[0] includes Akira's details
switch ("Akira's object") {
    // case contacts[i].firstName === name: 
    // case 'Akira' === 'Akira': 
    case true:
    // case contacts[i].hasOwnProperty(prop):
    // case contacts[i].hasOwnProperty('likes'):
    case true:
      return contacts[i][prop];
      break;
    // so you're comparing an object with a boolean like
    // {} === true => false
    // if you had default branch set up, it would always run that
    case contacts[i].hasOwnProperty(name):
    case contacts[i].hasOwnProperty(prop) === false:
      return "No such property";
      break;
  }

well and because your switch statement never returns anything, and you didn’t declare any other return within the function, it returns undefined every single time

function veryPoorFunction() {}
// try run this, you will see that you get `undefined`
console.log(veryPoorFunction())

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