Javascript - Trying counta sum from 1 to thenumber that user entered - Stack Overflow

I am very new to javascript, and have limited knowledge. I have just grasped the concept of hello world

I am very new to javascript, and have limited knowledge. I have just grasped the concept of hello world. At the moment, my code adds FirstNumber and SecondNumber together to give the result. I would like it to do the following:

I am trying to make a program where FirstNumber is pre-defined as 1 and SecondNumber is done by user input. The javascript should count a sum from 1 to the number that should be entered. For example, if the user entered 5, the program should count the sum from 1 to 5 (1 + 2 + 3 + 4 + 5) which will be 15. I was told to maybe use an array, although I'm not sure.

Here is my HTML code:

<!DOCTYPE html>
<html>
<head>
<title>Sum of numbers</title>
<script type="text/javascript">
function sum()
{
    var FirstNumber = 1;
    var SecondNumber = document.getElementById('txtSecondNumber').value;
    alert(parseInt(FirstNumber) + parseInt(SecondNumber));
}
</script>
</head>
<body>
Please enter a number:<input id="txtSecondNumber" type="text" />
<input id="btnAdd" type="button" value="Add" onclick="sum();"/>
</body>
</html>

Could someone help?

Thanks :)

I am very new to javascript, and have limited knowledge. I have just grasped the concept of hello world. At the moment, my code adds FirstNumber and SecondNumber together to give the result. I would like it to do the following:

I am trying to make a program where FirstNumber is pre-defined as 1 and SecondNumber is done by user input. The javascript should count a sum from 1 to the number that should be entered. For example, if the user entered 5, the program should count the sum from 1 to 5 (1 + 2 + 3 + 4 + 5) which will be 15. I was told to maybe use an array, although I'm not sure.

Here is my HTML code:

<!DOCTYPE html>
<html>
<head>
<title>Sum of numbers</title>
<script type="text/javascript">
function sum()
{
    var FirstNumber = 1;
    var SecondNumber = document.getElementById('txtSecondNumber').value;
    alert(parseInt(FirstNumber) + parseInt(SecondNumber));
}
</script>
</head>
<body>
Please enter a number:<input id="txtSecondNumber" type="text" />
<input id="btnAdd" type="button" value="Add" onclick="sum();"/>
</body>
</html>

Could someone help?

Thanks :)

Share Improve this question asked Feb 17, 2015 at 12:34 AdamAdam 751 silver badge6 bronze badges
Add a ment  | 

8 Answers 8

Reset to default 1

What you're looking for is a for loop

for (var i = FirstNumber ;i < SecondNumber ; i++){
    // do what ever you want in here
    // like adding i to the total
}

For the simple case of adding sequential number you dont need to loop at all:

1+2+3+4+5+...+n = n(n+1)/2

Formula from: http://en.wikipedia/wiki/1_%2B_2_%2B_3_%2B_4_%2B_%E2%8B%AF

function sum() {
    var num = document.getElementById('txtSecondNumber').value;
    var sum = (num*(num+1))/2;
    alert(sum);
}

While an array might help you with what you want, the important bit you're looking for is a for loop. You don't indicate your programming background or if JavaScript is your first language, but a for loop is a basic programming construct that has a starting condition, an ending condition, a way to change things (so something changes between start and end), and something to do while you're counting.

A simple for loop in JavaScript looks like this:

for( var i=0; i<10; i++ ){
  alert( i );
}

This will pop-up an alert for each number from 0 to 9 inclusive.

In your case, you want to set your start condition to the first number, the end condition to check if you've done the last number (both of these can be variables - not just the constants as I've illustrated), and increment the number. Inside the loop, you'll want to be adding the number to a reference counter.

Try this, you want to loop through all the numbers in between the first and second number and add them to the result

var submit = document.getElementById('submit');
var input = document.getElementById('txtSecondNumber');

function sum() {
    var FirstNumber = 1;
    var SecondNumber = input.value;

    var result = 0;

    for (var i = FirstNumber; i <= SecondNumber; i++) {
      result += i;  
    }

    alert(result);
}

submit.addEventListener('click', sum);

jsFiddle - http://jsfiddle/et8t3bgd/

You can easily count a sum from 1 to any number with a for loop. If you will ever only sum up from 1, you do not need the FirstNumber variable. Otherwise, you can change i = 1 to i = FirstNumber.

var sum;
for (i = 1; i < SecondNumber+1; i++) { 
    sum += i;
}
function sum()
{
var SecondNumber = parseInt(document.getElementById('txtSecondNumber').value);
var result=(SecondNumber *(SecondNumber +1))/2;
alert(result);
}

Formula for sum to 1 to n number is n*(n+1)/2

As per Your Code

<!DOCTYPE html>
<html>
<head>
<title>Sum of numbers</title>
<script type="text/javascript">
function sum()
{
var SecondNumber = parseInt(document.getElementById('txtSecondNumber').value);
var result=(secondnumber*(secondnumber+1))/2;
alert(result);
}
</script>
</head>
<body>
Please enter a number:<input id="txtSecondNumber" type="text" />
<input id="btnAdd" type="button" value="Add" onclick="sum();"/>
</body>
</html>

DEMO

Here

function sum(n) {
  var res = 0, total = 0;
  while ((n--)>0) total += ++res;
  return total;
}

Use a recursive program...

var num=Number(prompt("Enter a number"));
var sum=0;
for(var i=num;i!=0;i--){
      sum+=i;
      }
      console.log(sum)
 //print sum

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745112073a4611906.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信