Tell us what’s happening:
Your code so far
// 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){
// Only change code below this line
// Find Object indexes
var len = (Object.keys(contacts).length - 1);
var index = len;
// rewrite into a recurssion
const findIndex = (index,name) => {
if (index <= 0 ) {
return index = 0;
}
else {
return (contacts[index]['firstName'] !== name) ? findIndex(index - 1, name) : index;
}
}
//Call to find index of object in question
const id = findIndex(index, name);
// Test for prop validation
const isValidProp = () => {
return contacts[id].hasOwnProperty(prop) ? true : false;
}
// Test for name validation
const isValidName = () => {
return (contacts[id]['firstName'] === name) ? true : false;
}
// Display Object after validation
const showObject = (id) => {
return (isValidName() && isValidProp()) && console.log(contacts[id][prop]);
}
// Switch
switch(true){
case (isValidProp() && isValidName()):
return console.log(contacts[id][prop]);
break;
case (!isValidProp() && !isValidName()):
console.log(`"No such property", "No such contact"`);
break;
case (!isValidProp()):
console.log("No such property");
break;
case (!isValidName()):
console.log("No such contact")
break;
default:
console.log("There is an error");
break;
}
// Only change code above this line
}
lookUpProfile("Akira", "address"
);
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.67 Safari/537.36
.
Challenge: Profile Lookup
Link to the challenge: