Why does my calculation repeat and show an error?


#1


#2

I copied down your code and I got the same error so I satrted looking... First of all you are missing some semicolons ( ; ) in your syntax! After you finish every single function allways but ALLWAYS use this ;
To fix your problem all I did was just delete the text inside the console.log and it worked fine.

Just for curiosity: My code was a litle bit different... I did like this:

function orangeCost(price) {
var total = price * 5;
console.log(total);
};

orangeCost(5);

But don't do this unless you really know what you are doing because you can create bad habbits! I just made a function for the exercice (kinda like a shortcut) but because I didn't create a variable my function would be useless if I wanted to call her somewhere else.


#3

My code looks something like this

var orangeCost = function (price) {
var val = price * 5;
console.log(val);
};

orangeCost("10");

This exercise was hotly debated as when creating functions you need to be able to substitute any number to get an outcome. This exercise needs work

For example

function calculateVolume(length,width,height) {
var volume = length*width*height;
document.write(volume);
};

The structure is clearly defined and can accept any numerical input.


#4

Yes the problem is that the exercise expects to just see a number on the screen, so the additional strings are just distraction.

According to the error message I'd assume that the code should actually look something like this:

Spoiler Alert!

var orangeCost = function(price) {
    return price * 5;   
};
console.log(orangeCost(5));

Which makes more sense as now the function just computes a number that could be used inside of the program else where. Instead if you use the console.log inside of the function, its only purpose is to print that value, no further use inside the program and in this case using a string to explain the output would actually make sense too.

And about you're function. Maybe you should google function declaration and function expression. Your code is perfectly valid and you could call the function by orangeCost elsewhere (as you already did). You could actually call it before your declaration of the function, due to a feature called hoisting. But I guess this is too confusing and advanced for now and more like something you could google after finishing this track.

@r2d2_12 better pass a number to the function instead of a string. In case of * it might work but consider you'd had to calculate price+5 then 10+5 would be "105". And I'd rather consider to use the manipulation of a html tags text than using document.write().