
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Positive, negative and zeroes contribution of an array in JavaScript
Suppose we have an array of integers, (positive, negative and zero) like this −
const arr = [23, -1, 0, 11, 18];
We are required to write a JavaScript function that takes in one such array as the first and the only argument. The function should then find the fractional ratio for all three different groups, namely positive, negative and zero.
For example −
For the above array, its length is 5, the output for this array should be −
const output = [.2, .2, .6];
The output array will always contain 3 numbers, representing the fractional ratio of negative, zero and positive integers respectively. One rough way to verify our answer is to add these three values and check if they near 1 or not, if they do, we are very likely to have solved the problem correctly.
Example
The code for this will be −
const arr = [23, -1, 0, 11, 18]; const findRatio = (arr = []) => { const { length } = arr; const res = [0, 0, 0]; for(let i = 0; i < arr.length; i++){ const el = arr[i]; const key = el / Math.abs(el || 1); res[key + 1]++; }; return res.map(el => el / length); }; console.log(findRatio(arr));
Output
And the output in the console will be −
[0.2, 0.2, 0.6]
- Related Articles
- Split an array of numbers and push positive numbers to JavaScript array and negative numbers to another?
- Find Count of Positive, Negative and Zero Elements in an Array in Java
- Reversing negative and positive numbers in JavaScript
- Sum a negative number (negative and positive digits) - JavaScript
- Find count of positive and negative array elements in Java
- Increment Negative and Decrement Positive Numbers by 1 in an Array in Java
- Find the Pairs of Positive Negative values in an Array using C++\n
- How to print positive and negative infinity values in JavaScript?
- Implement Bubble sort with negative and positive numbers – JavaScript?
- Positive and Negative Effects of Daydreaming
- Finding array number that have no matching positive or negative number in the array using JavaScript
- Java Program to convert positive int to negative and negative to positive
- Arrange Negative Array Elements Before Positive Elements in Java
- Distinguish positive and negative numbers.
- Schizophrenia Symptoms: Positive and Negative?
