Slip 27 - A) Write a JavaScript function to compute the sum of factors of a input number

Solution:


<html>

<body>

<script>

function sum() {

var sum = 0, i, str,num1;

num1 = number(document.getelementbyid("n").value);

 str = num1.tostring();

sum = 0;

for (i = 0; i < str.length; i++) {

sum += parseint(str.charat(i), 10);

}

window.alert(sum);

}

</script>

</head>

<body>

enter a number : <input type="text" id="n"><br/>

<button onclick="sum()"> sum is </button>

<body>

</html>

Post a Comment

0 Comments