+ 2
In the currency converter on Javascript,it keeps on getting the right result but also says 'undefined' How do I clear this bug I
Currency converter( need help)
8 odpowiedzi
+ 2
The issue is that you console.log a function that doesn't return a value but prints; in your main function you have console.log(....... a function that also prints). To solve this you can change your console.log(amount * rate); -> return amount * rate; in your convert() function
+ 2
Guillem Padilla thanks
+ 1
Add your code please
+ 1
function main() {
var amount = parseFloat(readLine(), 10);
var rate = parseFloat(readLine(), 10);
console.log(convert(amount, rate));
}
function convert(amount, rate){
console.log(amount * rate);
}
+ 1
Just remove the console.log inside your main function because you are returning nothing inside converter function, that’s why it is printing undefined
Else you can change your console.log inside convert for this:
return amount * rate
And mantain the console.log in main
+ 1
Guillem Padilla so I should use 'return' instead of 'console.log'
+ 1
function main() {
const amount = parseFloat(readLine(), 10);
const rate = parseFloat(readLine(), 10);
console.log(convert(amount, rate));
}
function convert(amount, rate){
return amount * rate;
}
+ 1
Refilwe M. Mashile thanks