Wrong output from annual interest program | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Wrong output from annual interest program

The output is the same as the number that is originally entered as input. I have no idea what I'm doing wrong here: 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 = amount; var interestRate = new calcYearIncome(amount, percent); }; function calcYearIncome(amount, percent){ //complete the function to calculate yearly income interestRate = percent / 100; this.yearIncome = interestRate * amount; }

26th Sep 2021, 6:05 PM
Natanael
8 Answers
+ 2
If you make an instance of the class Loan, like this: var loan1 = new Loan(.... Then you can say "this" points to his owen object, in this case to loan1. this.amount means here loan1.amount
27th Sep 2021, 4:23 PM
Coding Cat
Coding Cat - avatar
+ 2
Oh yes, I was starting with a language named Basic. In the 80's or 70's. And I needed years to bring these OOP things into my head 😂 Python is maybe better to start. But it's not type safe. And the switch to other languages may be harder. At last it's a question of what you want to do with the programs.
27th Sep 2021, 5:13 PM
Coding Cat
Coding Cat - avatar
+ 2
Yes, I've always thought that OOP is difficult makes programming more difficult but it's funny that this is what we do everyday without thinking about it, in OOP a person is an object that has properties(hair) and values(black) but I think in the last challenge exercise it would have been helpful if the instructions mentioned that I would need to use the Return keyword to return the needed value back to the function, but I'm sure I would not have figured it out anyways because I was way off.
27th Sep 2021, 7:16 PM
Natanael
+ 1
Have a look at this. And try to find out how it works: 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; }
26th Sep 2021, 9:27 PM
Coding Cat
Coding Cat - avatar
+ 1
Thank you, I think that the statement (this.yearIncome=calcYearIncome;) calls the function calcYearIncome and the returned value goes into the variable this.yearIncome which will later be displayed by: console.log(loan1.yearIncome); It's amazing how complicated this is, I first learned about functions with C++ and it would have been so easy to write a program like this using statements like cin for input and cout to display the results, including functions and return statements just for practice.
27th Sep 2021, 3:08 PM
Natanael
+ 1
Is this statement here supposed to be considered a function? console.log(loan1.yearIncome()); I'm referring to the last part yearIncome(); Here's the working code: 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; }
27th Sep 2021, 4:09 PM
Natanael
0
Yes, I hate the JS methods too 😉
27th Sep 2021, 3:41 PM
Coding Cat
Coding Cat - avatar
0
Great thanks, it seems like javascript is not a good language for people starting to learn programming, I started with the Basic programming language back in the early 90's, to jump within the code you had to use the goto statement, I'm guessing c++ and python are better for a beginner but I'm familiar with c++
27th Sep 2021, 4:55 PM
Natanael