This in function inside object Javascript - 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());

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;
}

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

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
}

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.

Want to achieve functionality by using Javascripts map,reduce,foreach,filter methods

Please help me to write better code.
Suppose I have a JavaScript array like below:
var students = [
{ firstname: "stud1", lastname: "stud2", marks: "60" },
{ firstname: "stud3", lastname: "stud4", marks: "30" },
{ firstname: "stud5", lastname: "stud6", marks: "70" },
{ firstname: "stud7", lastname: "stud8", marks: "55" },
{ firstname: "stud9", lastname: "stud10", marks: "20" },
];
On page I want to show data in two parts, 1)Students who got marks >= 40 2)Students who got marks < 40
By using for loop I done this functionality easily as below.
var data = "";var data1 = "";
for (var i = 0; i < students.length; i++ )
{
if(students.marks >= 40){
data += = students.firstname + " " + students.lastname; //actual data in html div tag
....
}else{
data1 += = students.firstname + " " + students.lastname;
}
}
I want to achieve same functionality by using JavaScript methods like map, reduce, foreach, filter etc. (want to improve my JavaScript knowledge)
Don't know exactly which method is useful for such functionality.
Used map method and tried to display data, but there is a trailing , at the end of each object/array.
Can anyone please help/guide me to write proper code?
Here is solution using filter and forEach methods, using callback functions.
See references here :
filter method
var students = [
{ firstname: "stud1", lastname: "stud2", marks: "60" },
{ firstname: "stud3", lastname: "stud4", marks: "30" },
{ firstname: "stud5", lastname: "stud6", marks: "70" },
{ firstname: "stud7", lastname: "stud8", marks: "55" },
{ firstname: "stud9", lastname: "stud10", marks: "20" },
];
students.filter(function(item){
return item.marks>=40;
}).forEach(function(item){
div=document.getElementById('persons');
div.innerHTML+=item.firstname+' '+item.lastname+'<br>';
});
<div id="persons"></div>
You could use a single loop and add the information with predicate as index. Later join the elements for getting a sting for each element.
var students = [{ firstname: "stud1", lastname: "stud2", marks: "60" }, { firstname: "stud3", lastname: "stud4", marks: "30" }, { firstname: "stud5", lastname: "stud6", marks: "70" }, { firstname: "stud7", lastname: "stud8", marks: "55" }, { firstname: "stud9", lastname: "stud10", marks: "20" }],
predicate = function (o) { return o.marks >= 40; },
result = [[], []];
students.forEach(function (a) {
result[+predicate(a)].push(a.firstname + ' ' + a.lastname);
});
result = result.map(function (a) { return a.join(', '); });
console.log(result);

Pulling array from object

Im trying to pull the gpa numbers from the array that is in the object and have them displayed in the console but my code keeps giving me undefined '0' error. Any help would be appreciated.
var fsInfo = {
name: 'John Doe',
address:{
street: '123 Some Street ',
city: 'Town, ',
state: 'HI',
gpa: [3.0,4.0,2.0]
}
}
console.log("GPA: " + fsInfo.gpa['0'],fsInfo.gpa['1'],fsInfo.gpa['2'])
Use
console.log("GPA: " + fsInfo.gpa[0],fsInfo.gpa[1],fsInfo.gpa[2])
Note: Array indices are numbers.
In your case, they are inside address. So you should do
console.log("GPA: " + fsInfo.address.gpa[0],fsInfo.address.gpa[1],fsInfo.address.gpa[2])
If your object had been like this
var fsInfo = {
name: 'John Doe',
address:{
street: '123 Some Street ',
city: 'Town, ',
state: 'HI'
},
gpa: [3.0,4.0,2.0]
}
then
console.log("GPA: " + fsInfo.gpa[0],fsInfo.gpa[1],fsInfo.gpa[2])
will work.

Categories

Resources