const random = Math.floor(Math.random() * 100);
const array = ;
for (var i = 0; i <= random; i++) {
array.push(i);
}
console.log(array);
let sumArray = ;
for (var n = 0; n < random; n++) {
sumArray += array.push(n);
}
console.log(sumArray);
this is my code in Javascript
the top coed works fine but the base does not complete it well…
i was trying to generate a random number and create an array from it that will begin at 1 and end at that random number
then i will sum the numbers in th array using .reduce() and divide the answer by the random number(which is the highest number in the array)…
If you add 1 it will include 100 in the array (potentially), and will not include 0.
Math.floor(Math.random() * 100) + 1
or
Math.floor(Math.random() * 100 + 1)
Either way comes out the same.
There is a bit of a type match problem there. Isn’t sumArray an array? And, isn’t array.push(n) a statement?
There is a strong possibility that array.push() does return a value, the new length of the array, so in that sense it would be an expression. The .reduce you are working toward could be to set sumArray to a zero value, rather than an empty array. Then the above line would work, accordingly.
> array = []
<- []
> sum = 0
<- 0
> for (let i = 1; i < 101; i++) {
sum += array.push(i)
}
<- 5050
Check out this topic to allow other forum members to better help you. It might also be helpful to start a new topic since this initial concern in this one was in JS and has been solved.