+ 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)

29th Oct 2021, 6:32 PM
Jaabir
Jaabir - avatar
8 Antworten
+ 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
31st Oct 2021, 6:42 AM
// Ms R
// Ms R - avatar
+ 2
29th Oct 2021, 6:41 PM
Jaabir
Jaabir - avatar
+ 1
Add your code please
29th Oct 2021, 6:36 PM
Guillem Padilla
Guillem Padilla - avatar
+ 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); }
29th Oct 2021, 6:37 PM
Jaabir
Jaabir - avatar
+ 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
29th Oct 2021, 6:38 PM
Guillem Padilla
Guillem Padilla - avatar
+ 1
Guillem Padilla so I should use 'return' instead of 'console.log'
29th Oct 2021, 6:40 PM
Jaabir
Jaabir - avatar
+ 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; }
29th Oct 2021, 6:41 PM
Guillem Padilla
Guillem Padilla - avatar
31st Oct 2021, 6:59 AM
Jaabir
Jaabir - avatar