View Single Post
  #3 (permalink)  
Old June 11th, 2015, 12:03 PM
mrected mrected is offline
Registered User
Points: 3, Level: 1
Points: 3, Level: 1 Points: 3, Level: 1 Points: 3, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2015
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default This will work

Doesn't make use of frames so obviously the frame errors won't be thrown, but the rest of it functions within 1 page...

Code:
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Calculate Factorial</title>
<script>
function calcFactorial(factorialNumber){
	var factorialResult=1;
	for (; factorialNumber > 0; factorialNumber--){
		factorialResult=factorialResult*factorialNumber;
	}
	return factorialResult;
}

function butCalculate_onclick(){
	try{
		if (document.form1.txtNum1.value=="")
			throw "!Please enter a value before you calculate its factorial";
		if (isNaN(document.form1.txtNum1.value))
			throw "!Please enter a valid number";
		if (document.form1.txtNum1.value<0)
			throw "!Please enter a positive number";
		
		document.form1.txtResult.value = window.calcFactorial(document.form1.txtNum1.value);
}

	catch(exception){
		if (typeof(exception)=="string")
		{
			if (exception.charAt(0) =="!")
			{
				alert(exception.substr(1));
				document.form1.txtNum1.focus();
				document.form1.txtNum1.select();
			}
			else
			{
				alert(exception);
			}
		}
		else
		{
			alert("The following error occured: " + exception.message);
		}
	}
}
</script>
</head>

<body>
<form action="" name="form1">
	<input type="text" name="txtNum1" size="3" /> factorial is
    <input type="text" name="txtResult" size="25" /><br />
    <input type="button" value="Calculate Factorial" name="butCalculate" onClick="butCalculate_onclick()" />
</form>

</body>
</html>
Reply With Quote