About the Task:
- This task involves working with two arrays:
arrNumber
, which is a number array, andarrString
, which is a string array. - The goal is to create a list of objects using the values from
arrNumber
, where the object keys are numbers or characters and the values are the count of repeated occurrences.
Issue at Hand:
- The problem arises when trying to reuse the
arrDuplicateValues
array. During the second iteration, the array appears to have a length of 0 even though it contains items.
Access the code here for testing purposes
var arrNumber = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 2, 4, 6, 8, 0 ,4, 6, 8, 4];
var arrString = ["a", "e", "i", "o", "u", "a", "i", "u", "i"];
var arrDuplicateValues = new Array();
// First iteration: For numbers
arrNumber.forEach(x => {
arrDuplicateValues[x] = (arrDuplicateValues[x] || 0) + 1;
});
console.log(arrDuplicateValues); // Outputs successfully
// Second iteration: For strings
arrDuplicateValues = new Array();
arrString.forEach(x => {
arrDuplicateValues[x] = (arrDuplicateValues[x] || 0) + 1;
});
console.log(arrDuplicateValues); // Does not output as expected
console.log(arrDuplicateValues["i"]);