fab5freddy Posted November 1, 2010 Report Posted November 1, 2010 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); }
falkencreative Posted November 1, 2010 Report Posted November 1, 2010 Looks like you are pretty close. The main thing you have to do is add "userEnteredTotal += counter;" within your while loop, and then after the while loop make sure to add secondUserEnteredNumber to userEnteredTotal. And you can't have uppercase letters in functions, so you'd better fix that.
fab5freddy Posted November 2, 2010 Author Report Posted November 2, 2010 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); }
falkencreative Posted November 2, 2010 Report Posted November 2, 2010 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.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now