Desire to experiment with an external JavaScript file arose, but encountered difficulties in invoking a function defined within. Every attempt at calling it resulted in the error message "Uncaught ReferenceError: calculate (function name) is not defined." With only a week of coding experience under my belt, troubleshooting this issue independently proved fruitless.
Snippets of code:
<head>
<meta charset= "utf-8">
<script type= "text/javascript" src= "nameOfFile.js"></script>
</head
Attempting 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 residing in the 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 webpage appears to acknowledge the presence of the .js file, visible in Chrome's "Sources" tab, for example. Both files reside in the same folder.
EDIT: Issue resolved, please refer to my solution below.