Currency Converter | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Currency Converter

You are making a currency converter app. Create a function called convert, which takes two parameters: the amount to convert, and the rate, and returns the resulting amount. The code to take the parameters as input and call the function is already present in the Playground. //Here: https://code.sololearn.com/cPGzqZu7seXn/?ref=app Create the function to make the code work. //My attempt: https://code.sololearn.com/cg30RleQUDm6/?ref=app //Please help. What am I doing wrong? Thank you.

23rd Jan 2022, 8:00 PM
Tahiti🌍Castillo
Tahiti🌍Castillo - avatar
11 Answers
+ 2
Okay, try this function convert(amount, rate) { let cvt = amount * rate; return cvt; }
24th Jan 2022, 12:43 PM
Covenant Monday
Covenant Monday - avatar
+ 2
Thanks again Mohammed !
24th Jan 2022, 8:39 PM
Tahiti🌍Castillo
Tahiti🌍Castillo - avatar
+ 2
Covenant Monday Thank you. Looks like there are various ways to solve the question.
24th Jan 2022, 8:48 PM
Tahiti🌍Castillo
Tahiti🌍Castillo - avatar
+ 1
The example below might help understanding functions. var a=3; var b=4; function multiply(k,r){return k*r;} console.log(multiply(a,b)); You can copy the code above and paste in node js, the result is 12.
23rd Jan 2022, 9:08 PM
Mohammed
+ 1
Thank you Mohammed . So the reason my initial answer didn’t work is because I declared the function before the variables were declared?
23rd Jan 2022, 9:14 PM
Tahiti🌍Castillo
Tahiti🌍Castillo - avatar
+ 1
Tahiti🍷Castillo True, consider this example function e(){ var a=3 return 0; } console.log(a); // a not defined And you didn't enclosed the curly brackets in line 5.
23rd Jan 2022, 9:23 PM
Mohammed
+ 1
Thanks for your help Mohammed . One last question. Do we simply ignore Sololearn’s function main() { } and add our own code below it? It seems I’ve been confused by that as well. I didn’t find a good explanation on Stack Overflow or other places I looked.
23rd Jan 2022, 11:54 PM
Tahiti🌍Castillo
Tahiti🌍Castillo - avatar
+ 1
Tahiti🍷Castillo No problem.
24th Jan 2022, 8:40 PM
Mohammed
+ 1
Since the code to take the parameters as input and call the function is already provided in the Playground, you can simply change the function name from 'main' to 'convert' and input the parameters 'amount' and 'rate'. Once your function is created, you must set the values for your variables, which are: amount=100, rate=1.1. //Solution function convert(amount, rate) { var amount = parseFloat(readLine(), 10); var rate = parseFloat(readLine(), 10); return amount * rate; } var amount=100, rate=1.1; { console.log(convert(amount, rate)); } Side note: I'm a noob programmer so my apologies if I've used incorrect keywords or explained poorly.
14th Feb 2022, 12:25 AM
psychederik
psychederik - avatar
0
Try this function main(){ var amount = parseFloat(readLine(), 10); var rate = parseFloat(readLine(), 10); function convert(amount, rate) { return (amount*rate); } console.log(convert(3,0.58)); } replace 3 with amount and 0.58 with rate
23rd Jan 2022, 8:53 PM
Mohammed
0
Tahiti🍷Castillo You can ignore it, just call your function inside main.
24th Jan 2022, 6:36 AM
Mohammed