As someone who is just starting out with coding, I am currently focused on practicing loops and arrays. One of my exercises involves working with an array that contains multiple sub arrays, each of them consisting of pairs of strings. My goal is to extract and isolate each individual string using a set of nested for loops.
const pairs = [['Blue', 'Green'],['Red', 'Orange'],['Pink', 'Purple']];
//Using nested arrays to access each string from the array
function getString(arr){
//First loop to iterate through the list of arrays
for (let i = 0; i < arr.length; i++){
console.log(i , arr[i]);
//Assigning each sub array to a new variable for iteration
subArr = arr[i];
}
//Second loop to get each string from the sub arrays
for (let j = 0; j < subArr.length; j++){
console.log(j, arr[j]);
}
};
console.log(getString(pairs));
The issue I am facing is that the output of the last loop is showing ['Pink', 'Purple'] as a whole, rather than each individual color extracted from the nested loops.
Can anyone point out where I might be going wrong in my approach?
- Mirii