How to put multiple numbers on a var

Asked

Viewed 95 times

0

var input = require('fs').readFileSync('/dev/stdin', 'utf8');
var lines = input.split('\n');

var i, p;

console.log(a);
p = 0;

{
    for(i = 1; i <= 6; i = i + 1);

    if(x > 0.0){
        p = p + 1;
    }
    console.log(p + ' valores positivos');
}

I have to use this code to read 6 numbers (Lines[0] up to Lines[5]) and show how many are positive, but I don’t know how to create a var that reads all lines as numbers, I tried to use var x = [Lines[0], Lines[1], ..., Lines[5]]; however it reads as a string and I can’t use x.split(","); How should I do? Standard input:

7; -5; 6; -3.4; 4.6; 12;

2 answers

0

Try the following script:

var p = 0;
var lines = input.split(";")

for(var i = 0; i < 6; i++){
    if(parseFloat(lines[i]) > 0.0){
         p = p + 1;
    }
}
 console.log(p + ' valores positivos');

0

Try using "filter" with "Math.Sign"

var positivos = [7, -5, 6, -3.4, 4.6, 12].filter(x => Math.sign(x) == 0 || Math.sign(x) == 1);
    // positivos fica sendo [7, 6, 4.6, 12]

filter

Math.

Browser other questions tagged

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