# Need help with array operations JavaScript

``````const valid1 = [4, 5, 3, 9, 6, 7, 7, 9, 0, 8, 0, 1, 6, 8, 0, 8];
let arrLength = valid1.length - 1;
let emptyarr = [];
let doubleNumber = [];
let minusNine = [];
let notAffected = [];

for (let i = arrLength; i >= 0; i--) {
while (i < arrLength && i >= 0) {
doubleNumber = valid1[i] * 2;
emptyarr.push(valid1[i] * 2);
i--;
}
newArr = emptyarr;
}
console.log(newArr);
console.log("NewArr is an array: " + Array.isArray(newArr));
console.log("*************************************")
let arrLength2 = newArr.length;
for (let j = 0; j < newArr.length; j = j + 1) {
while (j < arrLength2 && j >= 0) {
if (newArr[j] > 9) {
minusNine = newArr[j] - 9;
console.log(minusNine);
} else if (newArr[j] < 9 && newArr[j] >= 0) {
notAffected = newArr[j] * 1;
console.log(notAffected);
}
j++
}
}
console.log("*************************************")
``````

Hello I am stuck with this code. What I am planning to do is taking the array, newArr, and check every element that this array has, if there´s a number greater than 9 the code will substract 9 to that element otherwise it will not affect the elements, in other words the numbers that are less than nine will remain invariable.

after doing the if and else if statements , I got the results that I expect however these numbers must be in a new array but when I try to place these numbers into a single array I get null results.
best wishes

In the last while-loop, you do `console.log(minusNine)`, but you didn’t actually add that to an array nor did you change an existing array.
Similarly, in the last while-loop, you do `console.log(notAffected)`, but you didn’t actually add that to an array nor did you change an existing array.

Also, I don’t understand why you have those nested loops: a while-loop inside a for-loop that loops through the same stuff in two places.

here’s a possible approach:

``````arr1 = [3, 6, 9, 12, 15];
arr2 = [];
for(let i = 0; i < arr1.length; i++) {
if (arr1[i] > 9) {
arr2.push(arr1[i] - 9);
}
else {
arr2.push(arr1[i]);
}
}
``````

I guess a shorter version might be:

``````arr1 = [3, 6, 9, 12, 15];
arr2 = arr1.map( x => (x > 9) ? (x - 9) : x );
``````

Thank you very much for giving your feedback Janbazant I appreciate your time and knowledge.

The problem that I have is this:
the array called newArr=[0,16,12,2,0,16,0,18,14,14,12,6,10,8]

based on the newArr I must substract 9 to the elements(numbers) that are
greater than 9 and the rest of the numbers will be invariable.
after doing the the coding on the newArr, I obtain this set of
numbers : 0,7,3,2,0,7,0,9,5,5,3,6,1,8 however I am having troubles
when I add minusNine and notAffected because in
minusNine I obtain this array=[null,7,3,null,null,7,null,9,5,5,3,null,1,null]

the array notAffect=[0,null,null,2,0,null,0,null,null,null,null,6,null,8]
when I try to add these two arrays I obtain a resulting array of only null elemts.

I did a test with two arrays in order to know if it works adding a number with null gives me
the number instead of null and it worked!
in the first array I put a number between 1 and 9 and in the second array I put a null value
and the result array contained the number.
for example:

let a=;
let b=[null];
let c=a+b;
console.log(c);
output: 2

what I want to do is putting the above set of numbers into an array after
adding minusNine with notAffected and get this result

[0,7,3,2,0,7,0,9,5,5,3,6,1,8]

You can combine those arrays into a new array using a loop (with an if statement inside):

``````// minus9 = [null,7,3,null,null,7,null,9,5,5,3,null,1,null];
// notAffected = [0,null,null,2,0,null,0,null,null,null,null,6,null,8];

let anotherArray = []
let length = notAffected.length;

for (let i = 0; i < length; i++) {
if (minus9[i] === null) {
anotherArray.push(notAffected[i]);
}
else {
anotherArray.push(minus9[i]);
}
}

// anotherArray = [0,7,3,2,0,7,0,9,5,5,3,6,1,8];
``````

You can also do this using `+` (because `null` acts like `0` for mathematical operations).

``````let anotherArray = []
let length = notAffected.length;

for (let i = 0; i < length; i++) {
anotherArray.push( minus9[i] + notAffected[i] );
}
``````
more stuff

Actually, I see no reason to have 2 separate arrays at all;
you can just change the values in the array you already have.

``````let arrLength2 = newArr.length;
// update the values of newArr (does not create a new array)
for (let j = 0; j < arrLength2; j++) {
if (newArr[j] > 9) {
newArr[j] = newArr[j] - 9;
//} else {
// do nothing
}
}
``````
1 Like

Thank you so much for your knowledge shared on my inquiries, I was trying several ways in order to learn more about loops applied into arrays in JavaScript, and the last solution that you provided with me was noteworthy.