+ 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.
11 Réponses
+ 2
Okay, try this
function convert(amount, rate) {
let cvt = amount * rate;
return cvt;
}
+ 2
Thanks again Mohammed !
+ 2
Covenant Monday Thank you. Looks like there are various ways to solve the question.
+ 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.
+ 1
Thank you Mohammed . So the reason my initial answer didn’t work is because I declared the function before the variables were declared?
+ 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.
+ 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.
+ 1
Tahiti🍷Castillo
No problem.
+ 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.
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
0
Tahiti🍷Castillo
You can ignore it, just call your function inside main.