0

What’s wrong with this code JavaScript

There is a practice question in SoloLearn that I am not sure what’s wrong with the coding. I have written 2 lines of coding , see follows: Question: The program you are given should calculate the annual income of a given loan. The program takes the loan initial amount and the annual interest percentage as input, then creates the loan object. Complete the function outside the constructor to calculate the annual income, then assign that value to the corresponding field, that you also create, of the constructor in order to execute the given output. Sample Input 15000 20 Sample Output 3000 Coding: function main() { //get the initial amount and the interest percentage var amount = parseInt(readLine(),10); var yearPercent = parseInt(readLine(),10); var loan1 = new Loan(amount, yearPercent); //output to console console.log(loan1.yearIncome()); } function Loan(amount, percent) { this.amount = amount; this.yearPercent = percent; //my code is here 1 this.yearIncome = calcYearIncome }; function calcYearIncome(amount,percent){ //my coding is here 2 complete the function to calculate yearly income this.yearIncome= amount*percent/100; }

4th Nov 2020, 11:39 AM
C Yam
C Yam - avatar
18 Answers
+ 2
try this. function Loan(amount, percent) { this.amount = amount; this.yearPercent = percent; //my code is here 1 this.yearIncome = calcYearIncome (amount,percent); }; console.log(loan1.yearIncome()); then change the output to this console.log(loan1.yearIncome); change also this function calcYearIncome(amount,percent){ //my coding is here 2 complete the function to calculate yearly income this.yearIncome= amount*percent/100; } to this function calcYearIncome(amount,percent){ //my coding is here 2 complete the function to calculate yearly income return( amount*percent/100); }
4th Nov 2020, 5:11 PM
Sacalivin Obiri
Sacalivin Obiri - avatar
+ 7
function main() { //get the initial amount and the interest percentage var amount = parseInt(readLine(),10); var yearPercent = parseInt(readLine(),10); var loan1 = new Loan(amount, yearPercent); //output to console console.log(loan1.yearIncome()); } function Loan(amount, percent) { this.amount = amount; this.yearPercent = percent; this.yearIncome = calcYearIncome;//your code goes here } function calcYearIncome(){ return this.amount * (this.yearPercent/100); //complete the function to calculate yearly income }
13th Feb 2021, 11:48 AM
Nuria Viana
Nuria Viana - avatar
+ 3
It is working for me, btw why have you used readLine() ? I've used prompt in this example, and it works, you used loan.yearIncome() but it is not a function, remove the (). This works: function main() { var amount = parseInt(prompt(),10); var yearPercent = parseInt(prompt(),10); var loan1 = new Loan(amount, yearPercent); console.log(loan1.yearIncome); } function Loan(amount, percent) { this.amount = amount this.yearPercent = percent this.yearIncome = calcYearIncome(amount, percent) }; function calcYearIncome(amount,percent){ return amount*percent/100 } main()
4th Nov 2020, 1:29 PM
maf
maf - avatar
+ 2
You should call the function by writing () after calcYearIncome inside Loan and pass amount and percent to calcYearIncome(), coz qs says that assign the value of calcYearSome to this.yearIncome. And yeah, return the value inside calcYearIncome function so that you can assign it to the property yearIncome Fixed: function Loan(amount, percent) { this.amount = amount this.yearPercent = percent this.yearIncome = calcYearIncome(amount, percent) }; function calcYearIncome(amount,percent){ return amount*percent/100 }
4th Nov 2020, 11:47 AM
maf
maf - avatar
+ 2
maf that is node js ,prompt won't work there ,tho I have no idea what is readLine and main function in node but I am assuming it's something how node process(or maybe sololearn did some changes) input from readLine function ,it calls main on its own and provides input to test
4th Nov 2020, 1:56 PM
Abhay
Abhay - avatar
+ 1
thanks but the suggested coding doesnt work
4th Nov 2020, 1:08 PM
C Yam
C Yam - avatar
+ 1
readLine() is provided as part of the solution by the program
4th Nov 2020, 1:41 PM
C Yam
C Yam - avatar
+ 1
Abhay yeah i figured that out, i have never done js challenges on sololearn that's why never encountered readLine(). But now it must work Cheri Yan just change it back to readLine() and see if it works. I tried fixing the code on my mobile on spck editor thats why changed it to prompt.
4th Nov 2020, 2:01 PM
maf
maf - avatar
+ 1
Hi there. From what I understand, you are omitting the ; in the initialization of the this.yearIncome property and not utilising the “this” keyword when describing variables within the calcYearIncome function. Here is the code that worked for me: function Loan(amount, percent) { this.amount = amount; this.yearPercent = percent; //your code goes here this.yearIncome = calcYearIncome; }; function calcYearIncome(amount,percent){ //complete the function to calculate yearly income return (this.yearPercent / 100 ) * this.amount; }
23rd Jun 2021, 10:55 AM
Jer
Jer - avatar
+ 1
HI ! this is I think the neater way, to solve this problem function main() { //get the initial amount and the interest percentage var amount = parseInt(readLine(),10); var yearPercent = parseInt(readLine(),10); var loan1 = new Loan(amount, yearPercent); //output to console console.log(loan1.yearIncome()); } function Loan(amount, percent) { this.amount = amount; this.yearPercent = percent; //your code goes here this.yearIncome = calcYearIncome; //the calcYearIncome function is store into yearIncome }; function calcYearIncome(){ //complete the function to calculate yearly income return (this.yearPercent/100)*this.amount }
25th Aug 2021, 11:29 AM
riven 2018
riven 2018 - avatar
0
i also tried changing it to “prompt” but it doesnt work when i submitted to sololearn
4th Nov 2020, 1:54 PM
C Yam
C Yam - avatar
0
it works now! really appreciate your help. thanks a lot !
4th Nov 2020, 11:32 PM
C Yam
C Yam - avatar
0
Rana Pratap Singh
5th Nov 2020, 9:30 AM
Akash Shishodia
Akash Shishodia - avatar
0
Welcomed always.
8th Nov 2020, 3:56 PM
Sacalivin Obiri
Sacalivin Obiri - avatar
0
function main() { //get the initial amount and the interest percentage var amount = parseInt(readLine(),10); var yearPercent = parseInt(readLine(),10); var loan1 = new Loan(amount, yearPercent); //output to console console.log(loan1.yearIncome()); } function Loan(amount, percent) { this.amount = amount; this.yearPercent = percent; //your code goes here this.yearIncome=calcYearIncome; }; function calcYearIncome(){ //complete the function to calculate yearly income return ((this.amount)*(this.yearPercent/100)); }
24th Feb 2021, 2:12 AM
Manish Bardhan
Manish Bardhan - avatar
0
I find I just can't follow the logic. I don't want to copy out a correct answer, if possible I want to understand how it works, but this section of the course moves very fast for me. Anyone got any other good resources for this? Thanks
12th Mar 2021, 7:20 PM
Owain
Owain - avatar
0
Owain, I hear you. Too fast for me too. I'd like to understand the Function, Object and Method concepts better. Even if I solve the problem sections I may not fully comprehend the concepts... I understand Manish's logic though – except for this: In his Loan function Manish already stores/defines this.amount = amount; this.yearPercent = percent; What's the point though if he then needs to use this.amount and this.yearPercent for the return trigger again? Why can't we use the parameters we stored above, as in: return ((amount)*(percent/100)); or even return (amount*percent)/100; Anyone?! Thanks!
15th Dec 2021, 12:14 PM
Rapha
0
function main() { //get the initial amount and the interest percentage var amount = parseInt(readLine(),10); var yearPercent = parseInt(readLine(),10); var loan1 = new Loan(amount, yearPercent); //output to console console.log(loan1.yearIncome()); } function Loan(amount, percent) { this.amount = amount; this.yearPercent = percent; this.yearIncome = calcYearIncome;//your code goes here } function calcYearIncome(){ return this.amount * (this.yearPercent/100); //complete the function to calculate yearly income } Good Luck
25th Jan 2022, 8:31 AM
Muhammad Alif Deva Rizqon
Muhammad Alif Deva Rizqon - avatar