简体   繁体   English

Javascript 分配有问题:数组和 object 迭代但我不知道解决方案是什么,我已经尝试了一切

[英]Having a problem with a Javascript assignment: Array and object iteration but I have no idea what the solution is, I have tried everything

Task: Iterate Over an Array任务:遍历数组

In this exercise, you'll use the for....of loop to iterate over an array and to iterate over an object's own properties.在本练习中,您将使用 for....of 循环遍历数组和对象自身的属性。


Step 1. You are given an array of dairy products:第 1 步:给您一系列乳制品:

var dairy = ['cheese', 'sour cream', 'milk', 'yogurt', 'ice cream', 'milkshake']

Create a function called logDairy .创建一个名为 logDairy 的logDairy Within it, console log each of the items in the dairy array, using the for...of loop.在其中,控制台使用 for...of 循环记录乳品数组中的每个项目。
The expected output should be:预期的 output 应该是:

cheese
sour cream
milk
yogurt
ice cream
milkshake

Step 2. You are given the following starter code: 第 2 步。您将获得以下起始代码:
 const animal = { canJump: true }; const bird = Object.create(animal); bird.canFly = true; bird.hasFeathers = true;

Create a function called birdCan , within it, loop over the bird object's properties and console log each one, using the for...of loop.创建一个名为birdCan的 function ,在其中使用 for...of 循环遍历鸟对象的属性和控制台日志。 Remember, you need to console log both the key and the value of each of the bird object's properties.请记住,您需要控制台记录每个鸟对象属性的键和值。


Step 3. Using the same starter code as in task 2, create a function called `animalCan` and within it, loop over all the properties in both the bird object and its prototype - the animal object - using the for...in loop. 步骤 3.使用与任务 2 相同的起始代码,创建名为“animalCan”的 function,并在其中循环遍历鸟 object 及其原型 - 动物 ZA8CFDE6331BD59EB2AC96F891 中的所有属性... .

My answer:我的答案:

 // Task 1 var dairy = ['cheese', 'sour cream', 'milk', 'yogurt', 'ice cream', 'milkshake']; function logDairy(items) { for (const element of items) { console.log(element); } } console.log(logDairy(dairy)); // Task 2 const animal = { canJump: true }; const bird = Object.create(animal); bird.canFly = true; bird.hasFeathers = true; function birdCan(items){ for (let [key, value] of Object.entries(items)) { console.log(key, value); } } console.log(birdCan(animal)); // Task 3 function animalCan(items) { for (let [key, value] in Object.entries(items)) { console.log(key, value); } } console.log(animalCan(animal));


Having this error after running the code: 运行代码后出现此错误:
 FAILED: Console logged expected values for logDairy - returned TypeError: items is not iterable but expected cheesesour creammilkyogurtice creammilkshake FAILED: Console logged expected values for birdCan - returned TypeError: Cannot convert undefined or null to object but expected canFly: truehasFeathers: true FAILED: Console logged expected values for animalCan - returned TypeError: Cannot convert undefined or null to object but expected canFly: truehasFeathers: truecanJump: true
var dairy = [
    "cheese",
    "sour cream",
    "milk",
    "yogurt",
    "ice cream",
    "milkshake",
];

function logDairy(array) {
    for (const item of array) {
        console.log(item);
    }
}

logDairy(dairy);

const animal = {
    canJump: true,
};

const bird = Object.create(animal);

bird.canFly = true;

bird.hasFeathers = true;

function birdCan(myObject) {
    for (let [key, value] of Object.entries(myObject)) {
        console.log(key, value);
    }
}
birdCan(bird);

function animalCan(myObject) {
    const props = Object.keys(Object.getPrototypeOf(myObject)).concat(
        Object.keys(myObject)
    );
    for (const prop of props) {
        console.log(prop);
    }
}


animalCan(bird);

// Task 1 // 任务1

var dairy = ['cheese', 'sour cream', 'milk', 'yogurt', 'ice cream', 'milkshake']; var milk = ['奶酪', '酸奶油', '牛奶', '酸奶', '冰淇淋', '奶昔'];

function logDairy() { function logDairy() {

for (prop of Object.keys(dairy)){

    console.log(dairy[prop]);
}

} }

logDairy();日志乳制品();

// Task 2 // 任务 2

const animal = {常量动物 = {

canJump: true可以跳:真

}; };

const bird = Object.create(animal);常量鸟 = Object.create(动物);

bird.canFly = true;鸟.canFly = true;

bird.hasFeathers = true;鸟.hasFeathers = true;

function birdCan() { function 鸟罐(){

for (prop of Object.keys(bird)) {

    console.log(`${prop}: ${bird[prop]}`);
 }

} birdCan(); } 鸟可以();

// Task 3 // 任务 3

function animalCan() { function animalCan() {

for (prop in bird) {

    console.log(`${prop}: ${bird[prop]}`);
 }

} animalCan(); } 动物可以();

You can use following code.您可以使用以下代码。 I've submitted my assignment and got 100/100.我已经提交了作业并获得了 100/100。 Hope this will work for you.希望这对你有用。 Happy Coding...快乐的编码...

 // Task 1 var dairy = ['cheese', 'sour cream', 'milk', 'yogurt', 'ice cream', 'milkshake']; function logDairy() { for (const element of dairy) { console.log(element); } } console.log(logDairy(dairy)); // Task 2 const animal = { canJump: true }; const bird = Object.create(animal); bird.canFly = true; bird.hasFeathers = true; function birdCan() { for (keys of Object.keys(bird)) console.log(keys,": ", bird[keys]); } birdCan(); // Task 3 function animalCan() { for (prop in bird) { console.log(prop,": ", bird[prop]); } } animalCan();

Hope this helps.希望这可以帮助。 The Coursera grader looks at a granular level whereby even the spacing of the colons may constitute an incorrect answer and cause you to fail the assignment even though the console doesn't log any errors. Coursera 评分器会查看细粒度级别,即使冒号的间距也可能构成错误答案,并导致您的作业失败,即使控制台没有记录任何错误。

// Task 1
var dairy = ['cheese', 'sour cream', 'milk', 'yogurt', 'ice cream', 'milkshake'];

function logDairy() {
    for (const element of dairy) {
    console.log(element);
    
    }
}

console.log(logDairy(dairy));

// Task 2

const animal = {
    canJump: true
};

const bird = Object.create(animal);
bird.canFly = true;
bird.hasFeathers = true;

function birdCan() {
    for (keys of Object.keys(bird))
        console.log(keys,": ", bird[keys]);
}


    
birdCan();


     
// Task 3

function animalCan() {
    for (prop in bird) {
        console.log(prop,": ", bird[prop]);
    }
}
animalCan();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Javascript 分配有问题,我不知道解决方案是什么,我已经尝试了一切 - Having a problem with a Javascript assignment and I have no idea what the solution is, i have tried everything 我在 HTML 中对 forms 的这个想法/问题最接近的解决方案是什么? - What is the closest solution to this Idea/Problem I have with forms in HTML? 我有一个 object 的数组,需要将其重组为所需的格式。 我尝试使用迭代使用数组解构 - I have an array of object which needs to restructured into a desired format. I tried using array destructuring using iteration 我不知道Object(this)的意思 - I have no idea Object(this) means 我不知道这段代码是什么 - I have no idea what this code is 此javascript代码在没有for循环的情况下可以正常运行,但是没有循环。任何帮助,我都尝试了一切 - This javascript code is running properly without the for loop but not with it.Any help, I have tried everything 我在将对象添加到数组时遇到问题 - I have problem with adding my object to array 我遇到了 Discord.js 代码的问题,我没有找到解决方案 - I am having a problem with Discord.js code I have not found a solution 我如何 map 反应 js 中的一个数组我试过了但是有一些问题谁能帮我 - how do i map an array in react js i have tried but there is some problem can anyone help me 我有一个这样的数组对象,我想根据 _0 属性对这个数组进行排序,请在 Javascript 或 Dojo 中提出一些解决方案 - i have a array object like this, i want to sort this array based on _0 attribute, please suggest some solution in Javascript or Dojo
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM