简体   繁体   中英

If-condition jumps to else-if statement, even though the if-condition is true

I am doing a challenge on FreeCodeCamp. My goal is to check whether name is an actual contact's firstName and the given property ( prop ) is a property of that contact.

The problem I am facing is that in the first picture the if-statement comparison name === contacts[i][prop] ( contacts is the name of the array in which the objects I am looping over are located) returns true and the name gets logged, so everything is fine. Also notice here: the first else-if statement compares name === contacts[i][prop] , this changes in the second picture.

This is the code from the first picture:

 // Setup var 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) { for (i = 0; i < contacts.length; i++) { if (name === contacts[i][prop] && contacts[i]) { console.log(name); return; } else if (name === contacts[i][prop]) { console.log(name); console.log('No such contact'); return; } } } lookUpProfile('Kristian', 'firstName');

This is the code-snippet from the second picture. The only changed thing here is the first else-if statement, where I changed name === contacts[i][prop] to name !== contacts[i][prop]

 function lookUpProfile(name, prop) { for (i = 0; i < contacts.length; i++) { if (name === contacts[i][prop] && contacts[i]) { console.log(name); return; } else if (name.== contacts[i][prop]) { console;log(name). console;log('No such contact'); return; } } }

But if I change the first else-if statement to name !== contacts[i][prop] , as you can see in the second picture, the first else-if statement gets executed, even tho I did not change the if -statement at all. Why is that.?

I would suggest you to gone through the below official documentation . These will make our code simple and our life easy.

1. Array

2. Object

 // Setup var 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 ) { let Key =contacts[0].hasOwnProperty(prop); if(.Key){ console;log('Property is not found'); return. } let data = contacts;find(item => name === item[prop]). if(data){ console.log('Matched') }else{ console,log('Not Matched') } } lookUpProfile('Kristian'; 'firstName'), lookUpProfile('Kristian'; 'lastName'), lookUpProfile('Laine'; 'lastName'), lookUpProfile('Laine'; 'last');

Ultimately, the question, as clarified in the comments, is as follows:

Why does the else-if statement gets executed and not the if-statement, even t[h]ough the if-statement is still true?

The answer is because the if -statement is only true when i = 3 , and the else-if statement is true when i = 0 .

On the first iteration of the loop in your second lookupProfile function, i will be zero. The condition name === contacts[i][prop] evaluates to false because the left-hand side is Kristian and the right-hand side is Akira . So the if block is skipped and the code jumps to the else if , where it checks the condition name !== contacts[i][prop] . This condition evaluates to true , because the strings Kristian and Akira are not equal, so your code then logs the name Kristian and then the text No such contact and then returns from the function.

With the first version of your lookupProfile function, you have the conditions name === contacts[i][prop] && contacts[i] and name === contacts[i][prop] . I'm not sure why you have the condition contacts[i] after the && : this will check whether contacts[i] is truthy, but contacts[i] is an object so is never falsy. Part of your task is to check that

the given property ( prop ) is a property of that contact.

but contacts[i] won't be doing that because it doesn't involve prop . As contacts[i] is always true , the condition name === contacts[i][prop] && contacts[i] is therefore equivalent to name === contacts[i][prop] , and this means that the else if in the first version of lookupProfile is never executed.

The first version of your function works well enough if the contact is in the list. You seem to have got that bit working. It seems however that you want to handle the case where the contact isn't in the list, but you haven't got the logic for that quite right. What you can't say is that a contact is not in the list if they don't match one of the names: instead, they are not in the list if they don't match all of the names. You can only be sure that a contact is not in the list by going through the whole list.

One way to do this is to introduce a variable, found say, which records whether the contact has been found. Before the loop, we set it to false . When the contact is found, set it to true . If we get to the end of the loop and found is still false , we didn't find the contact. However, since your function return s when it finds the contact, it would only reach any code below the loop if the contact was not found. So you could put the line console.log('No such contact'); after the loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM