此函数的目标是验证密钥。如果键匹配且不存在其他键,则应返回 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;
}
Qyouu
慕标琳琳
相关分类