0

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);

2 Answers 2

1

This should work but I really wonder what your code does. This is one of the weirdest code I have ever seen.

EDIT: Your dataset counting in the post was 1 based but it was 0 based in the code for error printing. I made all of them 1 based.

const data = [[true, false], ["error", true, 5, 10, false], [true, false], [4, 5, 6], ["error, false"]];

const printForecast = function (input) {
    for (let i = 0; i < input.length; i++) {
        let count = 0;
        let str = "";

        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+1}! (message from me)`);
        } else {
          console.log(`dataset ${i+1}: ${str}`);
        }
    }
}

printForecast(data);

Sign up to request clarification or add additional context in comments.

1 Comment

This is exactly what I was looking for - thanks so much! Yeah, I know it may look odd atm, but I'm just practicing the fundamentals of JS before I move on to more advanced stuff.
1

You can try this or If you want answers at end you can store each string str in array and print at last

const data = [
  [true, false],
  ["error", true, 5, 10, false],
  [true, false],
  [4, 5, 6],
  ["error, false"],
];

const printForecast = function (input) {
    
    for (let i = 0; i < input.length; i++) {
        let count = 0;
        let str = "";

    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)`
      );
    else {
        console.log(`dataset ${i+1}: ${str}`);
    }

}

};
printForecast(data);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.