0

Wanted to try out using external JS file, but I can't seem to be able to call a function I defined there. Every time I try to call it, an error "Uncaught ReferenceError: calculate (function name) is not defined" is returned. I've been coding for only a week, so trying to figure out my error myself led me nowhere.

Chunks of code:

<head>
 <meta charset= "utf-8">
 <script type= "text/javascript" src= "nameOfFile.js"></script>
</head

Trying to call the function:

<select type= "select" id = "currency" onchange= "calculate()">
 <option value= "RUB", selected> RUB </option>
 <option value= "USD"> USD </option>
 <option value= "EUR"> EUR </option>
 <option value= "GBP"> GBP </option>

The function in external file:

function calculate() {
 var currencyName = document.getElementById("currency");
 var currency = currencyName.value;
 var time = document.getElementById("timeInput");
 var sum = document.getElementById("sumInput");
 var amount = sum.value;
 var income; //pure percent revenue
 var earnings; //income + revenue
 var result = document.getElementById("results");
 var percentage;
 switch (currency) {
 case RUB:
 if (time>= 3 && time <= 5) {
 percentage = 5;
 }
 else if (time >= 6 && time <= 11){
 percentage = 6;
 }
 else if (time >= 12 && time <= 24){
 percentage = 6,5;
 }
 break;
 case USD:
 if (time>= 3 && time <= 5) {
 percentage = 0,1;
 }
 else if (time >= 6 && time <= 11){
 percentage = 0,5;
 }
 else if (time >= 12 && time <= 24){
 percentage = 1;
 }
 break;
 case EUR:
 case GBP:
 if (time>= 3 && time <= 5) {
 percentage = 0,1;
 }
 else if (time >= 6 && time <= 11){
 percentage = 0,25;
 }
 else if (time >= 12 && time <= 24){
 percentage = 0,5;
 }
 break;
 }
 income = percentage / 100 * amount;
 earnings = amount + income;

}

The page seems to recognize the existence of .js file, I can see it in "Sources" tab in Chrome, for example. Both files are in the same folder.

EDIT: Problem solved, see my answer.

asked Jan 17, 2017 at 9:46
13
  • 1
    Can you add the whole .js file? Commented Jan 17, 2017 at 9:47
  • is that the full js file or is there more? is the function declaration nested in somthing? Commented Jan 17, 2017 at 9:48
  • 1
    html and js both file on same folder? Commented Jan 17, 2017 at 9:48
  • Try using javascript:change() Commented Jan 17, 2017 at 9:52
  • @WasteD Kinda embarrassing, but added the whole function in the question for ya Commented Jan 17, 2017 at 9:54

2 Answers 2

1

Apparently, problem was that I accidentally added some HTML code in the external JS file.

I noticed that I had paragraphs at the bottom (for some reason...) and when I deleted them and fixed commas to dots (thanks, Tristup and Thomas), everything started to work fine.

answered Jan 17, 2017 at 10:23
Sign up to request clarification or add additional context in comments.

2 Comments

it does? because you have a few more mistakes in there: value= "RUB", selected here should be no ,, these: case RUB: should be strings case "RUB": and should throw errors at the moment, your </head tag is not properly closed, and I don't know wether you just missed to copy the closing </select>-tag, or wether it is missing in your markup.
Yeah, i noticed all this mess, while searching for problem , fixed that.
0

Can you load the script at the bottom or in Footer section of the HTML. May be the function unable to recognize the element that are called into it.

To check that file is included properly. You can write alert("hi"); at the top of the file that included.

Please let me know if you need to know anything more on it.

Thanks and Regards

answered Jan 17, 2017 at 9:52

7 Comments

Please use try this case 'USD':
Please check your console for the error, which does not let your function to load. Please fix "percentage = 6,5;" should be "percentage = 6.5;" . Comma "," is not allowed here in javascript for precision.
percentage = 6,5 is perfectly valid JS. But you're right, it does not do what he probably intended/meant.
Thomas, Just tried to see in w3c, <!DOCTYPE html> <html> <body> <p>Floating point arithmetic is not always 100% accurate.</p> <button onclick="myFunction()">Try it</button> <p id="demo"></p> <script> function myFunction() { var x = 5,2; document.getElementById("demo").innerHTML = x; } </script> </body> </html>
Seems that not working, you may be talking about the percentage function where the number of precision is specified in this way.
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.