I got the code below, which works perfectly fine. However, I'd also like to add a differentiator for each nested array inside data variable. For example, my code currently outputs (aside from my own error messages) -
... 5°C in 3 days... 10°C in 4 days... 4°C in 1 day... 5°C in 2 days... 6°C in 3 days
What I'd like to happen is this, where my differentiator dataset is printed with a count of a nested array where the °C is coming from -
dataset 2: ... 5°C in 3 days... 10°C in 4 days...
dataset 4: ... 4°C in 1 day... 5°C in 2 days... 6°C in 3 days
I know there are other ways to simplify my str with other methods, but could you tell/show me if it's possible to do it with the existing code?
Many thanks.
const data = [[true, false], ["error", true, 5, 10, false], [true, false], [4, 5, 6], ["error, false"]];
const printForecast = function (input) {
let str = "";
for (let i = 0; i < input.length; i++) {
let count = 0;
for (let b = 0; b < input[i].length; b++) {
if (typeof input[i][b] !== "number") continue;
str = `${str}... ${input[i][b]}°C in ${b + 1} day${(b + 1) === 1 ? "" : "s"}`
count++;
}
if (count === 0) console.error(`No numbers in your nested array # ${i}! (message from me)`);
}
console.log(str);
}
printForecast(data);
EDIT: This is my second attempt:
const data = [[true, false], ["error", true, 5, 10, false], [true, false], [4, 5, 6], ["error, false"]];
const printForecast = function (input) {
let str = "";
for (let i = 0; i < input.length; i++) {
let count = 0;
for (let b = 0; b < input[i].length; b++) {
if (typeof input[i][b] !== "number") continue;
console.log(`dataset ${i + 1}: ${str}... ${input[i][b]}°C in ${b + 1} day${(b + 1) === 1 ? "" : "s"}`);
count++;
}
if (count === 0) console.error(`No numbers in your nested array # ${i}! (message from me)`);
}
}
printForecast(data);