본문 바로가기
Research/Coding Test

HackerRank_Plus Minus

by RIEM 2023. 4. 2.
728x90

Problem

Code

function plusMinus(arr) {
  const totalLen = arr.length;
  let positives = 0;
  let negatives = 0;
  let zeros = 0;
  
  arr.forEach(el => {
    if(el > 0) positives++;
    else if(el < 0) negatives++;
    else zeros++;
  })
  
  positives /= totalLen
  negatives /= totalLen
  zeros /= totalLen
  
  positives = Number.parseFloat(positives).toFixed(6);
  negatives = Number.parseFloat(negatives).toFixed(6);
  zeros = Number.parseFloat(zeros).toFixed(6);

  console.log(positives)
  console.log(negatives)
  console.log(zeros)
}
728x90

'Research > Coding Test' 카테고리의 다른 글

HackerRank_Birthday Cake Candles  (0) 2023.04.05
HackerRank_Staircase  (0) 2023.04.02
HackerRank_Diagonal Difference  (0) 2023.03.30
leetcode_66. plus-one  (0) 2023.03.24
747. Largest Number At Least Twice of Others  (0) 2023.03.23

댓글