admin管理员组文章数量:1399770
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?
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?
Share Improve this question edited Jan 7, 2016 at 0:37 Willem D'Haeseleer 20.2k10 gold badges68 silver badges103 bronze badges asked Jan 7, 2016 at 0:36 Daniel R.Daniel R. 6491 gold badge12 silver badges28 bronze badges 1- that output doesn't make sense when pared to your attempt. Should clarify expected results. – charlietfl Commented Jan 7, 2016 at 0:42
4 Answers
Reset to default 8You'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;
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
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;
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;
本文标签: Dividing numbers between each other in a Javascript arrayStack Overflow
版权声明:本文标题:Dividing numbers between each other in a Javascript array - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744152601a2593117.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论