在处理来自另一个数组的值后,将数组存储在数组中 [英] Store values in an array after treat the values from another array

查看:86
本文介绍了在处理来自另一个数组的值后,将数组存储在数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一个问题,我必须对待一个给定的值,并检查这个值是否大于我的值数组,如果是,则使用我的数组组合输出。

I'm stumbling into a problem that I've to treat a given value and check if this value is bigger than my array of values, if it is, combine the output using my array.

例如。

我的数组一直是:

const ArrayPrimitive = [100,50,20,10];

例如输入中的给定值:


  • 条目:30.00结果:[20.00,10.00]

  • 条目:80.00结果:[50.00,20.00,10.00]

  • 条目:125.00结果:throw NoteUnavailableException

  • 条目:-130.00结果:throw InvalidArgumentException

  • 条目:NULL结果:[空集]

  • Entry: 30.00 Result: [20.00, 10.00]
  • Entry: 80.00 Result: [50.00, 20.00, 10.00]
  • Entry: 125.00 Result: throw NoteUnavailableException
  • Entry: -130.00 Result: throw InvalidArgumentException
  • Entry: NULL Result: [Empty Set]

我已经开始开发,但是我坚持了如何处理其余的,检查后是否值有效。

I've started to develop but I'm stuck in the in how to deal with the rest, after check if the value is valid.

const ArrayPrimitive = [100,50,20,10];
var combination = [];
$(document).ready(function(){
  $(".btn-submit").on("click",function(){
    var amountOfMoney = document.getElementById('amountOfMoney').value;
    for(let i=0; i=ArrayPrimitive.length;i++){
      if(amountOfMoney=ArrayPrimitive[i]){
        combination.push(amountOfMoney);
      }
      while(amountOfMoney > ArrayPrimitive[i]){
        var k = amountOfMoney/ArrayPrimitive[i];
        for(let j=0; j = k; j++){
          combination.push(ArrayPrimitive[i]);
          amountOfMoney = amountOfMoney - ArrayPrimitive[i];
        }
        var rest = n % ArrayPrimitive[i];
      }
    }
  });
});

我的主要问题是:


  1. 你们可以帮助我以旧的方式解决这个问题吗?

  2. 我将要学习ES6,并且可能要学习 .map 或其他功能可以节省我的生命,有人可以启发我吗?

我希望自己清楚。

https://jsfiddle.net/v748ha08 / 1 /

更新:感谢@Malk为您的答案,但您的解决方案只提供正确的答案,必须组合子集的多个值。

UPDATE: Thanks @Malk for your answer but your solution just provides the right answer only when you don't have to combine multiple values of the subset.

例如
条目:200.00
结果:[100.00,100.00]

e.g. Entry: 200.00 Result: [100.00, 100.00]

在这种情况下,我需要2个子集中的100个值,当我测试这个函数会给我一个错误。

In this case I need 2 values of 100 in my subset, and when I test this they functions throws me an error.

推荐答案

如果你想遍历一个数组并建立一个对象),然后 Array.reduce()是要走的路。

If you want to iterate through an array and build up an object (or anything else) along the way then Array.reduce() is the way to go.

const ArrayPrimitive = [100, 95, 20, 10]; // Assuming presorted array(descending)

function findChange(m) {
  return ArrayPrimitive.reduce((mm, c) => {
    if (mm.rest >= c) {
      mm.change.push(c);
      mm.rest -= c
    }
    return mm
  }, {
    change: [],
    rest: m
  });
}

function findChangeOld(m) {
  var retval = {
      change: [],
      rest: m
    },
    i = ArrayPrimitive.length;

  for (var x = 0; x < i; x++) {
    if (retval.rest >= ArrayPrimitive[x]) {
      retval.change.push(ArrayPrimitive[x])
      retval.rest -= ArrayPrimitive[x];
    }
  }
  return retval;
}

function calcChange(v) {
  var c = findChangeOld(v);

  if (v < 0 || isNaN(v)) {
    console.log(`${v}: throw InvalidArgumentException`);
    return;
  }

  if (c.rest > 0)
    console.log(`${v}: throw NoteUnavailableException`);
  else
    console.log(`${v}: ${c.change}`);
}

calcChange(30);
calcChange(80);
calcChange(105);
calcChange(125);
calcChange(-130);
calcChange(null);

这篇关于在处理来自另一个数组的值后,将数组存储在数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆