<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
</body>
</html>
function renderInventory(arr) {
var result = [];
for(var i = 0; i < arr.length; i++){
for(var j = 0; j < arr[i].shoes.length; j++){
result.push(arr[i].name + ', ' + arr[i].shoes[j].name + ', ' + arr[i].shoes[j].price);
}
}
return result.join('\n');
}
// try pushing your rendered strings to an array
// then join the array using the new line operator instead
// because right now, the way you’re returning it doesn’t do that
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 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"];
var actual = renderInventory(currentInventory);
console.log(actual);
function assertrenderInventory(actual, expected, testName){
if(JSON.stringify(actual) === JSON.stringify(expected)){
console.log('passed');
}else{
console.log('FAILED [' + testName + '] Expected "' + actual + '" to be "' + expected + '"');
}
}
assertrenderInventory(actual, actual, 'testing renderInventory');
Output
300px
This bin was created anonymously and its free preview time has expired (learn why). — Get a free unrestricted account
Dismiss xKeyboard Shortcuts
Shortcut | Action |
---|---|
ctrl + [num] | Toggle nth panel |
ctrl + 0 | Close focused panel |
ctrl + enter | Re-render output. If console visible: run JS in console |
Ctrl + l | Clear the console |
ctrl + / | Toggle comment on selected lines |
ctrl + ] | Indents selected lines |
ctrl + [ | Unindents selected lines |
tab | Code complete & Emmet expand |
ctrl + shift + L | Beautify code in active panel |
ctrl + s | Save & lock current Bin from further changes |
ctrl + shift + s | Open the share options |
ctrl + y | Archive Bin |
Complete list of JS Bin shortcuts |
JS Bin URLs
URL | Action |
---|---|
/ | Show the full rendered output. This content will update in real time as it's updated from the /edit url. |
/edit | Edit the current bin |
/watch | Follow a Code Casting session |
/embed | Create an embeddable version of the bin |
/latest | Load the very latest bin (/latest goes in place of the revision) |
/[username]/last | View the last edited bin for this user |
/[username]/last/edit | Edit the last edited bin for this user |
/[username]/last/watch | Follow the Code Casting session for the latest bin for this user |
/quiet | Remove analytics and edit button from rendered output |
.js | Load only the JavaScript for a bin |
.css | Load only the CSS for a bin |
Except for username prefixed urls, the url may start with http://jsbin.com/abc and the url fragments can be added to the url to view it differently. |