Slip 10 - A) Write a JavaScript Program to read a number from user, store its factors into the array and display that array. (Handle onClick Event)

Solution:

<html>
18
<head>
<script language="JavaScript">
function factors()
{
var n=form1.txtNum.value;
var num_factors = [], i;
for (i = 1; i <= Math.floor(Math.sqrt(n)); i += 1)
if (n % i === 0)
{
num_factors.push(i);
if (n / i !== i)
num_factors.push(n / i);
}
alert("factors are:"+" " + num_factors);
}
</script>
</head>
<body>
<form name="form1">
Enter the Number :
<input type="text" name="txtNum">
<input type="button" value="Display" onclick="factors();">
</form>
</body>
</html>

Post a Comment

0 Comments