gpt4 book ai didi

javascript - 多输入一输出

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:08:04 24 4
gpt4 key购买 nike

如何将多个输入值处理为一个输出值?

function c(input,val){
return input.indexOf(val)>-1;
}

function result(i){
if(c(i,1) && c(i,2) && c(i,3)){
return "alpha";
}else if(c(i,1) && c(i,2) && !c(i,3)){
return "beta";
}else if(c(i,1) && !c(i,2) && c(i,3)){
return "gamma";
}else if(c(i,1) && !c(i,2)){
return "delta";
}else if(c(i,3)){
return "theta";
}
//..... and so on covering all possible combinations

return null;
}

result([1,2,3]); //output : alpha
result([1,3,2]); //output : alpha
result([1,3,1,1]); //output : gamma
result([1,2,4]); //output : beta
result([3]); //output : theta

数组中的值的数量可以是 N,但只能来自一组预定义的值

处理这么多组合的正确方法是什么?

最佳答案

根据 Condor 的回答,以下内容应该可以完成工作。它使用一系列测试来计算结果并执行“不”测试。如果值为“true”,则它必须出现,如果值为“false”,则不得出现。如果未提及,则它是否在值中并不重要。

重复不是问题,值会被处理直到一个失败。不过,先删除重复项可能会加快速度。

结果是第一组通过的测试的名称。

function testValues(values) {

var checks = [
{alpha: {1:true, 2:true, 3:true }},
{beta : {1:true, 2:true, 3:false}},
{gamma: {1:true, 2:false, 3:true }},
{theta: {3:true}}
];
var check, resultName, tests, passed;

// Do checks in sequence
for (var i=0, iLen=checks.length; i<iLen; i++) {
check = checks[i]

// Get name of result to return if the checks pass
for (resultName in check) {

// Make sure result is own property
if (check.hasOwnProperty(resultName)) {

// Passed is true until a fail is found
passed = true;

// Get tests to perform
tests = check[resultName];

// For each value in tests, make sure value exists or doesn't in values
for (var v in tests) {

if (tests.hasOwnProperty(v)) {

// Only test if passed is true
if (passed) {

// Note that indexOf uses === so must have same type
// Property names are always strings so if passing numbers,
// Must convert to numbers
passed = tests[v] === (values.indexOf(+v) != -1);
}
}
}

// If passed tests, return
if (passed) return resultName;

}
}
}
return 'failed all tests...';
}

console.log(testValues([1,2,3])); //output : alpha
console.log(testValues([1,3,2])); //output : alpha
console.log(testValues([1,3,1,1])); //output : gamma
console.log(testValues([1,2,4])); //output : beta
console.log(testValues([3])); //output : theta

如果 Object.keysforEach 一起使用,代码可能会更短一些,但上面的代码更清楚一点(也许),它可以重构为更短。如果提供了用于 Array.prototype.indexOf 的垫片,以上内容将在 ECMA-262 ed 3 环境中工作。

编辑

如果使用现代功能,代码可以稍微简化。为旧浏览器提供支持并不困难:

// These are sufficient to support the function but are not suitable for general use
// Better versions are avaialble at MDN, see links below
if (!Object.keys) {
Object.keys = function(obj) {
var keys = [];
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
keys.push(key);
}
}
return keys;
};
}

if (!Array.prototype.indeOf) {
Array.prototype.indexOf = function (value) {
for (var i=0, iLen=this.length; i<iLen; i++) {
if (this[i] === value) return i;
}
return -1;
};
}

function testValues(values) {

var checks = [
{alpha: {1:true, 2:true, 3:true }},
{beta : {1:true, 2:true, 3:false}},
{gamma: {1:true, 2:false, 3:true }},
{theta: {3:true}}
];
var check, passed, resultName, tests, testKeys;

for (var i=0, iLen=checks.length; i<iLen; i++) {
check = checks[i]
resultName = Object.keys(check)[0];
passed = true;
tests = check[resultName];
testKeys = Object.keys(tests);

for (var j=0, jLen=testKeys.length; j<jLen && passed; j++) {
passed = tests[testKeys[j]] === (values.indexOf(+testKeys[j]) != -1);
}

if (passed) return resultName;
}
return 'failed all tests...';
}

对象键:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys

Array.prototype.indexOf: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf

关于javascript - 多输入一输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22754029/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com