-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path06-Plus_Minus.js
81 lines (61 loc) · 1.71 KB
/
06-Plus_Minus.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/**
* Created on Sun Feb 27 2022
*
* @author Carlos Páez
*/
'use strict'
process.stdin.resume()
process.stdin.setEncoding('utf-8')
let inputString = ''
let currentLine = 0
/* Reading the input from the user and storing it in the variable `inputString` */
process.stdin.on('data', (inputStdin) => {
inputString += inputStdin
})
/* Reading the input from the user and storing it in the variable `inputString` */
process.stdin.on('end', () => {
inputString = inputString.split('\n')
main()
})
/**
* It reads a line from the input string and increments the current line counter
*/
const readLine = () => inputString[currentLine++]
/*
* Complete the `plusMinus` function below
*
* The function accepts INTEGER_ARRAY arr as parameter
*/
/**
* Given an array of integers, calculate the ratios of positive, negative, and zero values in the array
* @param n - the size of the array
* @param arr - an array of integers
*/
const plusMinus = (n, arr) => {
let pos = 0
let neg = 0
let zero = 0
if (0 < n && n <= 100) {
arr.map(i => {
if (-100 <= i && i <= 100) {
pos += (i > 0) ? 1 : 0
neg += (i < 0) ? 1 : 0
zero += (i == 0) ? 1 : 0
}
})
}
let ratio_pos = pos / n
let ratio_neg = neg / n
let ratio_zero = zero / n
console.log(ratio_pos.toFixed(6))
console.log(ratio_neg.toFixed(6))
console.log(ratio_zero.toFixed(6))
}
/**
* Print the ratio of positive, negative, and zero items in the array
*/
const main = () => {
const n = parseInt(readLine().trim(), 10)
const arr = readLine().replace(/\s+$/g, '').split(' ').map(arrTemp => parseInt(arrTemp, 10))
plusMinus(n, arr)
}