简体   繁体   English

给定一组正数,负数和零数字,返回百分比数组

[英]Returning an array of percentages given an array of positives, negatives, and zero numbers

I need some help with a problem which requires this: 我需要一些与此相关的问题的帮助:

We need a function FractionOf that receives an array and return another with the following three numbers: 我们需要一个函数FractionOf,该函数接收一个数组并返回另一个具有以下三个数字的数组:

 in the first position, the fraction of numbers that are positive
 in the second position, the fraction of numbers that are zero
 in the last position, the fraction of numbers that are negative

For example, FractionOf ([1, 2, 0, -1]) should return [0.5, 0.25, 0.25], given that there are 50% positive, 25% zero, and 25% negative. 例如,假定存在50%的正数,25%的零和25%的负数,FractionOf([1、2、0,-1])应该返回[0.5、0.25、0.25]。

So i tried to give at least some form to what they asked me, but i'm stuck on how to convert percentages to fractions and add them to the position. 所以我试图至少给他们提出的要求某种形式,但是我仍然坚持如何将百分比转换为分数并将其添加到职位。

var nums=[1,2,0,0,-1,-4]
function FractionOf(nums){
  var result=[]
  var num1=0
  var num2=0
  var num3=0
  for(var i=0; i<nums.length; i++){
    var num=nums[i]
    if(num>0){
      num1= num1 + 0.25}
    if (num==0){
      num2=num2 + 0.25}
    if(num<0){
     num3=num3 + 0.25}
result.push(num1)
result.push(num2)
result.push(num3)
return result
}
function FractionOf(nums) {
  return nums
    .reduce((acc, cur) => {
      if (cur > 0) acc[0]++;
      else if (cur === 0) acc[1]++;
      else acc[2]++;
      return acc;
    }, [0, 0, 0])
    .map(result => result / nums.length);
}

This solution uses reduce to count up the occurrences of each of the three cases, starting with an array filled with 0s. 该解决方案使用reduce来计算三种情况中每种情况的发生次数,从填充0的数组开始。 The result is piped to the map function, which divides each value by the length of the input num to obtain a percentage ranging from 0-1. 结果通过管道传递给map函数,该函数将每个值除以输入num的长度以获得0-1范围内的百分比。

To get the results in percentages you should compute the fraction of a set with the formula: 要获得百分比结果,您应该使用以下公式计算集合的分数

( total_numbers_from_a_subset / total_numbers ) * 100.

try with: 尝试:

var nums=[1,2,0,0,-1,-4]
function FractionOf(nums){
  var result=[]
  var num1=0
  var num2=0
  var num3=0
  for(var i=0; i<nums.length; i++){
    var num=nums[i]
    if(num>0){
      num1++; //how many numbers in the set are < 0
    }
    if (num==0){
      num2++
    }
    if(num<0){
     num3++
    }
}
result.push(num1 / nums.length * 100)
result.push(num2 / nums.length * 100)
result.push(num3 / nums.length * 100)
return result

you can get your desired result with this.... 您可以以此获得您想要的结果。

var nums=[6,5,0,-5,-3]
        function FractionOf(nums){
            var result = [];
            var positive = 0;
            var zero = 0;
            var negative = 0;
            for(var i = 0; i < nums.length; i++){
                if(nums[i] > 0){
                    positive++;
                }
                if (nums[i] == 0){
                    zero++;
                }
                if(nums[i] < 0){
                    negative++;
                }
            }
            result.push(positive / nums.length * 100);
            result.push(zero / nums.length * 100);
            result.push(negative / nums.length * 100);
            return result;
        }

hope this helps... 希望这可以帮助...

You could reduce the array by returning the sum foe every wanted check and map the parts of it. 您可以通过返回每一个想要的检查的总和并映射它的各个部分来减少数组。

 function fractionsOf(nums) { return nums .reduce(([p, m, n], v) => [p + (v > 0), m + (v < 0), n + (v === 0)], [0, 0, 0]) .map(v => v / nums.length); } console.log(fractionsOf([1, 2, 0, -1])); console.log(fractionsOf([1, 2, 0, 0, -1, -4])); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM