Skip to content

Instantly share code, notes, and snippets.

@DrinKaziu
Created September 18, 2017 16:35
Show Gist options
  • Select an option

  • Save DrinKaziu/98362cb602282b6e3565602a724dbed8 to your computer and use it in GitHub Desktop.

Select an option

Save DrinKaziu/98362cb602282b6e3565602a724dbed8 to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/furiyac
<!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">
// you can pass in `scratchData` to test out `findByid`
// your function
var scratchData = [
{id: 22, foo: 'bar'},
{id: 28, foo: 'bizz'},
{id: 19, foo: 'bazz'}
];
function findById(items, idNum) {
return items.find(function(item) {
return item.id === idNum;
});
}
//
function testIt() {
var testData = [
{id: 1, foo: 'bar'},
{id: 2, foo: 'bizz'},
{id: 3, bang: 'boo'}
];
var result = findById(testData, 3);
if (!(result && result !== null && typeof result === 'object')) {
console.error('`findById` must return an object');
return
}
if (result.id !== 3) {
console.error(
'Asked for item with id of `3` but got back one with id of ' +
result.id
);
return
}
if (result.bang !== 'boo') {
console.error('Expected all key/value pairs from target object to be returned');
return
}
console.log('SUCCESS: `findByid` is working');
}
testIt();
</script>
<script id="jsbin-source-javascript" type="text/javascript">// you can pass in `scratchData` to test out `findByid`
// your function
var scratchData = [
{id: 22, foo: 'bar'},
{id: 28, foo: 'bizz'},
{id: 19, foo: 'bazz'}
];
function findById(items, idNum) {
return items.find(function(item) {
return item.id === idNum;
});
}
//
function testIt() {
var testData = [
{id: 1, foo: 'bar'},
{id: 2, foo: 'bizz'},
{id: 3, bang: 'boo'}
];
var result = findById(testData, 3);
if (!(result && result !== null && typeof result === 'object')) {
console.error('`findById` must return an object');
return
}
if (result.id !== 3) {
console.error(
'Asked for item with id of `3` but got back one with id of ' +
result.id
);
return
}
if (result.bang !== 'boo') {
console.error('Expected all key/value pairs from target object to be returned');
return
}
console.log('SUCCESS: `findByid` is working');
}
testIt();</script></body>
</html>
// you can pass in `scratchData` to test out `findByid`
// your function
var scratchData = [
{id: 22, foo: 'bar'},
{id: 28, foo: 'bizz'},
{id: 19, foo: 'bazz'}
];
function findById(items, idNum) {
return items.find(function(item) {
return item.id === idNum;
});
}
//
function testIt() {
var testData = [
{id: 1, foo: 'bar'},
{id: 2, foo: 'bizz'},
{id: 3, bang: 'boo'}
];
var result = findById(testData, 3);
if (!(result && result !== null && typeof result === 'object')) {
console.error('`findById` must return an object');
return
}
if (result.id !== 3) {
console.error(
'Asked for item with id of `3` but got back one with id of ' +
result.id
);
return
}
if (result.bang !== 'boo') {
console.error('Expected all key/value pairs from target object to be returned');
return
}
console.log('SUCCESS: `findByid` is working');
}
testIt();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment