Jump to content
Killersites Forums

Adding Numbers


Recommended Posts

This is a very basic question I need to find the sum of all numbers based on what the user inputs. For example if the user inputs 5 and 10 I need to come up with an answer of 45. 5 + 6 + 7 etc. all the way to and including 10.

I have a start to the statement but am unsure where to go from there. I am just starting to learn javascript, so if you need more information just let me know. Here is me code:

 

function exercise7Part2() {

//Variables

var firstUserEnteredNumber;

var secondUserEnteredNumber;

var counter;

var userEnteredTotal;

 

//User Input

firstUserEnteredNumber = prompt("Enter a number: ");

secondUserEnteredNumber = prompt("Enter another number: ");

 

//Assignments

firstUserEnteredNumber = Number(firstUserEnteredNumber);

secondUserEnteredNumber = Number(secondUserEnteredNumber);

counter = firstUserEnteredNumber;

 

//Output

while (counter < secondUserEnteredNumber) {

document.write(counter + " + ");

counter++;

 

}

document.write(secondUserEnteredNumber + " = " + userEnteredTotal);

}

Link to post
Share on other sites

That worked perfect. I now have to do the same thing with multiplication in a for statement and can not get it to work. Here is the code I have so far. I basically copied everything from the one before but just changed it a little bit. Changing the += to a *= makes it work in the while loop. However in the for loop I can not get it to work. Thanks again for help with the first part.

 

function exercise8part2() {

// PART 2: YOUR CODE STARTS AFTER THIS LINE

//Variables

var firstUserEnteredNumber;

var secondUserEnteredNumber;

var counter;

var userEnteredTotal;

 

//User Input

firstUserEnteredNumber = prompt("Enter a number: ");

secondUserEnteredNumber = prompt("Enter another number: ");

 

//Assignments

firstUserEnteredNumber = Number(firstUserEnteredNumber);

secondUserEnteredNumber = Number(secondUserEnteredNumber);

 

//Output

for (counter = firstUserEnteredNumber; counter < secondUserEnteredNumber; counter++) {

userEnteredTotal = firstUserEnteredNumber *= counter;

document.write(counter + " * ");

 

}

document.write(secondUserEnteredNumber + " = " + userEnteredTotal);

}

Link to post
Share on other sites

A couple key things that may help:

 

-- keep in mind that since you are doing multiplication, userEnteredTotal needs to be set to 1 initially, rather than 0. (since 0 times x is always going to be 0.)

 

-- "firstUserEnteredNumber *= counter;" is the same as "firstUserEnteredNumber = firstUserEnteredNumber * counter;", so your line "userEnteredTotal = firstUserEnteredNumber *= counter;" actually could read "userEnteredTotal = firstUserEnteredNumber = firstUserEnteredNumber * counter;" which doesn't make much sense (too many = signs.)

 

Seems to me you'd want to set userEnteredTotal to 1 initially, and then something like "userEnteredTotal *= counter;"

 

-- Also, make sure to double check your math. Your for loop above skips multiplying by the final higher number (if you need to multiply 2*3*4, your for loop would skip the *4 part and leave you with incorrect results.) Watch for a similar issue in your exercise8part1() as well.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...