JS Bin
// source https://jsbin.com/talucir
function makeStudentsReport(data){
return data.map(function(d) {
return d.name + ': ' + d.grade;
});
}
var Data = [
{name: 'Jane Doe', grade: 'A'},
{name: 'John Dough', grade: 'B'},
{name: 'Jill Do', grade: 'A'}
];
/* From here down, you are not expected to
understand.... for now :)
Nothing to see here!
*/
// tests
function testIt() {
var testData = [
{name: 'Jane Doe', grade: 'A'},
{name: 'John Dough', grade: 'B'},
{name: 'Jill Do', grade: 'A'}
];
var expectations = [
'Jane Doe: A',
'John Dough: B',
'Jill Do: A'
];
var results = makeStudentsReport(testData);
if (!(results && results instanceof Array)) {
console.error(
'FAILURE: `makeStudentsReport` must return an array');
return
}
if (results.length !== testData.length) {
console.error(
'FAILURE: test data had length of ' + testData.length +
' but `makeStudentsReport` returned array of length ' + results.length);
return
}
for (var i=0; i < expectations.length; i++) {
var expect = expectations[i];
if (!results.find(function(item) {
return item === expect;
})) {
console.error(
'FAILURE: `makeStudentsReport` is not ' +
'producing expected strings'
);
return
}
}
console.log('SUCCESS: `makeStudentsReport` is working');
}
testIt();
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">
function makeStudentsReport(data){
return data.map(function(d) {
return d.name + ': ' + d.grade;
});
}
var Data = [
{name: 'Jane Doe', grade: 'A'},
{name: 'John Dough', grade: 'B'},
{name: 'Jill Do', grade: 'A'}
];
/* From here down, you are not expected to
understand.... for now :)
Nothing to see here!
*/
// tests
function testIt() {
var testData = [
{name: 'Jane Doe', grade: 'A'},
{name: 'John Dough', grade: 'B'},
{name: 'Jill Do', grade: 'A'}
];
var expectations = [
'Jane Doe: A',
'John Dough: B',
'Jill Do: A'
];
var results = makeStudentsReport(testData);
if (!(results && results instanceof Array)) {
console.error(
'FAILURE: `makeStudentsReport` must return an array');
return
}
if (results.length !== testData.length) {
console.error(
'FAILURE: test data had length of ' + testData.length +
' but `makeStudentsReport` returned array of length ' + results.length);
return
}
for (var i=0; i < expectations.length; i++) {
var expect = expectations[i];
if (!results.find(function(item) {
return item === expect;
})) {
console.error(
'FAILURE: `makeStudentsReport` is not ' +
'producing expected strings'
);
return
}
}
console.log('SUCCESS: `makeStudentsReport` is working');
}
testIt();
</script>
<script id="jsbin-source-javascript" type="text/javascript">function makeStudentsReport(data){
return data.map(function(d) {
return d.name + ': ' + d.grade;
});
}
var Data = [
{name: 'Jane Doe', grade: 'A'},
{name: 'John Dough', grade: 'B'},
{name: 'Jill Do', grade: 'A'}
];
/* From here down, you are not expected to
understand.... for now :)
Nothing to see here!
*/
// tests
function testIt() {
var testData = [
{name: 'Jane Doe', grade: 'A'},
{name: 'John Dough', grade: 'B'},
{name: 'Jill Do', grade: 'A'}
];
var expectations = [
'Jane Doe: A',
'John Dough: B',
'Jill Do: A'
];
var results = makeStudentsReport(testData);
if (!(results && results instanceof Array)) {
console.error(
'FAILURE: `makeStudentsReport` must return an array');
return
}
if (results.length !== testData.length) {
console.error(
'FAILURE: test data had length of ' + testData.length +
' but `makeStudentsReport` returned array of length ' + results.length);
return
}
for (var i=0; i < expectations.length; i++) {
var expect = expectations[i];
if (!results.find(function(item) {
return item === expect;
})) {
console.error(
'FAILURE: `makeStudentsReport` is not ' +
'producing expected strings'
);
return
}
}
console.log('SUCCESS: `makeStudentsReport` is working');
}
testIt();
</script></body>
</html>