JavaScript: functions return nothing (vscode + Quokka.js)

1.2k Views Asked by At

I'm practicing JavaScript (just started this week) on Visual Studio Code with the Quokka.js extension. I started learning about functions, this is my first code:

function calculateTip(price, percentage) {
    console.log('calculating tip')
return (percentage/100) * price;
}

calculateTip(50, 18);

This function above only prints me the text 'calculating tip'. It returns nothing (no result from the calculation: percentage/100*price) although I have written the return code.

// function expression
var divideByN = function(number, n) {
return number / n;
}

divideByN(18, 3);

The function expression above returns me nothing (it should return 18/3).

// anonymous function
(function(number, n) {
console.log('I am an expression');

return number / n;
})(18, 3);

The anonymous function above only prints me the text 'I am an expression'. It should return number/n.

So, console.log seems to be working alright, but the functions/return calculations themselves not. At least they are not returning any numbers or text, not even undefined or errors or NaN. What could be the cause of this in vscode+Quokka.js? Do you have any alternative options for learning JS functions in vscode?

(keywords and keyphrases function returns nothing, nothing returned out of a function, function no return, function prints nothing, javascript function returns nothing, javascript function no return, javascript function not working, javascript functions not working in vscode)

1

There are 1 best solutions below

1
On BEST ANSWER

You need output your results to actually see them. The function is returning the values, but you're not outputting which is evidenced by the console.log being called inside those functions. Use console.log to see them live on the file using Quokka. This is possible duplicate, but I couldn't find that question.

console.log(calculateTip(50, 18))
console.log(divideByN(18, 3))
console.log(
  // anonymous function
  (function(number, n) {
    console.log('I am an expression');

    return number / n;
  })(18, 3)
)