How to print the mathematical values one below the other?

Asked

Viewed 47 times

0

I need to return on the screen the three values below each other and not on each other’s side. I searched a lot, but did not find break values as a subject in the javascript documentation. Follows Code below:

function plusMinus(arr) {
    // Write your code here
    let len = arr.length;
    let positiveCount = 0;
    let negativoCount = 0;
    let zeroCount = 0;
    
    for (let i = 0; i < len; i++) {
        if (arr[i] > 0) {
            positiveCount++; 
        }
        else if (arr[i] < 0) {
            negativoCount++;
        }
        else if (arr[i] == 0) {
            zeroCount++;
        }
    }
    return console.log ((positiveCount / arr.length).toFixed(6), (negativoCount / arr.length).toFixed(6), (zeroCount / arr.length).toFixed(6));

}

You can teach me how to format these values?

  • Here at Stack Overflow you say thank you by voting on the posts. See: How to say thank you in replies?. If one of the answers below solved your problem and there was no doubt left, choose the one you liked the most and mark it as correct/accepted by clicking on the " " that is next to it, which also marks your question as solved. Behold: How and why to accept an answer?

2 answers

3

One possibility is the use of string replacement following this table:

Replacement string Description
%the Emits a Javascript object.
%d or %i Emits an integer number. Example: console.log("Foo %.2d", 1.1), the number will be generated as two significant digits with a 0 on the left:Foo 01
%s Emits a string.
%f Emits a floating point number. Example: console.log("Foo %.2f", 1.1), the output of the number to 2 decimal places:Foo 1.10

Each of these pull the next argument in the parameter list after the string initially provided.

In his example:

function plusMinus(arr) {
  // Write your code here
  let len = arr.length;
  let positiveCount = 0;
  let negativoCount = 0;
  let zeroCount = 0;

  for (let i = 0; i < len; i++) {
    if (arr[i] > 0) {
      positiveCount++;
    } else if (arr[i] < 0) {
      negativoCount++;
    } else if (arr[i] == 0) {
      zeroCount++;
    }
  }
  return console.log("%s\n%s\n%s\n", (positiveCount / arr.length).toFixed(6), (negativoCount / arr.length).toFixed(6), (zeroCount / arr.length).toFixed(6));

}
plusMinus([1, 2, 3])

  • Thank you for the information, and for teaching me how to do it, I was really just missing a little bit of syntax. Thank you all!

1

Taking into account that the values will only be used to be displayed on the console, you can insert a line break in this way:

function plusMinus(arr) {
    // Write your code here
    let len = arr.length;
    let positiveCount = 0;
    let negativoCount = 0;
    let zeroCount = 0;
    
    for (let i = 0; i < len; i++) {
        if (arr[i] > 0) {
            positiveCount++; 
        }
        else if (arr[i] < 0) {
            negativoCount++;
        }
        else if (arr[i] == 0) {
            zeroCount++;
        }
    }
    return console.log ((positiveCount / arr.length).toFixed(6), "\n"+ (negativoCount / arr.length).toFixed(6), "\n"+ (zeroCount / arr.length).toFixed(6));

}
plusMinus([1, 2, 3])

  • Thank you for the information, and for teaching me how to do it, I was really just missing a little bit of syntax. Thank you all!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.