Else vs Or statements

Tell us what’s happening:
Describe your issue in detail here.
When creating the (else) statement
" }else {
return “No such property” after returning “contacts[i][prop]”, my code fails all tests. However, if I return "contacts[i][prop] || “No such property”,
the code works. What is happening here? Why is the OR statement accepted and the ELSE statement isn’t? Is it a syntax issue?

  **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++) {
  if (contacts[i].firstName === name) {
   return contacts[i][prop];
  } else {        <----- This 'else' statement is returning failed tests. Why?
    return "No such property"
  }
}
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/105.0.0.0 Safari/537.36

Challenge: Basic JavaScript - Profile Lookup

Link to the challenge:

In words, your code says “if the firstName property of contacts[i] matches the name, then return contacts[i][prop], otherwise return "No such property"”.

Flipping that logic around, you are saying “if the firstName property of contacts[i] does not match the name, then return "No such property", otherwise return contacts[i][prop]”.

I’m a bit confused. If the first condition is met, the code stops running. If not, it moves on to the next condition. So shouldn’t the “else” statement suffice if the first condition isn’t met. Even, with the logic switched the “else” statement should hold true because the first condition wasn’t met.

Either the if condition will be met or it will be not, so you will always return on the first iteration.

Nice code. The issue is with the else statement’s position. The if statement will run for the first element in the array and then it’ll pass to the else which means it’ll return ‘No such property’ before you even test the other elements in the contacts array.

This:

if (contacts[i].firstName === name) {
  return contacts[i][prop] || "No such property";
}

Is the same as this:

if (contacts[i].firstName === name) {
  if (contacts[i][prop]) {
    return contacts[i][prop];
  } else {
    return "No such property"
  }
}
contacts[i][prop] || "No such property"

If the left-hand side of || is a truthy value it will be used, otherwise the right-hand side.

console.log(true || 'Some Value'); // true
console.log(false || 'Some Value'); // 'Some Value'

That makes sense. So if the function runs in a loop, the first statement must be applied to all elements before applying the second?

Yes, lasjorg’s code snippet above shows it exactly. Your if statement is basically looking for an element in contacts where firstName === name, but if it doesn’t find it the else is triggered. So, if the first element doesn’t meet the criteria it goes straight to the else. In stead you could nest an if/else conditional inside that first if. That way your running those extra tests only when an element is found that meets the criteria.

Hope that makes sense.

Thank you for the code-breakdown. This makes sense now.

I get it now. Thank you for the explanation.

1 Like

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