error in a search function in javascript - javascript

I am trying to return contact info for "steve", but codecademy is telling me that it is not returning correctly.
It looks like your search function doesn't return contact information for Steve.
I am not able to find any error in this code. Can you help me with finding any syntax or logical errors in the search function that you can see?
var friends = {
bill: {
firstName: "bill",
lastName: "gates",
number :21415,
address: ["mcklsn", "wcnskln"]
},
Steve: {
firstName: "bill",
lastName: "gates",
number: 21415,
address: ["mcklsn", "wcnskln"]
}
};
var list = function (friends)
{
for (bill in friends)
console.log(bill);
for (steve in friends)
console.log(steve);
};
var search = function(name)
{
for (var i in friends)
{
if (friends[i].firstName === name)
{
console.log(friends[i]);
return friends[i];
}
else
console.log("contact doesn't exist!");
}
};

You should really pay attention to what you are writing, don't Ctrl-C Ctrl-V everything blindly. You are not calling your friends in the correct names and I would be offended if someone did that to me. Heck, Steve Jobs would be furious if you called him Bill!
This should work:
var friends = {
Bill: {
firstName: "Bill",
lastName: "Gates",
number: 21415,
address: ["mcklsn", "wcnskln"]
},
Steve: {
firstName: "Steve",
lastName: "Jobs",
number: 21416,
address: ["mcklsnn", "wcnsklnn"]
}
};
search("Bill");
search("Steve");

Yah, because your Steve property object's firstName is set to "bill".
So that code, searching for a match to 'steve' on the firstName property will not find one...because there isn't one. Both of those object's have 'bill' as their firstName property.

Related

Need my Object Code to return a string and convert object into a string

My code works and outputs "This is Bob Martin from USA."
However, I want my code to: 1.) Have function printBio(user) return a string. The return value from the function should be a string. And 2.) Should convert object into a string with the structure: "This is NAME SURNAME from COUNTRY." function should convert the user object into a properly formatted string and then return that string
function printBio (user) {
user = {
name: 'Bob',
surname: 'Martin',
age: 25,
address: {
country: "USA"
}
}
console.log(`This is ${user.name} ${user.surname} from ${user.address.country}.`);
return user;
}
printBio();
I don't understand the problem as you already did make a template, but this is your code rearanged.
const user = {
name: 'Bob',
surname: 'Martin',
age: 25,
address: {
country: "USA"
}
}
function printBio(user) {
return `This is ${user.name} ${user.surname} from ${user.address.country}.`
}
const bio = printBio(user);
console.log(bio)

Algolia: search for multiples values in each fields with AND clause

I would like know if i can query different values in each field
returning values that only match with the search like AND condition. I tried use search in multpile indexes, but it return values like OR condition.
Example:
my index contains:
[
{
firstName: 'Alisson',
lastName: 'Oliveira',
},
{
firstName: 'Alex',
lastName: 'Oliver',
},
{
firstName: 'Daniel',
lastName: 'Costa',
}
]
my query would be:
index.search(query: { firstName: 'Al', lastName: 'Oliv' })
the response espected:
[
{
firstName: 'Alisson',
lastName: 'Oliveira',
},
{
firstName: 'Alex',
lastName: 'Oliver',
}
]
Someone know if is possible in the algolia?
Based on your example it sounds like your end-user would type the query: "Al Oliv" and you would expect the two records shown above to return.
To do this, you would change the default settings from prefixLast to prefixAll. You can do this in the Dashboard:
By default Algolia only uses the last word as a prefix. If you make the change above the query will return the expected results:

Iterating through object properties/keys

I'm just starting to learn coding, and i came across this question that i could not understand.
"The second function we'll add will be called search, and it will take a first name as an argument. It will try to match the first name it receives to any of the first names in our friends contact list. If it finds a match, it will log our friend's contact information (firstName, lastName, number, address) to the console."
variables are define as follows :
var friends = {};
friends.bill = {
firstName: "Bill",
lastName: "gates",
number: "1234567",
address: ['bishan','starbucks', 'centertable']
};
friends.steve = {
firstName: "Steve",
lastName: "jobs",
number: "987654",
address: ['orchird', 'ikoma', 'ga']
};
the answer is as follows :
var search = function(name) {
for(var key in friends) {
if(friends[key].firstName === name) {
console.log(friends[key]);
return friends[key];
}
}
};
could someone better explain how did the var "key" came about ? and why can't i just input friends.firstName === name, console.log(friends.name), return friends.name ??
would appreciate if someone could explain thank you.
From OP's comment:
var friends = {};
friends.bill = {
firstName: "Bill",
lastName: "gates",
number: "1234567",
address: ['bishan','starbucks', 'centertable']
};
friends.steve = {
firstName: "Steve",
lastName: "jobs",
number: "987654",
address: ['orchird', 'ikoma', 'ga']
};
friends is a nested object which can also be represented like so:
friends = {
bill: {
firstName: "Bill",
lastName: "gates",
number: "1234567",
address: ['bishan','starbucks', 'centertable']
},
steve: {
firstName: "Steve",
lastName: "jobs",
number: "987654",
address: ['orchird', 'ikoma', 'ga']
}
}
The for..in loop iterates over all keys in the friends object, with the variable key in your case.
why can't i just input friends.firstName === name, console.log(friends.name), return friends.name ??
Because, to do that, you need to have firstName or name as a property in friends. Since those properties are nested inside (name is not event inside the nested objects), there was a for..in loop used.
You have an object friends that has 2 properties bill and steve (those are the keys). Calling friends.bill will return you an object (the value) with firstname, lastname, number, address. You need to iterate all the properties of your object friends to find the one you need
You can use Object.values(obj)
var firstNameInput = "Steve";
var friends = {};
friends.bill = {
firstName: "Bill",
lastName: "gates",
number: "1234567",
address: ['bishan','starbucks', 'centertable']
};
friends.steve = {
firstName: "Steve",
lastName: "jobs",
number: "987654",
address: ['orchird', 'ikoma', 'ga']
};
//Iterates all the friends
Object.values(friends).forEach(function(f){
//Compare the property "firstname" with the input
if(f.firstName === firstNameInput){
//Friend found
console.log(f);
return;
}
});

I created two different objects in one object. On printing them, only the last modified object is getting displayed. How to create?

var friends=new Object();
friends.bill=new Object();
friends.steve=new Object();
var friends={
bill:{
firstName: "Bill",
lastName: "gates",
number:'040404040',
address: ['bcd','sdad']
}
};
var friends={
steve:{
firstName: "Steve",
lastName: "Jobs",
number:'131313131',
address:['abc','sdsdsd']
}
};
console.log(friends);
the output of this program is all about the object "steve". If I log "friends.steve" it logs as "undefined". I want the output to be two diffrent objects in the object friend.
You are overriding the previously create object.
Rather you should try
var friends=new Object();
friends.bill=new Object();
friends.steve=new Object();
friends.bill={
firstName: "Bill",
lastName: "gates",
number:'040404040',
address: ['bcd','sdad']
};
friends.steve={
firstName: "Steve",
lastName: "Jobs",
number:'131313131',
address:['abc','sdsdsd']
};
console.log(friends);
To store multiple values in a variable you can use the Array.
var bill = {
firstName: "Bill",
lastName: "gates",
number:'040404040',
address: ['bcd','sdad']
}
var steve = {
firstName: "Steve",
lastName: "Jobs",
number:'131313131',
address:['abc','sdsdsd']
}
var friends = [bill, steve];
console.log(friends);
Also, in your code, you are defining the variable friends, which overwrite the variable defined with the same name. Additionally, you can define the class for friend and can create 2 objects from the class.
function Friend(firstname, lastname, number, address)
{
this.firstname = firstname;
this.lastname = lastname;
this.number = number;
this.address = address;
}
var bill = new Friend("Bill", "gates", "address", 040404040 ,['bcd','sdad']);
var steve = new Friend("Steve", "Jobs", "address", 131313131, ['abc','sdsdsd']);
var friends = [bill, steve];
console.log(friends);
In your code you define friends variable and override again in next few lines
'var friends={
bill:{'
This will override the first object and new instance will taking place instead.
Try this code and this will solve your problem
var friends = new Object();
friends.bill = {
firstName: "Bill",
lastName: "gates",
number:'040404040',
address: ['bcd','sdad']
};
friends.steve ={
firstName: "Steve",
lastName: "Jobs",
number:'131313131',
address:['abc','sdsdsd']
};
console.log(friends);

A program in javascript using constructors

<script>
<!-- A program to search for a friend from an object -->
var friends = {
bill: {
firstName: "Bill",
lastName: "Gates",
number: "205-555-1111",
address:["One Microsoft Day","Redmond","WA","90852"]
},
steve: {
firstName: "Steve",
lastName: "Jobs",
number: "408-555-2222",
address: ["One Infinite Loop", "Cupertino", "CA", "95014"]
},
wendy: {
firstName: "Wendy",
lastName: "Johnson",
number: "510-555-3333",
address: ["3555 Anyplace drive","New York", "NY","11001"]
}
}
alert(friends["steve"].lastName);
alert(friends.length);
var search = function(name)
{
document.write(name);
for (var nameSearch in friends)
{
alert(nameSearch.firstName);
if(friends[nameSearch].firstName===name)
{
return friends[nameSearch];
}
}
}
search("Wendy");
</script>
Theres a couple things wrong with your code:
Objects do not have a length property so the second alert for friends.length will not work
When you're using for in you are referencing the key of the object, so in this case it will be bill, steve, or wendy so when you do nameSearch.firstName it will be undefined since nameSearch is a string
Finally, the reason that your example is failing is because you're searching against case-sensitive text. wendy != Wendy. Also please keep in mind that triple equals checks the constructor.
To fix your code, you can try just lower-casing all of your search text:
var friends = {
bill: {
firstName: "Bill",
lastName: "Gates",
number: "205-555-1111",
address:["One Microsoft Day","Redmond","WA","90852"]
},
steve: {
firstName: "Steve",
lastName: "Jobs",
number: "408-555-2222",
address: ["One Infinite Loop", "Cupertino", "CA", "95014"]
},
wendy: {
firstName: "Wendy",
lastName: "Johnson",
number: "510-555-3333",
address: ["3555 Anyplace drive","New York", "NY","11001"]
}
};
var search = function(name) {
for (var nameSearch in friends) {
if(friends[nameSearch].firstName.toLowerCase()===name.toLowerCase()) {
return friends[nameSearch];
}
}
}
console.log(search("wendy"));

Categories

Resources