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