Help needed in java script [Resolved]

Posted by Crniranjanraj under JavaScript on 11/22/2015 | Points: 10 | Views : 1598 | Status : [Member] | Replies : 14
Write a JavaScript code to find the sum of N natural Numbers. (Use user-defined function)




Responses

Posted by: Rajnilari2015 on: 11/22/2015 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 50

Up
1
Down

Resolved
Try this (using for loop)

<html>

<head>
<script type="text/javascript">
<!--
function SumofNNaturalNumbers() {

var N=10;
var sum = 0;
for(var i=1;i<=N;i++)
sum+=i;
alert("Sum Of N Natural Numbers : " + sum);
}
//-->
</script>
</head>
<body>
<input type="button" onclick="SumofNNaturalNumbers()" value="Sum Of N Natural Numbers" />
</body>
</html>


--
Thanks & Regards,
RNA Team

Crniranjanraj, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajnilari2015 on: 11/22/2015 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 50

Up
0
Down

Resolved
Using While loop

<html>
<head>
<script type="text/javascript">
<!--
function SumofNNaturalNumbers() {

var N=10;
var sum = 0;
var i=1;

while(i<=N) {
sum+=i;
i++;
}
alert("Sum Of N Natural Numbers : " + sum);
}
//-->
</script>
</head>
<body>
<input type="button" onclick="SumofNNaturalNumbers()" value="Sum Of N Natural Numbers" />
</body>
</html>


--
Thanks & Regards,
RNA Team

Crniranjanraj, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajnilari2015 on: 11/22/2015 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 50

Up
0
Down

Resolved
Using the mathematical formula (this is the optimized one since this yields O(1) always)

Sum of N Natural Numbers=(n*(n+1))/2

<html>
<head>
<script type="text/javascript">
<!--
function SumofNNaturalNumbers() {
var N=10;
var sum = 0;
sum = (N * (N+1)) /2;
alert("Sum Of N Natural Numbers : " + sum);
}
//-->
</script>
</head>
<body>
<input type="button" onclick="SumofNNaturalNumbers()" value="Sum Of N Natural Numbers" />
</body>
</html>


--
Thanks & Regards,
RNA Team

Crniranjanraj, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajnilari2015 on: 11/22/2015 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 50

Up
0
Down

Resolved
Using Map and Foreach function

<html>
<head>
<script type="text/javascript">
<!--
function SumofNNaturalNumbers() {
var N=10;
var sum = 0;

Array.apply(null,Array(N))
.map(function(_, index){
return index+1;
})
.forEach(function(number) {
sum += number;
});
alert("Sum Of N Natural Numbers : " + sum);
}
//-->
</script>
</head>
<body>
<input type="button" onclick="SumofNNaturalNumbers()" value="Sum Of N Natural Numbers" />
</body>
</html>


--
Thanks & Regards,
RNA Team

Crniranjanraj, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajnilari2015 on: 11/22/2015 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 50

Up
0
Down

Resolved
Using Map and Reduce function
<html>
<head>
<script type="text/javascript">
<!--
function SumofNNaturalNumbers() {
var N=20;
var sum = 0;

var sum = Array.apply(null,Array(N))
.map(function(_, index){
return index+1;
})
.reduce(function(a,b){return a + b})
alert("Sum Of N Natural Numbers : " + sum);
}
//-->
</script>
</head>
<body>
<input type="button" onclick="SumofNNaturalNumbers()" value="Sum Of N Natural Numbers" />
</body>
</html>


--
Thanks & Regards,
RNA Team

Crniranjanraj, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajnilari2015 on: 11/22/2015 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 50

Up
0
Down

Resolved
Using recursive function

<html>
<head>
<script type="text/javascript">
<!--
function SumofNNaturalNumbers() {

var N = 10;
sum = SumofNNumbers(N);
alert("Sum Of N Natural Numbers : " + sum);
}

//using recursive function
function SumofNNumbers(n){
if(n==1) return n;
else return (n + SumofNNumbers(n-1));
}
//-->
</script>
</head>
<body>
<input type="button" onclick="SumofNNaturalNumbers()" value="Sum Of N Natural Numbers" />
</body>
</html>


--
Thanks & Regards,
RNA Team

Crniranjanraj, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajnilari2015 on: 11/22/2015 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 25

Up
0
Down
Using Do..while loop

<html>
<head>
<script type="text/javascript">
<!--
function SumofNNaturalNumbers() {

var N=10;
var sum = 0;
var i=1;

do{
sum+=i;
i++; }while(i<=N);

alert("Sum Of N Natural Numbers : " + sum);
}
//-->
</script>
</head>
<body>
<input type="button" onclick="SumofNNaturalNumbers()" value="Sum Of N Natural Numbers" />
</body>
</html>


--
Thanks & Regards,
RNA Team

Crniranjanraj, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajnilari2015 on: 11/22/2015 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 25

Up
0
Down
Using for..in loop

<html>
<head>
<script type="text/javascript">
<!--
function SumofNNaturalNumbers() {
var N=10;
var sum = 0;
var numbers = [];
var count = 1;

while ( count <= N+1 ) numbers.push( count++ );
for(var number in numbers) { sum += parseInt(number);}
alert("Sum Of N Natural Numbers : " + sum);
}
//-->
</script>
</head>
<body>
<input type="button" onclick="SumofNNaturalNumbers()" value="Sum Of N Natural Numbers" />
</body>
</html>


--
Thanks & Regards,
RNA Team

Crniranjanraj, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajnilari2015 on: 11/22/2015 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 25

Up
0
Down
Using Map, Reduce and Arrow function

<html>
<head>
<script type="text/javascript">
<!--
function SumofNNaturalNumbers() {

var N = 10;
var sum = Array.apply(null,Array(N))
.map(function(_, index){
return index+1;
}).reduce((a, b) => a + b);

alert("Sum Of N Natural Numbers : " + sum);
}
//-->
</script>
</head>
<body>
<input type="button" onclick="SumofNNaturalNumbers()" value="Sum Of N Natural Numbers" />
</body>
</html>


--
Thanks & Regards,
RNA Team

Crniranjanraj, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajnilari2015 on: 11/22/2015 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 25

Up
0
Down
Using for loop in a reverse way

<html>

<head>

<script type="text/javascript">

<!--

function SumofNNaturalNumbers() {



var N=10;

var sum = 0;

for(var i=N;i>=0;i--)
sum+=i;

alert("Sum Of N Natural Numbers : " + sum);

}

//-->

</script>

</head>

<body>

<input type="button" onclick="SumofNNaturalNumbers()" value="Sum Of N Natural Numbers" />

</body>

</html>


--
Thanks & Regards,
RNA Team

Crniranjanraj, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajnilari2015 on: 11/22/2015 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 25

Up
0
Down
Using while loop in a reverse way

<html>

<head>

<script type="text/javascript">

<!--

function SumofNNaturalNumbers() {

var N=10;
var sum = 0;

while(N>0) {
sum+=N;
N--;
}
alert("Sum Of N Natural Numbers : " + sum);

}

//-->

</script>

</head>

<body>

<input type="button" onclick="SumofNNaturalNumbers()" value="Sum Of N Natural Numbers" />

</body>

</html>


--
Thanks & Regards,
RNA Team

Crniranjanraj, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajnilari2015 on: 11/22/2015 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 25

Up
0
Down
Using do..while loop in a reverse way

<html>

<head>

<script type="text/javascript">

<!--

function SumofNNaturalNumbers() {

var N=10;
var sum = 0;

do{
sum+=N;
N--; }while(N>0);

alert("Sum Of N Natural Numbers : " + sum);

}

//-->

</script>

</head>

<body>

<input type="button" onclick="SumofNNaturalNumbers()" value="Sum Of N Natural Numbers" />

</body>

</html>


--
Thanks & Regards,
RNA Team

Crniranjanraj, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Crniranjanraj on: 11/23/2015 [Member] Starter | Points: 25

Up
0
Down
thanks..it's working

Crniranjanraj, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajnilari2015 on: 11/29/2015 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 25

Up
0
Down
Glad that it has helped you (:

--
Thanks & Regards,
RNA Team

Crniranjanraj, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response