+ 1

Spend before Saving

I have no clue what I'm doing with this can someone please explain what to do, it keeps saying that salaryAmount is not defined, but I don't understand how it's not?? "Do not save what is left after spending; instead spend what is left after saving", said Warren Buffett. Inspired by this words, Jack decided to save 15% of his monthly salary. You are given a program that takes salary as input. Complete the function in order to calculate and output the savings. function main() { var salaryAmount = parseInt(readLine(), 10); // complete the function call getSavings(); } //complete the function function getSavings() { };

3rd Feb 2021, 9:26 PM
Adamjhw
Adamjhw - avatar
21 Réponses
+ 5
you are getting error of "salaryAmount is not defined", because....... you haven't try to write incomplete code. The given lines of codes are hints for you to complete
4th Feb 2021, 1:02 AM
Shivani 📚✍
Shivani 📚✍ - avatar
+ 2
SuperKitsune94 ahh fair enough, I'm very new to coding, but I'll explain my thought process, so obviously salaryAmount will change each time, all we want to do is do salaryAmount times by 0.15 to give 15% of the salaryAmount. You want this to be a function though, so you need something in the function that can be reused (e.g. getSavings(amount)). Then when you call the function with getSavings(salaryAmount), it substitutes the word amount in the code for salaryAmount, which the gets times be 0.15, but we need a variable to assign this to which I called savings, and then I return the value of savings to the console. I hope this helped but as I said Im really new to it all
6th Feb 2021, 12:38 PM
Adamjhw
Adamjhw - avatar
+ 1
I don't get any error saying salaryAmount is not defined.
3rd Feb 2021, 9:51 PM
Abhay
Abhay - avatar
+ 1
function main() { var salaryAmount = parseInt(readLine(), 10); // complete the function call getSavings(salaryAmount); } //complete the function function getSavings(amount) { savings = amount * 0.15 console.log(savings) };
4th Feb 2021, 1:04 AM
Adamjhw
Adamjhw - avatar
+ 1
I think just over two weeks
6th Feb 2021, 12:40 PM
Adamjhw
Adamjhw - avatar
+ 1
The easiest way to solve this challenge is to use the user input directly into the function and complete the math inside the console.log function main() { var salaryAmount = parseInt(readLine(), 10); // complete the function call getSavings(salaryAmount); } //complete the function function getSavings(salaryAmount) { console.log(salaryAmount * 0.15) };
16th Apr 2021, 7:54 AM
Leonard Baker
Leonard Baker - avatar
+ 1
function main() { var salaryAmount = parseInt(readLine(), 10); // complete the function call getSavings(salaryAmount); } //complete the function function getSavings(getSavings) { getSavings = salaryAmount*0.15; console.log(getSavings); }; hi, I came up with the same code, but it doesn't pass the challenge, I tried other solutions proposed in the chat, but it doesn't seem to work. Do you have any idea what could be the problem? So far I didn't have any problems using Chrome. Thanks in advance :)
2nd Jul 2021, 9:53 AM
Brigi
Brigi - avatar
+ 1
@Brigi in line 7 you need to change "(getSavings)" to "(salaryAmount)"
20th Aug 2021, 12:30 PM
Marcin
+ 1
function main() { var salaryAmount = parseInt(readLine(), 10); // complete the function call getSavings(salaryAmount); } //complete the function function getSavings(salaryAmount) { var percentageSavings = .15; console.log(salaryAmount * percentageSavings); };
16th Nov 2021, 6:42 AM
MD RAKIBUL HASSAN NAYON
MD RAKIBUL HASSAN NAYON - avatar
0
What did you put for it?
3rd Feb 2021, 10:17 PM
Adamjhw
Adamjhw - avatar
0
Sorry i don't get what you mean, I copy pasted the above code in node playground for code coaches and got no error saying "salaryAmount is not defined " . I would have tested the code myself if i had access to it.
3rd Feb 2021, 10:22 PM
Abhay
Abhay - avatar
0
Yeah I actually just did it like a minute ago, im glad I finally get it
4th Feb 2021, 1:03 AM
Adamjhw
Adamjhw - avatar
0
Adamjhw This doesn't work
6th Feb 2021, 12:13 PM
SuperKitsune94
SuperKitsune94 - avatar
0
function main() { var salaryAmount = parseInt(readLine(), 10); // complete the function call getSavings(salaryAmount); } //complete the function function getSavings(amount) { savings = amount * 0.15 console.log(savings) }; this is copy and pasted from the code section and it comes up with all ticks for me, sorry if it is wrong though, not trying to spread false info or anything, just my best attempt that gave me ticks SuperKitsune94
6th Feb 2021, 12:19 PM
Adamjhw
Adamjhw - avatar
0
Adamjhw Abhay it worked but I do not get the logic of this at all...
6th Feb 2021, 12:28 PM
SuperKitsune94
SuperKitsune94 - avatar
0
I'm new to coding as well haha Adamjhw how long have you been doing it? Thanks for your advice!
6th Feb 2021, 12:39 PM
SuperKitsune94
SuperKitsune94 - avatar
0
why cant i do somthing like this: function main() { var salaryAmount = parseInt(readLine(), 10); // complete the function call console.log(getSavings(salaryAmount)); } //complete the function function getSavings(salaryAmount) { return salaryAmount * 0.15 };
7th May 2021, 11:13 PM
userx
userx - avatar
0
function main() { var salaryAmount = parseInt(readLine(), 10); // complete the function call getSavings(salaryAmount); } //complete the function function getSavings(parameter) { parameter = parameter * 0.15 console.log(parameter); };
8th Jun 2021, 10:34 PM
Igor
Igor - avatar
0
function main() { var salaryAmount = parseInt(readLine(), 10); // complete the function call getSavings(salaryAmount); } //complete the function function getSavings(salaryAmount) { salary=salaryAmount*0.15; console.log(salary); };
26th Sep 2021, 7:04 AM
GUNASEKARA H.G.D.A.P. (CST18022)
GUNASEKARA H.G.D.A.P. (CST18022) - avatar
0
function main() { var salaryAmount = parseInt(readLine(), 10); // complete the function call getSavings(salaryAmount); } //complete the function function getSavings(salaryAmount) { result= salaryAmount * 0.15 console.log(result);}
10th Oct 2021, 6:15 AM
Hasan Ali
Hasan Ali - avatar