-3

I am very new to javascript and i've been trying to make a simple function that calculates the total amount of money i spend in a week depending on the money i spend every day but i keep getting the error: Uncaught ReferenceError: numdays is not defined

var amount=calculateamount(7,20);

function calculateamount(numdays, avgmoney) 
{
   return numdays*avgmoney;
    } 

I found the mistake but i have another question. How do i use the Number function and the isNaN function in the code? Is it like this?

return numdays*avgmoney; 
isNaN(calculateamount(numdays, avgmoney));
KatyK
  • 17
  • 1
  • 4
  • 2
    That looks correct... What error do you get and where? – some Jan 22 '18 at 00:55
  • Please, [edit] your question and provide a [mcve]. – Sebastian Simon Jan 22 '18 at 00:58
  • 2
    what is the *exact* error you are getting? – Jaromanda X Jan 22 '18 at 01:07
  • The error i'm getting is Uncaught ReferenceError: numdays is not defined – KatyK Jan 22 '18 at 12:18
  • @KatyK And where’s the code necessary to reproduce this error? – Sebastian Simon Jan 22 '18 at 12:31
  • @Xufox In my code haven't i defined the 'numdays'? – KatyK Jan 22 '18 at 12:34
  • @KatyK Yes, you have. The code you’ve shown is working. That’s why we’ve been asking you for the code that _reproduces the error_. – Sebastian Simon Jan 22 '18 at 12:36
  • @Xufox Something was wrong with my browser,i don't get that error anymore, thanks! I have another question, how do i use the Number function and the isNaN function in the code? Is it like this? return numdays*avgmoney; isNaN(calculateamount(numdays, avgmoney)); – KatyK Jan 22 '18 at 13:19
  • 1
    @KatyK Post a new question rather than editing this one. – Scott Jan 22 '18 at 19:53
  • Please invest some time in learning how to use a debugger to go through the code line by line so you can see exactly what is happening. – Heretic Monkey Jan 22 '18 at 19:53
  • @MikeMcCaughan where can i find more information about a debugger? – KatyK Jan 22 '18 at 20:03
  • The usual places; [Google](https://www.google.com/search?q=how+to+debug+javascript&oq=how+to+debug+javascript), [Bing](https://www.bing.com/search?q=how+to+debug+javascript), [DuckDuckGo](https://duckduckgo.com/?q=how+to+debug+javascript&t=hf&ia=qa). Here's something on Stack Overflow from a long time ago: https://stackoverflow.com/q/988363/215552 – Heretic Monkey Jan 22 '18 at 20:06
  • @MikeMcCaughan thanks. Can you help me with my second question? – KatyK Jan 22 '18 at 20:10
  • I'd suggest looking these things up in documentation. I find MDN to be the best: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/isNaN and https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number. Try things out; put things in different orders and see how that affects your output. I find that's the best way to learn, rather than getting the answers spoonfed to you. – Heretic Monkey Jan 22 '18 at 20:13
  • @MikeMcCaughan ok, thanks! – KatyK Jan 22 '18 at 20:13

2 Answers2

0

It looks correct.

var amount=calculateamount(7,20);
console.log('The amount is', amount);

function calculateamount(numdays, avgmoney) 
{
   return numdays*avgmoney;
} 

I added console.log to see the result.

some
  • 48,070
  • 14
  • 77
  • 93
0

Your code looks good. Please check the simple HTML below where I used your function to return the correct value.

<html>
<head>
<script>
var amount=calculateamount(7,20);
alert(amount);
function calculateamount(numdays, avgmoney) 
{
   return numdays*avgmoney;
} 
</script>
</head>
</html>
Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75