0

I'm a total JS noobie, and I'm trying to make it so that after one input is entered all of the numbers between 0 and that input are added up, including the input. Here is the basic pseudo code I'm trying to accomplish but i can't figure it out?

get count from user

loop up to count{

     add current number to sum

}

display sum

display breakline

loop up to count with a different loop type{

    add current number to sum

}

display sum
Im0rtality
  • 3,463
  • 3
  • 31
  • 41

4 Answers4

2

The fastest way to do this is by adding a sequence (looping not required):

let sum = input * (input + 1) / 2

See this link for more information.

Adil B
  • 14,635
  • 11
  • 60
  • 78
Ruth N
  • 151
  • 2
  • 7
1

You could try something like this:

// input would be the number that the user will enter
var input = 10;

// the starting number is 0.
var temp = 0;

// the sum.
var sum = 0;

// while the temp would be less or equal to the input,
// then we will add the temp to the sum and we increase it by 1.
// This way, when temp would be equal to imput+1, the statement in 
// the while loop would be false, and the statements in the while loop 
// wouldn't be executed. At this moment, the sum would hold the sum of 
// the integers numbers in the range [0,input].
while(temp<=input)
{
    sum+=temp;
    temp++;
}

As for the part of displaying is totally depends on where you want to display the result. If you want to display it on the console.

console.log(sum);

If you want to display it on an alert box.

alert(sum);

etc.

Christos
  • 53,228
  • 8
  • 76
  • 108
0
var sum = 0;
var input = getCountFromUser(); //However you do it.
for (var j = 1; j <= input; j++) {
  sum += j;
}

Or, somewhat shorter:

var sum = 0, input = getCountFromUser(), j = 1;
for (; j <= input;) {
  sum += j++;
}
Liglo App
  • 3,719
  • 4
  • 30
  • 54
  • I tried doing this, and it's adding the numbers different from what i need. Example: when the input is 5, it alerts 1, then 3, then 6, then 10, then 15. – Phillip Charles Jackson Dec 07 '14 at 19:20
  • But when I'm trying to accomplish is for it to alert the sum of (1+2+3+4+5) once. Maybe the alert function is wrong? here is me Jsfiddle http://jsfiddle.net/vj4b9jut/2/ – Phillip Charles Jackson Dec 07 '14 at 19:22
  • You do not have to put `alert()` within the loop. Just place it behind - after the sum has been calculated. – Liglo App Dec 07 '14 at 19:47
0

Here it is the full code displaying you want:

<HTML>
<HEAD>
<TITLE>JavaScript Form - Input Text Field</TITLE>
<SCRIPT Language="JavaScript">
<!--//
function showAndClearField(frm){
  if (frm.num.value == "")
      alert("Hey! You didn't enter anything!")
  else{
    var result = 0;
  var input = frm.num.value; //However you do it.
  for (var i = 1; i <= input; i++) {
    result += i;
  }
      alert("Result: " + result)
  }
  frm.firstName.value = ""
}
//-->
</SCRIPT>
</HEAD>
<BODY>
<FORM NAME="test">
<H2>Enter a number.</H2>
<INPUT TYPE="Number" NAME="num"><BR><BR>
<INPUT TYPE="Button" Value="Show and Clear Input" onClick="showAndClearField(this.form)">
</P>
</FORM>
</BODY>
</HTML>
MQ87
  • 1,008
  • 1
  • 13
  • 30