Reputation: 21
I need some help explaining this code. I have changed the values but for some reason, the calculated value is always coming up as 12.
<html>
<head>
<script>
function display(x)
{
alert("The result is " + x);
}
function calculate(){
var values = new Array(5, 8, 18, 16);
var output = values[1]+values[3];
if (output >= 23){output = output / 2;}
else {output = output++;}
display(output);
}
</script>
</head>
<body>
<button onclick="calculate()">Click to calculate</button>
</body>
Thanks!
Upvotes: 0
Views: 569
Reputation: 1114
You may be thinking that using values[1]
and values[3]
will give you the first and third elements, specifically 5 and 18. However, the array index begins with 0. Changing your code to values[0]
and values[2]
would most likely give the desired result.
Upvotes: 1