我需要创建一个函数测试用例,它将平面类名称列表转换为以每个学生的年龄装饰的对象文字。
应为每个学生(10岁或11岁)随机生成年龄。鉴于每个学生的年龄在每次运行时是随机的,建议测试检查年龄值为10或11岁。
举个例子:
var classList = ["Joe", "Jack", "John", "Fred", "Frank", "Barry", "Larry", "Mary",
"Harry", "Farrell", "Susan", "Monica", "Keira", "Caroline", "Harriet", "Erica",
"Luann", "Cheryl", "Beth", "Rupa", "Linda", "Allison", "Nancy", "Dora"];
var classListWithAges = [{"name":"Joe","age":11},{"name":"Jack","age":10},
{"name":"John","age":11},{"name":"Fred","age":11},{"name":"Frank","age":11},
{"name":"Barry","age":11},{"name":"Larry","age":11},{"name":"Mary","age":11},
{"name":"Harry","age":11},{"name":"Farrell","age":10},{"name":"Susan","age":10},
{"name":"Monica","age":11},{"name":"Keira","age":10},{"name":"Caroline","age":10},
{"name":"Harriet","age":11},{"name":"Erica","age":11},{"name":"Luann","age":10},
{"name":"Cheryl","age":11},{"name":"Beth","age":10},{"name":"Rupa","age":11},
{"name":"Linda","age":10},{"name":"Allison","age":10},{"name":"Nancy","age":10},
{"name":"Dora","age":10}]
现在这里是首发代码:
// See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random
function getRandomIntInclusive(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function decorateClassListWithAges(classList) {
// your code here
}
任何帮助怎么做?我真的很困惑。
答案 0 :(得分:1)
function decorateClassListWithAges(classList) {
var list = [];
classList.map(function(n){
list.push({ name: n, age: getRandomIntInclusive(10,11) });
});
return list;
}
var classListWithAges = decorateClassListWithAges(classList);
答案 1 :(得分:1)
function decorateClassListWithAges (classList) {
return classList.map(name => {
return {
name: name,
age: getRandomIntInclusive(10, 11)
}
})
}
const result = decorateClassListWithAges(classList)
console.log(result)
答案 2 :(得分:-1)
function decorateClassListWithAges() {
for (let student of classList) {
classListWithAges.push({name: student, age: getRandomIntInclusive(10, 11)});
}
}
或
classListWithAges = classList.map(function(student) {
return { name: student, age: getRandomIntInclusive(10, 11) };
})