I've object {name: 'John', 'surname': 'Johnson', 'age': '20'} - javascript

Please tell me, I need to write a function that writes the corresponding values
into the variables name, surname and age.
How can this be implemented?
let user = {
name: 'John',
surname: 'Johnson',
age: '20',
};
let{name, surname, age} = user;
document.write(name + ' ' + surname + ' ' + age);

Ummm
function writeUser(user){
document.write(user.name + " " + user.surname + " " +user.age)
}
?

Let's see if this is something in the lines of what you're looking for:
const user = {
name: 'John',
surname: 'Johnson',
age: '20',
};
function updateUser(user) {
user.name = "New name";
user.surname = "New surname";
user.age = "New age";
}
updateUser(user);
const { name, surname, age } = user;
document.write(name + ' ' + surname + ' ' + age);
By adding an other parameter to this function you might make it more useful, like this:
function updateUser(user, overrides) {
Object.assign(user, overrides);
}
updateUser(user, { age: '30', name: 'New name' });
This would change the age and name but leave the surname, but it modifies the input user which isn't really a good pattern, better to return a new object instead:
function updateUser(user, overrides) {
return Object.assign({}, user, overrides); // First assign user to the empty object, then assign the overrides to that new object
}

Related

How can I add firstName and lastName to fullName?

person = { firstName: 'Joseph', lastName: 'Magnolia', ageInYears: 34 }
function addFullName(personObj) {
person = { fullName: firstName + lastName, ageInYears: 34, }
}
It tells me undefined, and I have tried different things.
your code doesn't really make any sense, but did you mean something like this:
var person = { firstName: 'Joseph', lastName: 'Magnolia', ageInYears: 34 };
addFullName(person);
console.log(person);
function addFullName(personObj) {
personObj.fullName = personObj.firstName + ' ' + personObj.lastName;
}

Compare these simple javascript code related to object

I have written this code can anyone tell what I have written is correct or not.
var employee = {
name: "John Smith",
job: "Programmer",
age: 31,
printing: function(){
for (just in employee){
alert(this.emloyee(just)"is"this.employee[just])
}
}
}
And tell me that the upper code can work the same.
var employee = {
name: "John Smith",
job: "Programmer",
age: 31,
showAlert: function(){
alert("Name is " + this.name);
alert("Job is " + this.job);
alert("Age is " + this.age);
}
}
employee.showAlert()
It seems you are looking for
var employee = {
name: "John Smith",
job: "Programmer",
age: 31,
showAlert() {
for (const just in this) {
if (typeof this[just] != "function") {
alert(just + " is " + this[just])
}
}
},
};
employee.showAlert()
Notice the const declaration in the loop, the usage of the property name just instead of employee(just), the string concatenation with +, and the use of this instead of employee.
Also the showAlert method itself is just a property that gets enumerated by the for … in loop, so I added a condition to only alert non-function properties.

How to properly check if a name is available in an array made of objects (record collection)?

so my plan for this was to have a message appear asking someone to type in a student name. Javascript would look through a record, which is in a seperate JS file, and then output that in the message variable. If suppose the student didn't exist, the output message would be the alert box in the else statement.
Heres a record of the students:
var students=[
{
name:'Chris',
track:'IOS',
achievements:'100',
points:'1000'
},
{
name:'John',
track:'Web Design',
achievements:'90',
points:'1000'
},
{
name:'Brent',
track:'Front-End',
achievements:'70',
points:'1000'
},
{
name:'Josh',
track:'Full-Stack',
achievements:80,
points:'1000'
},
{
name:'Nick',
track:'AI',
achievements:'60',
points:'1000'
}
];
var message="";
var search=prompt("Type name of student");
while (search!=="quit") {
for (var i=0; i<students.length; i+=1) {
var studentName=students[i].name;
if (studentName===search) {
message+="<h1>"+studentName+"</h1>";
message+="<p>"+student[i].track+"</p>";
message+="<p>"+student[i].achievements+"</p>";
message+="<p>"+student[i].points+"</p>";
break;
} else {
alert("That student does not exist. Try again");
break;
}
}
search=prompt("Type name of student");
}
print(message);
When I try this code, it asks me for the student's name and then says he/she is not available. Apparently, the determination that the student is not in the list should only be made after the loop has finished checking all the students. Then, and only if nothing was found, should the failure message be output.
The problem for me, conceptually, is that the final value of the variable, studentName, after the for loop ends will be the name property of the last object in the array. So how would I redesign my for loop then?
How can I redesign my code to accomplish just that?
You can try this,
var message="";
var search=prompt("Type name of student");
while (search!=="quit") {
// we will get result if any one student name matches
var result = students.find((student) => student.name === search);
if (result) {
message+="<h1>"+result.name+"</h1>";
message+="<p>"+result.track+"</p>";
message+="<p>"+result.achievements+"</p>";
message+="<p>"+result.points+"</p>";
}
else {
alert("That student does not exist. Try again");
}
search=prompt("Type name of student");
}
print(message);
you can filter your list first and then check it like
const students = [
{
name: 'Chris',
track: 'IOS',
achievements: '100',
points: '1000'
},
{
name: 'John',
track: 'Web Design',
achievements: '90',
points: '1000'
},
{
name: 'Brent',
track: 'Front-End',
achievements: '70',
points: '1000'
},
{
name: 'Josh',
track: 'Full-Stack',
achievements: 80,
points: '1000'
},
{
name: 'Nick',
track: 'AI',
achievements: '60',
points: '1000'
}
];
let search = prompt('Type name of student');
while (search !== 'quit') {
const filteredList = students.filter(function(student) {
return student.name === search;
});
let message = '';
if (filteredList.length > 0) {
for (const student of filteredList) {
message += '<h1>' + student.name + '</h1>';
message += '<p>' + student.track + '</p>';
message += '<p>' + student.achievements + '</p>';
message += '<p>' + student.points + '</p>';
}
alert(message);
} else {
alert('That student does not exist. Try again');
}
search = prompt('Type name of student');
}
In order to avoid looping through the entire array each time you want to show a message for the user, making an object from the array is the best approach.
for example:
var students=[
{
id: 1,
name:'Chris',
track:'IOS',
achievements:'100',
points:'1000'
},
{
id: 2,
name:'John',
track:'Web Design',
achievements:'90',
points:'1000'
},
{
id: 3,
name:'Brent',
track:'Front-End',
achievements:'70',
points:'1000'
},
{
id: 4,
name:'Josh',
track:'Full-Stack',
achievements:80,
points:'1000'
},
{
id: 5,
name:'Nick',
track:'AI',
achievements:'60',
points:'1000'
}
];
const arrayToObject = (array) =>
array.reduce((obj, item) => {
obj[item.id] = item
return obj
}, {});
const studentsObject = arrayToObject(students);
console.log(studentsObject);
console.log(studentsObject[2]);

This in function inside object Javascript

what is the wrong in this code , function fullAdress
i don't know what is the wrong in my code,
help my to fix it
var person = {
firstName: 'Ammar',
lastName: 'Gais',
age:21,
adress:{
street:'king road',
city:'atabra',
state:'River Nile'
fullAdress: function(){
return this.street+" "+this.city+" "+this.state;
}
},
fullName: function() {
return this.firstName+" "+this.lastName;
}
}
You are missing a comma after 'River Nile'. It is always recommended to look at the browser console for such errors. Even the object has properties or methods, everything should be separated by comma:
var person = {
firstName: 'Ammar',
lastName: 'Gais',
age: 21,
adress: {
street: 'king road',
city: 'atabra',
state: 'River Nile',
fullAdress: function() {
return this.street + " " + this.city + " " + this.state;
}
},
fullName: function() {
return this.firstName + " " + this.lastName;
}
}
console.log(person.adress.fullAdress());
console.log(person.fullName());

Firebase, Anyway to create name dynamically

fredNameRef.set({ first: 'Fred', last: 'Flintstone' });
I want to create 'name' which is first and last name dynamically.
For Example :
var x = first_1;
var y = last_1;
fredNameRef.set({ x: 'Fred', y: 'Flintstone' });
If you are asking about auto-calculated value, Firebase doesn't have such a feature. You will have to pre-calculate the value as you write the name to the database:
fred = { first: 'Fred', last: 'Flintstone' };
fred.name = fred.first + ' ' + fred.last;
refNameRef.set(fred);
You could even validate that write operations don't violate this rule:
".validate": "newData.child('name').val() === (newData.child('first').val() + ' ' + newData.child('last').val())"

Categories

Resources