简体   繁体   中英

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.


Step 1. You are given an array of dairy products:

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

Create a function called logDairy . Within it, console log each of the items in the dairy array, using the for...of loop.
The expected output should be:

cheese
sour cream
milk
yogurt
ice cream
milkshake

You are given the following starter code: 您将获得以下起始代码:
 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. Remember, you need to console log both the key and the value of each of the bird object's properties.


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. 使用与任务 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

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

function logDairy() {

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

    console.log(dairy[prop]);
}

}

logDairy();

// Task 2

const animal = {

canJump: true

};

const bird = Object.create(animal);

bird.canFly = true;

bird.hasFeathers = true;

function birdCan() {

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

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

} birdCan();

// Task 3

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. 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM