This is what is understand from your question and try to solve your issue.
const arr = [1441,1468,1445,1512,1621,1000];
let finalAnswer = [];
for(let i = 1;i<arr.length;i++)
{
finalAnswer.push(arr[i-1] - arr[i]);
}
console.log(finalAnswer);
Answer from Ferin Patel on Stack OverflowVideos
This is what is understand from your question and try to solve your issue.
const arr = [1441,1468,1445,1512,1621,1000];
let finalAnswer = [];
for(let i = 1;i<arr.length;i++)
{
finalAnswer.push(arr[i-1] - arr[i]);
}
console.log(finalAnswer);
Not totally sure what you want as a final result. Maybe this would work for you.
getStats()
.then(stats => {
let eloArr = [stats[0].elo];
console.log(eloArr[0]);
let total = stats[0].elo;
for (let i = 1; i < stats.length; i++) {
eloArr.push(stats[i].elo);
console.log(stats[i].elo);
total -= stats[i].elo;
}
console.log(total);
});
The problem with % is that it is a remainder operator with truncated division, not a modulo one with floored division. When the divisor (i-1) becomes negative, so does the result. You can use
if (--i < 0) i = stuff.length - 1;
or
i = (i + stuff.length - 1) % stuff.length;
instead (which only work for input values of i in the expected range, though)
If you want next() to increment i between 0 and 2 and prev() to decrement between 2 and 0 you can use the following:
next() {
this.props.dispatch(increaseCounter());
i = Math.min(i + 1, stuff.length - 1);
}
prev() {
this.props.dispatch(decreaseCounter());
i = Math.max(i - 1, 0);
}
For some number y and some divisor x compute the quotient (quotient)[1] and remainder (remainder) as:
const quotient = Math.floor(y/x);
const remainder = y % x;
Example:
const quotient = Math.floor(13/3); // => 4 => the times 3 fits into 13
const remainder = 13 % 3; // => 1
[1] The integer number resulting from the division of one number by another
I'm no expert in bitwise operators, but here's another way to get the whole number:
var num = ~~(a / b);
This will work properly for negative numbers as well, while Math.floor() will round in the wrong direction.
This seems correct as well:
var num = (a / b) >> 0;
Note: Only use ~~ as a substitution for Math.trunc() when you are confident that the range of input falls within the range of 32-bit integers.
Based on your constraints, which are:
- Single loop variable
- No restarting of iteration over large arrays
You could calculate your looping step sizes first, then go over the array only once.
const mushedArr = [];
const remainderSize = arr.length % size
const numberOfChunks = Math.floor(arr.length / size);
let remainderStepSize = Math.floor(remainderSize / numberOfChunks);
I'm going to define the remainder with negative index slicing, so there has to be a guard against calculating a remainder even when division is exact.
let remainder = remainderSize ? arr.slice(-remainderSize) : []
Second edge case is where the remainder is smaller than the chunk size, which would cause my step size to evaluate to 0 and not loop through the remainder properly.
if(remainderStepSize === 0) remainderStepSize ++;
Finally, loop over the array and the remainder:
let i = 0;
while(i < numberOfChunks){
let remainderPortion = remainder
.slice(i*remainderStepSize, i*remainderStepSize+remainderStepSize);
let arrayPortion = arr.slice(i*size, i*size+size);
mushedArr.push([...arrayPortion, ...remainderPortion]);
i++;
};
return mushedArr;
};
Variable names here could be shorter, and the slice can be simplified to
something like arr.slice(size*i, size*(i+1), but the idea is to loop over the array and copy the items in sizes that are equal to the chunk size.
Testing for both inputs in your question yielded:
Calling function with input: [A,B,C,D,E,F,G,H,I,J,K,L,M,N]
chunk size: 5
[ [ 'A', 'B', 'C', 'D', 'E', 'K', 'L' ],
[ 'F', 'G', 'H', 'I', 'J', 'M', 'N' ] ]
Calling function with input: [A,B,C,D,E,F,G,H]
chunk size: 3
[ [ 'A', 'B', 'C', 'G' ], [ 'D', 'E', 'F', 'H' ] ]
For solution
(a) - just accept a shorter array at the end
code can be very short
function mushInLittleArray(arr, size) {
let resultArr = [];
for (let i = 0; i < (arr.length / size); i++ ) {
resultArr.push( arr.slice( i * size, (i+1) * size ) );
}
return resultArr;
}
arr.push(i); pushes the index position of the number within the numbers array. Change it to arr.push(numbers[i]); so it'll be the actual number at that index position of the numbers array.
function divisibleBy(numbers, divisor){
arr = []
for(let i = 0 ; i <= numbers.length; i++) {
if(numbers[i] % divisor === 0) {
arr.push(numbers[i]);
}
}
return arr
}
console.log(divisibleBy([1,2,3,4,5,6], 2))
You're pushing the indexes, and not the elements. Change it to:
function divisibleBy(numbers, divisor){
arr = []
for(let i = 0 ; i <= numbers.length; i++) {
if(numbers[i] % divisor === 0) {
arr.push(numbers[i]) // Push the element at index i
}
}
return arr
}