'why after applying my function to the array it returns the same array?
I'm looping over an array with my function compare to get the max number of temperature from the array but there was no any changes after applying the function to the array temperature.
let temperature = [1, 2, 3, 4, 5, 6, 7, 8, 9, "error"];
function compare(temperature) {
let i = 0;
for (i = 0; i < temperature.length; i++) {
if (typeof temperature[i] === "string") temperature.splice(i, 1);
}
for (let k = i + 1; k < temperature.length; k++) {
if (temperature[i] < temperature[k]) Array.splice(i, 1);
else if (temperature[i] > temperature[k]) Array.splice(k, 1);
}
return temperature;
}
console.log(temperature);
Solution 1:[1]
Check this out!
let temperature = [1, 2, 3, 4, 5, 6, 7, 8, 9, "error"];
function compare(temperature) {
let i = 0;
for (i = 0; i < temperature.length; i++) {
if (typeof temperature[i] === "string")
temperature.splice(i, 1);
}
for (let k = i + 1; k < temperature.length; k++) {
if (temperature[i] < temperature[k])
Array.splice(i, 1);
else if (temperature[i] > temperature[k])
Array.splice(k, 1);
}
return temperature;
}
console.log(compare(temperature));
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | PreethiSamanthaBennet |
