Javascript validateKey函数未返回预期的键值

Javascript validateKey函数未返回预期的键值,javascript,function,object,Javascript,Function,Object,此函数的目标是验证密钥。如果键匹配且不存在其他键,则应返回true。如果没有匹配的键或它们小于预期的键,则应返回false 函数validateKeys(object,expectedKeys)通常应返回true或false。我发布了详细的code供您查看程序流程 //running the function with `objectA` and `expectedKeys` // should return `true` const objectA = { id: 2, name:

此函数的目标是验证密钥。如果键匹配且不存在其他键,则应返回true。如果没有匹配的键或它们小于预期的键,则应返回false

函数
validateKeys(object,expectedKeys)
通常应返回
true
false
。我发布了详细的
code
供您查看程序流程

//running the function with `objectA` and `expectedKeys`
// should return `true`

const objectA = {
  id: 2,
  name: 'Jane Doe',
  age: 34,
  city: 'Chicago',
};

// running the function with `objectB` and `expectedKeys`
// should return `false`
const objectB = {
  id: 3,
  age: 33,
  city: 'Peoria',
};

const expectedKeys = ['id', 'name', 'age', 'city'];

function validateKeys(object, expectedKeys) {
  // your code goes here
    for (let i=0; i<expectedKeys.length;i++) {
        if (Object.keys(object).length === expectedKeys[i]) {
                return false;
        }else if (expectedKeys[i] < Object.keys(object) || Object.keys(object).length > expectedKeys[i] ) {
            return false;
        }else
        return;
    }
return true;
} 


/* From here down, you are not expected to 
   understand.... for now :)  


   Nothing to see here!

*/

function testIt() {
  const objectA = {
    id: 2,
    name: 'Jane Doe',
    age: 34,
    city: 'Chicago',
  };

  const objectB = {
    id: 3,
    age: 33,
    city: 'Peoria',
  };

  const objectC = {
    id: 9,
    name: 'Billy Bear',
    age: 62,
    city: 'Milwaukee',
    status: 'paused',
  };

  const objectD = {
    foo: 2,
    bar: 'Jane Doe',
    bizz: 34,
    bang: 'Chicago',
  };

  const expectedKeys = ['id', 'name', 'age', 'city'];

  if (typeof validateKeys(objectA, expectedKeys) !== 'boolean') {
    console.error('FAILURE: validateKeys should return a boolean value');
    return;
  }

  if (!validateKeys(objectA, expectedKeys)) {
    console.error(
      `FAILURE: running validateKeys with the following object and keys
      should return true but returned false:
      Object: ${JSON.stringify(objectA)}
      Expected keys: ${expectedKeys}`
    );
    return;
  }

  if (validateKeys(objectB, expectedKeys)) {
    console.error(
      `FAILURE: running validateKeys with the following object and keys
      should return false but returned true:
      Object: ${JSON.stringify(objectB)}
      Expected keys: ${expectedKeys}`
    );
    return;
  }

  if (validateKeys(objectC, expectedKeys)) {
    console.error(
      `FAILURE: running validateKeys with the following object and keys
      should return false but returned true:
      Object: ${JSON.stringify(objectC)}
      Expected keys: ${expectedKeys}`
    );
    return;
  }

  if (validateKeys(objectD, expectedKeys)) {
    console.error(
      `FAILURE: running validateKeys with the following object and keys
      should return false but returned true:
      Object: ${JSON.stringify(objectD)}
      Expected keys: ${expectedKeys}`
    );
    return;
  }

  console.log('SUCCESS: validateKeys is working');
}

testIt();
//使用'objectA'和'expectedKeys'运行函数`
//应该返回“true”`
常量对象A={
id:2,
姓名:“简·多伊”,
年龄:34岁,
城市:'芝加哥',
};
//使用'objectB'和'expectedKeys'运行函数`
//应该返回“false”`
常量对象B={
id:3,
年龄:33岁,
城市:“皮奥里亚”,
};
const expectedKeys=['id','name','age','city'];
函数validateKeys(对象、预期键){
//你的密码在这里
for(设i=0;我期望密钥[i]){
返回false;
}否则
回来
}
返回true;
} 
/*从这里开始,你不应该
懂目前:)
这里没什么可看的!
*/
函数testIt(){
常量对象A={
id:2,
姓名:“简·多伊”,
年龄:34岁,
城市:'芝加哥',
};
常量对象B={
id:3,
年龄:33岁,
城市:“皮奥里亚”,
};
常量对象C={
id:9,
名字:'比利熊',
年龄:62,
城市:“密尔沃基”,
状态:“已暂停”,
};
const objectD={
傅:2,,
酒吧:“无名氏”,
比兹:34岁,
砰,‘芝加哥’,
};
const expectedKeys=['id','name','age','city'];
if(validateKeys的类型(objectA,expectedKeys)!='boolean'){
错误('FAILURE:validateKeys应返回布尔值');
回来
}
如果(!validateKeys(objectA,expectedKeys)){
控制台错误(
`失败:使用以下对象和键运行validateKeys
应返回true,但返回false:
对象:${JSON.stringify(objectA)}
预期的键:${expectedKeys}`
);
回来
}
if(validateKeys(objectB,expectedKeys)){
控制台错误(
`失败:使用以下对象和键运行validateKeys
应返回false,但返回true:
对象:${JSON.stringify(objectB)}
预期的键:${expectedKeys}`
);
回来
}
if(validateKeys(objectC,expectedKeys)){
控制台错误(
`失败:使用以下对象和键运行validateKeys
应返回false,但返回true:
对象:${JSON.stringify(objectC)}
预期的键:${expectedKeys}`
);
回来
}
if(validateKeys(objectD,expectedKeys)){
控制台错误(
`失败:使用以下对象和键运行validateKeys
应返回false,但返回true:
对象:${JSON.stringify(objectD)}
预期的键:${expectedKeys}`
);
回来
}
log('SUCCESS:validateKeys正在工作');
}
testIt();

您正在比较字符串(expectedKeys[i])和数字(长度)。 Javascript不会给你一个错误,但它的计算结果总是错误的。 另外,您在for循环中放置了一个返回,该返回在循环运行时会中断该循环
遇到。

您可能需要以下内容:

function validateKeys(object, expectedKeys) {
  let keys = Object.keys(object);

  // Check if both arrays have the same length
  // if not, we can exit early
  if (keys.length !== expectedKeys.length) {
    return false;
  }

  // If they do have the same length, then let's see if they
  // have all the keys, if not, return false
  for (let index = 0; index < expectedKeys.length; index++) {
    if (!expectedKeys.includes(keys[index])) {
        return false;
    };
  }

  // else return true, the keys are valid
  return true;
}
函数validateKeys(对象、预期键){
让keys=Object.keys(Object);
//检查两个数组是否具有相同的长度
//如果没有,我们可以提前退出
if(keys.length!==expectedKeys.length){
返回false;
}
//如果它们的长度相同,那么让我们看看它们是否相同
//拥有所有的密钥,如果没有,则返回false
for(让index=0;index
//使用'objectA'和'expectedKeys'运行函数`
//应该返回“true”`
常量对象A={
id:2,
姓名:“无名氏”,
年龄:34岁,
城市:“芝加哥”,
};
//使用'objectB'和'expectedKeys'运行函数`
//应该返回“false”`
常量对象B={
id:3,
年龄:33岁,
城市:“皮奥里亚”,
};
const expectedKeys=[“id”、“name”、“age”、“city”];
函数validateKeys(对象、预期键){
让keys=Object.keys(Object);
//检查两个数组是否具有相同的长度
//如果没有,我们可以提前退出
if(keys.length!==expectedKeys.length){
返回false;
}
//如果它们的长度相同,那么让我们看看它们是否相同
//拥有所有的密钥,如果没有,则返回false
for(让index=0;index