I'm struggling to grasp the distinction between
var maxResult = window.max(maxValInt1, maxValInt2);
which functions properly, and
var maxResult = max(maxValInt1, maxValInt2);
which gives an error "object is not a function".
Why do I have to include window.
before the max function?
I am new to JavaScript and would appreciate it if you could explain in detail.
<div id="maxOfTwo">Max function: <input type = "number" name="val1" value="0">, <input type = "number" name="val2" value="0"> = <span></span></div>
<script type="text/javascript">
function max(val1, val2){
if(val1 > val2){
return val1;
}else{
return val2;
}
}
window.onload = function() {
var max = document.getElementById("maxOfTwo");
var maxNumber1 = max.children[0];
var maxNumber2 = max.children[1];
maxNumber1.addEventListener("blur", doMax);
maxNumber2.addEventListener("blur", doMax);
function doMax() {
var maxValue1 = maxNumber1.value;
var maxValue2 = maxNumber2.value;
var maxValInt1 = parseInt(maxValue1);
var maxValInt2 = parseInt(maxValue2);
var maxResult = window.max(maxValInt1, maxValInt2);
max.children[2].innerHTML = maxResult;
}
}
</script>