I am facing a challenge where I need to convert an array of strings into an array of decimal numbers. The original array of strings is structured like this:
var array = [" 169.70", " 161.84", " 162.16", " 176.06", " 169.72", " 170.77", " 172.74", " 175.73", " 0.00", " 0.00", " 0.00", " 0.00"]
To tackle this issue, I have written a concise function that parses each element of the array as shown below:
function parseFloatArray(array) {
var parsedArray = [];
for (var i = 0; i < array.length ; i++) {
parsedArray[i] = parseFloat(array[i]);
parsedArray.push(parsedArray[i]);
console.log(parsedArray[i]);
};
return parsedArray;
}
Upon inspecting the length of the array, it appears to contain 12 elements. Similarly, individual values are logged in the for loop showing 12 separate entries. However, when checking console.log(parsedArray)
, an additional zero appears at the end of the array despite displaying 12 values initially:
[169.7, 161.84, 162.16, 176.06, 169.72, 170.77, 172.74, 175.73, 0, 0, 0, 0, 0]
I would appreciate any suggestions on how to enhance this function and insights on why the additional zero is appended even though the logs indicate only 12 elements present.