【问题标题】:My validateKey function is not returning expected key values [closed]我的 validateKey 函数没有返回预期的键值 [关闭]
【发布时间】:2020-04-06 05:52:37
【问题描述】:

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

函数validateKeys(object, expectedKeys) 通常应返回truefalse。我贴了详细的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();

【问题讨论】:

  • else return; ????这似乎是你的问题之一
  • 这看起来很像家庭作业。
  • 您的逻辑似乎有缺陷,例如,您要在这里做什么Object.keys(object).length === expectedKeys[i]?一个是给出给定对象中键的数量,另一个是具有某个键值的字符串。

标签: javascript function object


【解决方案1】:

您可能想要以下内容:

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;
}

//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) {
  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;
}

/* 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();

【讨论】:

    【解决方案2】:

    您正在比较 string(expectedKeys[i]) 与 number(length) 。 Javascript不会给你一个错误,但它总是会评估为假。 此外,您还在 for 循环中放置了一个 return,该循环在循环时会中断 遇到过。

    【讨论】:

    • 请在评论中提出一些解决方案
    猜你喜欢
    • 2020-06-30
    • 1970-01-01
    • 2021-04-04
    • 2018-04-10
    • 1970-01-01
    • 2016-06-15
    • 2014-03-25
    • 2016-05-05
    • 2018-01-31
    相关资源
    最近更新 更多