Jump to content

help :(


SnakesBite101

Recommended Posts

can someone please show me how just 1 of these functions are done. ill try to work out the other: Define a function sum() and a function multiply() that sums and multiplies (respectively) all the numbers in an array of numbers. For example, sum([1,2,3,4]) should return 10, and multiply([1,2,3,4]) should return 24. why can't i work out these simple tasks? :(

Edited by SnakesBite101
Link to comment
Share on other sites

Look into arrays, the for loop and keep in mind that a variable can be part of its own assignment, like:

x = x operator y

which BTW can also be written as

 x operator= y

Link to comment
Share on other sites

i did the sum to get 10. please tell me if this was a good or bad method: <!DOCTYPE html> <html> <head> <script type="text/javascript"> function sum(){ var answer = [1,2,3,4]; for (x in answer){ document.write(answer[x] += x); } } </script> </head> <body> <button onclick="sum();"> addition</button> </body> </html> time to try the multiply:(

Link to comment
Share on other sites

The algorithm for summation... or any accumulation of data really... is to have a variable with a default value, and then perform the operation over it, using the notation above.So, you'd have

var sum=0;

and you'd have an array with all values you want to sum, e.g.

var numbers = [1,2,3,4];

Once you have that, you simply loop over the array and add each value to the sum variable. You only output the result once you're done, not during the loop.Think about how would you do this if you were doing it on paper. Imagine you could only perform one adding at a time, and that you can only pick a number if you've labelled the place on the paper that number is at.I don't know what else to say, other than write down the code itself (which does exactly what I just described), but that's not the point here, right?(BTW, for multiplication, your default value will be 1, since 1 multiplied by any number is the number itself, whereas 0 multiplied by any number is always 0)

Link to comment
Share on other sites

Hey Snakes, I was able to add up the following like so:

<!DOCTYPE html><html><head><script type="text/javascript">function sum(){     var result = 0;     var answer = [1,2,3,4];     for (var i=0; i<answer.length; i++)     {          result = answer[i] + result;      }      alert(result);}</script></head><body><button onclick="sum();"> addition</button></body></html>

Instead of document.write I just alerted the result and used a regular for loop. Hopefully this helps.

Link to comment
Share on other sites

i cheated and looked at Don's code. not sure why i couldnt work this out, but at least i understand the code. thank you Don and boen robot for your efforts to help me

Edited by SnakesBite101
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...