猿问

我的 validateKey 函数没有返回预期的键值

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

  }



守着星空守着你
浏览 142回答 2
2回答

Qyouu

您可能需要以下内容:function validateKeys(object, expectedKeys) {&nbsp; let keys = Object.keys(object);&nbsp; // Check if both arrays have the same length&nbsp; // if not, we can exit early&nbsp; if (keys.length !== expectedKeys.length) {&nbsp; &nbsp; return false;&nbsp; }&nbsp; // If they do have the same length, then let's see if they&nbsp; // have all the keys, if not, return false&nbsp; for (let index = 0; index < expectedKeys.length; index++) {&nbsp; &nbsp; if (!expectedKeys.includes(keys[index])) {&nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; };&nbsp; }&nbsp; // else return true, the keys are valid&nbsp; return true;}//running the function with `objectA` and `expectedKeys`// should return `true`const objectA = {&nbsp; id: 2,&nbsp; name: "Jane Doe",&nbsp; age: 34,&nbsp; city: "Chicago",};// running the function with `objectB` and `expectedKeys`// should return `false`const objectB = {&nbsp; id: 3,&nbsp; age: 33,&nbsp; city: "Peoria",};const expectedKeys = ["id", "name", "age", "city"];function validateKeys(object, expectedKeys) {&nbsp; let keys = Object.keys(object);&nbsp;&nbsp;&nbsp; // Check if both arrays have the same length&nbsp; // if not, we can exit early&nbsp; if (keys.length !== expectedKeys.length) {&nbsp; &nbsp; return false;&nbsp; }&nbsp; // If they do have the same length, then let's see if they&nbsp; // have all the keys, if not, return false&nbsp; for (let index = 0; index < expectedKeys.length; index++) {&nbsp; &nbsp; if (!expectedKeys.includes(keys[index])) {&nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; };&nbsp; }&nbsp; // else return true, the keys are valid&nbsp; return true;}/* From here down, you are not expected to&nbsp;&nbsp; &nbsp;understand.... for now :)&nbsp;&nbsp;&nbsp; &nbsp;Nothing to see here!*/function testIt() {&nbsp; const objectA = {&nbsp; &nbsp; id: 2,&nbsp; &nbsp; name: "Jane Doe",&nbsp; &nbsp; age: 34,&nbsp; &nbsp; city: "Chicago",&nbsp; };&nbsp; const objectB = {&nbsp; &nbsp; id: 3,&nbsp; &nbsp; age: 33,&nbsp; &nbsp; city: "Peoria",&nbsp; };&nbsp; const objectC = {&nbsp; &nbsp; id: 9,&nbsp; &nbsp; name: "Billy Bear",&nbsp; &nbsp; age: 62,&nbsp; &nbsp; city: "Milwaukee",&nbsp; &nbsp; status: "paused",&nbsp; };&nbsp; const objectD = {&nbsp; &nbsp; foo: 2,&nbsp; &nbsp; bar: "Jane Doe",&nbsp; &nbsp; bizz: 34,&nbsp; &nbsp; bang: "Chicago",&nbsp; };&nbsp; const expectedKeys = ["id", "name", "age", "city"];&nbsp; if (typeof validateKeys(objectA, expectedKeys) !== "boolean") {&nbsp; &nbsp; console.error("FAILURE: validateKeys should return a boolean value");&nbsp; &nbsp; return;&nbsp; }&nbsp; if (!validateKeys(objectA, expectedKeys)) {&nbsp; &nbsp; console.error(&nbsp; &nbsp; &nbsp; `FAILURE: running validateKeys with the following object and keys&nbsp; &nbsp; &nbsp; should return true but returned false:&nbsp; &nbsp; &nbsp; Object: ${JSON.stringify(objectA)}&nbsp; &nbsp; &nbsp; Expected keys: ${expectedKeys}`&nbsp; &nbsp; );&nbsp; &nbsp; return;&nbsp; }&nbsp; if (validateKeys(objectB, expectedKeys)) {&nbsp; &nbsp; console.error(&nbsp; &nbsp; &nbsp; `FAILURE: running validateKeys with the following object and keys&nbsp; &nbsp; &nbsp; should return false but returned true:&nbsp; &nbsp; &nbsp; Object: ${JSON.stringify(objectB)}&nbsp; &nbsp; &nbsp; Expected keys: ${expectedKeys}`&nbsp; &nbsp; );&nbsp; &nbsp; return;&nbsp; }&nbsp; if (validateKeys(objectC, expectedKeys)) {&nbsp; &nbsp; console.error(&nbsp; &nbsp; &nbsp; `FAILURE: running validateKeys with the following object and keys&nbsp; &nbsp; &nbsp; should return false but returned true:&nbsp; &nbsp; &nbsp; Object: ${JSON.stringify(objectC)}&nbsp; &nbsp; &nbsp; Expected keys: ${expectedKeys}`&nbsp; &nbsp; );&nbsp; &nbsp; return;&nbsp; }&nbsp; if (validateKeys(objectD, expectedKeys)) {&nbsp; &nbsp; console.error(&nbsp; &nbsp; &nbsp; `FAILURE: running validateKeys with the following object and keys&nbsp; &nbsp; &nbsp; should return false but returned true:&nbsp; &nbsp; &nbsp; Object: ${JSON.stringify(objectD)}&nbsp; &nbsp; &nbsp; Expected keys: ${expectedKeys}`&nbsp; &nbsp; );&nbsp; &nbsp; return;&nbsp; }&nbsp; console.log("SUCCESS: validateKeys is working");}testIt();

慕标琳琳

您正在将string(expectedKeys[i]) 与 number(length)进行比较。Javascript不会给你一个错误,但它总是会评估为假。此外,您在 for 循环中放置了一个 return,当遇到它时会中断循环。
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答