[英]Having a problem with a Javascript assignment: Array and object iteration but I have no idea what the solution is, I have tried everything
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
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.请记住,您需要控制台记录每个鸟对象属性的键和值。
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 // 任务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.