How To Solve The Plus Minus Code Challenge

How To Solve HackerRank’s Plus Minus Code Challenge With JavaScript

Manny
3 min readJan 17, 2019
How To Solve HackerRank’s Plus Minus Code Challenge With JavaScript

Problem

Let’s break down the challenge into requirements:

  • Link to challenge: HackerRank’s Plus Minus Code Challenge
  • n = length of array arr, between 0 and 100
  • arr = array with values between -100 and 100
  • Goal is to console.log 3 values based on the number negative, positive, and zeros present in the array.
  • Count the number of (positive, negative, zeros) and divide it by the total length of the array to get the ratio.

Examples

// [1, 1, 0, -1, -1]
// Positive Numbers = 2
console.log(2/5); // 0.4
// Negative Numbers = 2
console.log(2/5); // 0.4
// Zero Number = 1
console.log(1/5); // 0.2

Goal

Write a function or functions that console.logs the ratios for positive, negative, and zero numbers in that order

Covering Our Bases

Making sure we meet the requirements:

function plusMinus(arr) {
let positives = 0, negatives = 0, zeros = 0;
const len = arr.length || 0;
if (len > 0 && len <= 100) {
// continue
}

// output
console.log((positives / len) || 0);
console.log((negatives / len) || 0);
console.log((zeros / len) || 0);
}

Understanding The Problem

This is quite simple problem. All we need to do is count the number of positives, negatives, and zeros. In order to traverse the array, we’ll use the map function.

function plusMinus(arr) {
let positives = 0, negatives = 0, zeros = 0;
const len = arr.length || 0;

if (len > 0 && len <= 100) {
arr.map((elem, key) => {
if (elem > 0) {
positives++;
} else if (elem < 0) {
negatives++;
} else {
zeros++;
}

return elem;
});
}

// output
console.log((positives / len) || 0);
console.log((negatives / len) || 0);
console.log((zeros / len) || 0);
}

Solution

The full solution without the comments:

function plusMinus(arr) {
let positives = 0, negatives = 0, zeros = 0;
const len = arr.length || 0;

if (len > 0 && len <= 100) {
arr.map((elem, key) => {
if (elem > 0) {
positives++;
} else if (elem < 0) {
negatives++;
} else {
zeros++;
}

return elem;
});
}

console.log((positives / len) || 0);
console.log((negatives / len) || 0);
console.log((zeros / len) || 0);
}

Test Cases

Now let’s validate the code:

// -4 3 -9 0 4 1, Expected
// 0.500000
// 0.333333
// 0.166667

// 1 2 3 -1 -2 -3 0 0, Expected
// 0.375000
// 0.375000
// 0.250000
plusMinus([-4, 3, -9, 0, 4, 1]);
// 0.500000 ✅
// 0.333333 ✅
// 0.166667 ✅
plusMinus([1, 2, 3, -1, -2, -3, 0, 0]);
// 0.375000 ✅
// 0.375000 ✅
// 0.250000 ✅

Feedback

If you have any tips on how this can be better optimized or talk about coding, I would love to talk.

If you got value from this, please share it on twitter 🐦 or other social media platforms. Thanks again for reading. 🙏

Please also follow me on twitter: @codingwithmanny and instagram at @codingwithmanny.

--

--

Manny
Manny

Written by Manny

DevRel Engineer @ Berachain | Prev Polygon | Ankr & Web Application / Full Stack Developer

Responses (3)