Why is the if-else branch not firing correctly?
-
I can't understand why else in this code is triggered first, and then if?
pumpJavaScript Anonymous, Apr 30, 2020 -
Because you compare the entered name with each of the array and at each iteration if the name does not match, print a message.Anonymous
-
Well, for example, I'm Brad Pitt. I enter this means: 'Pitt'. The following happens in the code:
- 'Pitt' === 'Kutcher'?
- No. alert ('No results found for your request')
- 'Pitt' === 'Pitt'?
- Yes. alert ('with a bunch of uninteresting information')Ruby Jefferson -
const data = [
{
firstName: 'Ashton',
lastName: 'Kutcher',
age: 40
},
{
firstName: 'Bredley',
lastName: 'Pitt',
age: 54
},
{
firstName: 'Hannah',
lastName: 'Dakota',
age: 24
}
];
let pr = prompt('Пользователь, введи свою фамилию!');
let result = pr.slice(0, 1).toUpperCase() + pr.slice(1).toLowerCase();
const user = data.find(user => result === user.lastName)
alert(user != null ? `User name: ${user.firstName} ${user.lastName}`:`No results found for your request`)Myles Woods
4 Answers
Your Answer
To place the code, please use CodePen or similar tool. Thanks you!