Skip to content

Instantly share code, notes, and snippets.

@Mattteo1220
Created March 13, 2017 13:11
Show Gist options
  • Save Mattteo1220/0adf6de919ec05c2ac1c1f3c40e3bcdc to your computer and use it in GitHub Desktop.
Save Mattteo1220/0adf6de919ec05c2ac1c1f3c40e3bcdc to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/hodicab
<!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 updateObject(obj) {
obj.foo = 'foo';
obj.bar = 'bar';
obj.bizz = 'bizz';
obj.bang = 'bang';
return obj;
}
/* From here down, you are not expected to
understand.... for now :)
Nothing to see here!
*/
(function testUpdateObject() {
var oldObj = {
cats: 'cats',
dogs: 'dogs',
};
var newObj = updateObject(oldObj);
if (typeof newObj !== 'object') {
console.error('ERROR: `createMyObject` must return an object');
return false
}
['foo', 'bar', 'bizz', 'bang'].forEach(function(key) {
if (!(key in newObj)) {
console.error('ERROR: `' + key + '` not in object returned by `updateObject`');
return false;
}
});
['foo', 'bar', 'bizz', 'bang'].forEach(function(key) {
if (newObj[key] !== key) {
console.error('ERROR: `' + key + '` should be "' + key + '" but was ' + newObj[key]);
return false;
}
});
if (!(newObj.cats === 'cats' && newObj.dogs === 'dogs')) {
console.error('ERROR: your function doesn\'t preserve existing key/value pairs');
return false;
}
console.log('SUCCESS: `updateObject` works correctly!');
})();
</script>
<script id="jsbin-source-javascript" type="text/javascript">function updateObject(obj) {
obj.foo = 'foo';
obj.bar = 'bar';
obj.bizz = 'bizz';
obj.bang = 'bang';
return obj;
}
/* From here down, you are not expected to
understand.... for now :)
Nothing to see here!
*/
(function testUpdateObject() {
var oldObj = {
cats: 'cats',
dogs: 'dogs',
};
var newObj = updateObject(oldObj);
if (typeof newObj !== 'object') {
console.error('ERROR: `createMyObject` must return an object');
return false
}
['foo', 'bar', 'bizz', 'bang'].forEach(function(key) {
if (!(key in newObj)) {
console.error('ERROR: `' + key + '` not in object returned by `updateObject`');
return false;
}
});
['foo', 'bar', 'bizz', 'bang'].forEach(function(key) {
if (newObj[key] !== key) {
console.error('ERROR: `' + key + '` should be "' + key + '" but was ' + newObj[key]);
return false;
}
});
if (!(newObj.cats === 'cats' && newObj.dogs === 'dogs')) {
console.error('ERROR: your function doesn\'t preserve existing key/value pairs');
return false;
}
console.log('SUCCESS: `updateObject` works correctly!');
})();</script></body>
</html>
function updateObject(obj) {
obj.foo = 'foo';
obj.bar = 'bar';
obj.bizz = 'bizz';
obj.bang = 'bang';
return obj;
}
/* From here down, you are not expected to
understand.... for now :)
Nothing to see here!
*/
(function testUpdateObject() {
var oldObj = {
cats: 'cats',
dogs: 'dogs',
};
var newObj = updateObject(oldObj);
if (typeof newObj !== 'object') {
console.error('ERROR: `createMyObject` must return an object');
return false
}
['foo', 'bar', 'bizz', 'bang'].forEach(function(key) {
if (!(key in newObj)) {
console.error('ERROR: `' + key + '` not in object returned by `updateObject`');
return false;
}
});
['foo', 'bar', 'bizz', 'bang'].forEach(function(key) {
if (newObj[key] !== key) {
console.error('ERROR: `' + key + '` should be "' + key + '" but was ' + newObj[key]);
return false;
}
});
if (!(newObj.cats === 'cats' && newObj.dogs === 'dogs')) {
console.error('ERROR: your function doesn\'t preserve existing key/value pairs');
return false;
}
console.log('SUCCESS: `updateObject` works correctly!');
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment