Created
March 13, 2017 13:11
-
-
Save Mattteo1220/0adf6de919ec05c2ac1c1f3c40e3bcdc to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/hodicab
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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