Reputation: 31
function renderInventory(inventory) {
//create a flat list
var flatList = '';
//iterate over the inventory
for (var i = 0; i < inventory.length; i++) {
var designerObject = inventory[i];
var shoes = designerObject.shoes;
//iterate over the each shoe in the array
for (var j = 0; j < shoes.length; j++) {
var currentShoe = shoes[j];
//add the designer name, the shoe name, and the shoe price and the new line
flatList = designerObject.name +", " + currentShoe.name +", " + currentShoe.price + '\n';
}
}
//return the flat list
return flatList;
}
//assertion Function
function assertEqual(actual, expected, testName) {
if (actual === expected) {
console.log('passed');
} else {
console.log('FAILED [' + testName + '] Expected "' + expected + '", but got "' + actual + "'");
}
}
//test cases
var currentInventory = [{
name: 'Brunello Cucinelli',
shoes: [
{name: 'tasselled black low-top lace-up', price: 1000},
{name: 'tasselled green low-top lace-up', price: 1100},
{name: 'plain beige suede moccasin', price: 950},
{name: 'plain olive suede moccasin', price: 1050}
]
},
{
name: 'Gucci',
shoes: [
{name: 'red leather laced sneakers', price: 800},
{name: 'black leather laced sneakers', price: 900}
]
}
];
var actualFlatList = renderInventory(currentInventory);
var expectedFlatList = 'Brunello Cucinelli, tasselled black low-top lace-up, 1000\nBrunello Cucinelli, tasselled green low-top lace-up, 1100\nBrunello Cucinelli, plain beige suede moccasin, 950\nBrunello Cucinelli, plain olive suede moccasin, 1050\nGucci, red leather laced sneakers, 800\nGucci, black leather laced sneakers, 900\n';
assertEqual(actualFlatList, expectedFlatList, "should render flat list of inventory items");
Result: FAILED [should render flat list of inventory items] Expected "Brunello Cucinelli, tasselled black low-top lace-up, 1000 Brunello Cucinelli, tasselled green low-top lace-up, 1100 Brunello Cucinelli, plain beige suede moccasin, 950 Brunello Cucinelli, plain olive suede moccasin, 1050 Gucci, red leather laced sneakers, 800 Gucci, black leather laced sneakers, 900 ", but got "Gucci, black leather laced sneakers, 900 '
I tried to add my code properly. Hopefully, I did it right. I'm a newbee in coding. My question is, this unit testing code block supposed to give 'passed'. Where did I do wrong? Can someone clarify it please.
Upvotes: 0
Views: 46
Reputation: 31
I just found the problem.
flatList **+=** designerObject.name + ", " + currentShoe.name + ", " + currentShoe.price + '\n';
It was missing an addition in the function renderInventory(inventory). Thank you!
Upvotes: 1