Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,153,776 members, 7,820,715 topics. Date: Tuesday, 07 May 2024 at 08:05 PM

Please Help With This Javascript Code - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Please Help With This Javascript Code (757 Views)

Help Me With This Javascript Problem / Need Someone To Help With My Javascript Code / Javascript Code Not Performing Its Functionality (2) (3) (4)

(1) (Reply) (Go Down)

Please Help With This Javascript Code by arabmoney14: 7:46pm On Jan 01, 2022
I am following a JavaScript tutorial in which the tutor runs a similar code as below. It's supposed to create a new property called age and set the value to (2022 - birth year).
Apparently, mine outputs undefined.

Anyone with an idea why?

Re: Please Help With This Javascript Code by TinubuFirstBorn: 8:25pm On Jan 01, 2022
const profile = {
name: "Michael",
level: 100,
birthYear: 2003,
calcAge() {
let age = 2022 - this.birthYear;
return age;
},
};

console.log(profile.calcAge());

1 Like

Re: Please Help With This Javascript Code by TinubuFirstBorn: 8:31pm On Jan 01, 2022
const profile = {
name: "Michael",
level: 100,
birthYear: 2003,
calcAge() {
this.age = 2022 - this.birthYear;
return this.age;
},
};

console.log(profile.calcAge());

1 Like

Re: Please Help With This Javascript Code by TinubuFirstBorn: 8:36pm On Jan 01, 2022
The two ways I posted will output the value of age.

You have to return the age from the function.

You also have to call the function: profile.calcAge()
Re: Please Help With This Javascript Code by jbreezy: 8:43pm On Jan 01, 2022
@ op...Do you know about Scoping in Javascript? You can't access a variable located in a local scope outside its curly parenthesis.
Re: Please Help With This Javascript Code by efelico: 8:55pm On Jan 01, 2022
TinubuFirstBorn:
const profile = {
name: "Michael",
level: 100,
birthYear: 2003,
calcAge() {
let age = 2022 - this.birthYear;
return age;
},
};

console.log(profile.calcAge());
@ OP this is what you need. In addition to that you are getting undefine because age is a local variable. Variables declared within a JavaScript function, become LOCAL to the function. They can only be accessed from within the function. Read more on local and global scope
Re: Please Help With This Javascript Code by qtguru(m): 10:24pm On Jan 01, 2022
this.age is not defined because it's defined in the function, when the function is called, age will be added to the property of the object. Hope that explains
Re: Please Help With This Javascript Code by arabmoney14: 11:49am On Jan 02, 2022
Thanks to everybody TinubuFirstBorn jbreezy
efelico qtguru.
The code now works as expected after implementing your suggestions
Re: Please Help With This Javascript Code by stanliwise(m): 12:44pm On Jan 02, 2022
arabmoney14:
I am following a JavaScript tutorial in which the tutor runs a similar code as below. It's supposed to create a new property called age and set the value to (2022 - birth year).
Apparently, mine outputs undefined.

Anyone with an idea why?
You need to do this before the console.log()
profile.calcAge();
Re: Please Help With This Javascript Code by Xinjin: 2:37pm On Jan 02, 2022
you can do it as @TinubufirstBorn did. see below
Re: Please Help With This Javascript Code by Xinjin: 2:48pm On Jan 02, 2022
The "this" keyword belongs to the Profile Object you define not the function. and Profile Object doesn't have any property call "age"
Re: Please Help With This Javascript Code by duyilekan98(m): 11:27pm On Jan 16, 2022
arabmoney14:
I am following a JavaScript tutorial in which the tutor runs a similar code as below. It's supposed to create a new property called age and set the value to (2022 - birth year).
Apparently, mine outputs undefined.

Anyone with an idea why?

First of all, there’s no object property called age, so what is this.age referencing?

In my example I create an object profile with some properties. I set age to 0, so I can calculate based on current year which I would be passing as a parameter in the calcAge function or method(a method is an object property that has a function).
Initially when I check the value of property, age is set to 0. But when I run profile.calcAge(2022), you can see the age changed to 24.

Re: Please Help With This Javascript Code by Naijatask: 8:40am On Jan 17, 2022
Apparently, what happened was that you did not call the function which was returning the age variable. You have to call the profile.calcAge() function before it can return the age variable.

(1) (Reply)

Php Developer Needed For Modification On An Already Existing Website! / Do Programmers Believe In God Like Me??? / Please I Need Your Help!

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 16
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.