Why does this output undefined? And how can I solve it? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Why does this output undefined? And how can I solve it?

https://code.sololearn.com/Wjfb0A8J88YF/?ref=app

15th May 2022, 12:57 PM
Muhammad Awwal Yusuf
Muhammad Awwal Yusuf - avatar
12 Answers
+ 1
Done thanks
15th May 2022, 2:45 PM
Muhammad Awwal Yusuf
Muhammad Awwal Yusuf - avatar
+ 1
In function, return this pointer. And then you use like this without new function Person(firstName,lastName,age,eyeColor) { this.firstName = firstName; this.lastName = lastName; this.age = age; this.eyeColor = eyeColor; return this; //return function pointer } const f = Person(firstName , lastName , age);
15th May 2022, 2:59 PM
Jayakrishna 🇮🇳
+ 1
Yes. It works but create second object in same as first object. You are creating second object with previous first inputs, next changing through inputs. So father.age is same as mothers.age, instead of undefined because you given any value still. If not problem then, leave as it is..
15th May 2022, 3:23 PM
Jayakrishna 🇮🇳
+ 1
Thanks for your help
15th May 2022, 3:32 PM
Muhammad Awwal Yusuf
Muhammad Awwal Yusuf - avatar
0
//you are not setting Function values. And also you can't use out side function that way.. function Person(firstName,lastName,age,eyeColor) { this.firstName = firstName; this.lastName = lastName; this.age = age; this.eyeColor = eyeColor; } firstName =prompt("write first name"); lastName = prompt("Write last name"); age = prompt("Write your age"); var f = new Person(firstName ,lastName ,age); document.write("first name is " + f.firstName + " " +"and she is" + f.age + " years old");
15th May 2022, 2:15 PM
Jayakrishna 🇮🇳
0
Jayakrishna🇮🇳 , I still get undefined, it didn't work.
15th May 2022, 2:32 PM
Muhammad Awwal Yusuf
Muhammad Awwal Yusuf - avatar
0
It seems I can only use predefined values with the Constructor
15th May 2022, 2:33 PM
Muhammad Awwal Yusuf
Muhammad Awwal Yusuf - avatar
0
How do I write a function that accept input and output it back?
15th May 2022, 2:34 PM
Muhammad Awwal Yusuf
Muhammad Awwal Yusuf - avatar
0
Observe my reply carefully.. Try that, just copy and paste in your code. Run it and see.. Your object creation is still wrong.
15th May 2022, 2:37 PM
Jayakrishna 🇮🇳
0
What if I don't want to use "new" ? What can I use to replace it?
15th May 2022, 2:45 PM
Muhammad Awwal Yusuf
Muhammad Awwal Yusuf - avatar
0
Jayakrishna🇮🇳 can you check my code again, did I do everything right this time?
15th May 2022, 3:10 PM
Muhammad Awwal Yusuf
Muhammad Awwal Yusuf - avatar
0
You're welcome..
15th May 2022, 3:33 PM
Jayakrishna 🇮🇳