1

I have an array of randomly generated numbers. I want to create a function that divides all those numbers. Basically, assuming that I have 5 numbers in the array [5, 7, 6, 8, 2], I want the output to be equal to 5 / 7 / 6 /8 / 2

array = [5, 7, 6, 8, 2];

var total = 1;    
for(var i = 0; i < array.length; i++) {
total = array[i] / total; 
}

return total;

This is what I did so far, but the output isn't the correct one. Any idea where I am doing wrong?

1
  • that output doesn't make sense when compared to your attempt. Should clarify expected results. Commented Jan 7, 2016 at 0:42

4 Answers 4

8

You've basically got your math backwards. With your approach, you want to progressively divide total, rather than progressively dividing by total.

var total = array[0];
for (var i = 1; i < array.length; i++) 
    total = total / array[i];
return total;
Sign up to request clarification or add additional context in comments.

3 Comments

I've tried it but it's still doesn't return the correct result. That's because the first division is made between total which is equal to array[0] and array[0] itself. Any idea how to fix it?
This code never divides array[0] by itself, as the loop starts at i = 1. Using your sample data, this code provides an output of 0.00744 which is correct (5/6/7/8/2)
Oh, now I see. I didn't see that you changed var i = 1. You are right! It's works now! Awesome, thank you!
5

Try this. It uses the array's reduce method along with es6 arrow functions which makes it a one liner. You can use babel to convert es6 syntax to es5.

var arr = [5, 7, 6, 8, 2];

arr.reduce((prev,curr) => prev/curr);

ES5 version:

var arr = [5, 7, 6, 8, 2];

arr.reduce(function(prev, curr) {
  return prev/curr;
});

As you can see in the docs, Array.reduce() will reduce a list of values to one value, looping through the list, applying a callback function and will return a new list. In that callback you can access four parameteres:

previousValue: If you pass an argument after callback function, previousValue will assume that value, otherwise it'll be the first item in array.

currentValue: The current value in the loop.

index: Index of the current item on loop.

array: The list

2 Comments

I like that -- Here is some documentation for the OP as well: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
@RobG Well many have answered that. Im simply suggesting an alternative solution thats terse. As for arrow functions, there are transpilers like which people can use to convert to es5. I've added links to documentation for more explanation
0

Well you messed up with the total, you kept dividing each new number with the result. You just have to flip the '/' operators.

array = [5, 7, 6, 8, 2];

var total = array[0];    
for(var i = 1; i < array.length; i++) {
total = total/array[i]; 
}

return total;

1 Comment

Except the initial value should be array[0] not 1 per CollinD's answer. ;-)
-3

Try this ...

array = [5, 7, 6, 8, 2];
var total = array[0];
for(var i = 1; i < array.length; i++) {
    total = array[i] / total; 
}
return total;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.