So i have done this simple task but i don’t see why this is not working? Can same one see it and tell me? I see that the problem is that button does not call out the function but why ?
<html>
<head>
<title>Task count</title>
<script>
var over21 = 6.19;
var under21 = 5.19;
// Getting data from a user connecting variables with textboxes by ID
var age = document.getElementById('age');
var hours = document.getElementById('hours');
// function wich does all of the calculation
function magic(){
//cheking if he is under or over 21
if(age < 21){
alert("You will make " + (age*under21));
}else{
alert("You will make " + (age*over21));
};
};
// function that does everything and is connected to the button
function myFunction(){
// Validation checking that entered number is not more than 150
// And also if age is correct
if((hours < 150 && hours > 0) && (age < 150 && age > 12)){
magic();
}else{
alert("Wrong!");
};
};
</script>
</head>
<body>
<label>Enter your age.(12-150)</label>
<input type="text" class="textBox" id="age"><br>
<label>Enter how long will you work.(1-150)</label>
<input type="text" class="textBox" id="hours"><br>
<button onclick="myFunction()" >How much will i make ?</button>
</body>
</html>
I would suggest not using so many global variables.
Move:
into
myFunction().You’ll then need to make those two:
We use
parseIntto take the value as a string and turn it into an integer value.Since
ageandhoursare now defined inmyFunction, you’ll need to passagetomagic()Also, if you want (1-150), you’ll need to modify this
ifstatement:to:
Lastly, I believe the math may be incorrect in the
magic()function:I believe you wanted
hours*under21andhours*over21. Note thathoursis now also being passed in as a parameter.EXAMPLE