In general, in the following code, my goal was to store the values of the array from end to begining in a new array, then display it as a number, Of course, in the meantime, I doubled the value of each array cell.
I want to know is there an easier way?
let firstArray = [1, 2, 3];
const secondArray = [];
firstArrayLength = firstArray.length;
let i = 0;
let y = 0;
while (i < firstArrayLength) {
secondArray.push(firstArray.pop());
i++;
};
secondArrayLength = firstArrayLength;
while (y < secondArrayLength) {
if (y == 0) {
secondArray[y] = secondArray[y] * 100;
secondArray[y] = secondArray[y] * 2;
}
if (y == 1) {
secondArray[y] = secondArray[y] * 10;
secondArray[y] = secondArray[y] * 2;
}
if (y == 2) {
secondArray[y] = secondArray[y] * 1;
secondArray[y] = secondArray[y] * 2;
}
y++;
}
let sum = 0;
for (let i = 0; i < secondArray.length; i++) {
sum = sum + secondArray[i];
}
console.log(sum);
secondArrayother than sum its elements. There's no need for that. You can getsumalso with just the elements infirstArray.console.log(sum))