Last active
June 17, 2020 12:14
Star
You must be signed in to star a gist
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
// |
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
let nonPrimitiveArray = [ | |
{name: 'Alpha', letter: 'A'}, | |
{name: 'Bravo', letter: 'B'}, | |
{name: 'Charlie', letter: 'C'} | |
]; |
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
// Objectives: | |
// 1. Find the value Bravo | |
// 2. Find the index of 1 | |
const OBJECTIVE_STRING = 'Bravo'; | |
let arr = [ | |
{name: 'Alpha', letter: 'A'}, | |
{name: 'Bravo', letter: 'B'}, | |
{name: 'Charlie', letter: 'C'}, | |
{name: 'Delta', letter: 'D'}, | |
{name: 'Echo', letter: 'E'}, | |
{name: 'Foxtrot', letter: 'F'}, | |
{name: 'Golf', letter: 'G'}, | |
]; | |
let foundObject = null; | |
let foundIndex = -1; |
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
// Using array and variables from base code block above… (starter-code.js) | |
for (let index = 0; index < arr.length; index++) { | |
const value = arr[index]; | |
if(value.name === OBJECTIVE_STRING) { | |
foundObject = value; | |
foundIndex = index; | |
break; | |
} | |
}; | |
console.log(foundObject); // expected output: {name: 'Bravo', letter: 'B'}; | |
console.log(foundIndex); // expected output: 1; |
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
// Using array and variables from base code block above… (starter-code.js) | |
for (const value of arr) { | |
if(value.name === OBJECTIVE_STRING) { | |
foundObject = value; | |
} | |
}; | |
console.log(foundObject); // expected output: {name: 'Bravo', letter: 'B'}; |
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
// Using array and variables from base code block above… (starter-code.js) | |
arr.forEach((value, index) => { | |
if(value.name === OBJECTIVE_STRING) { | |
foundObject = value; | |
foundIndex = index; | |
} | |
}); | |
console.log(foundObject); // expected output: {name: 'Bravo', letter: 'B'}; | |
console.log(foundIndex); // expected output: 1; |
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
// Using array and variables from base code block above… (starter-code.js) | |
let length = arr.length; | |
let index = 0; | |
while(index < length) { | |
const value = arr[index]; | |
if(value.name === OBJECTIVE_STRING) { | |
foundObject = value; | |
foundIndex = index; | |
index = length - 1; | |
} | |
index++; | |
}; | |
console.log(foundObject); // expected output: {name: 'Bravo', letter: 'B'}; | |
console.log(foundIndex); // expected output: 1; |
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
// Using array and variables from base code block above… (starter-code.js) | |
let length = arr.length; | |
let index = 0; | |
do { | |
const value = arr[index]; | |
if(value.name === OBJECTIVE_STRING) { | |
foundObject = value; | |
foundIndex = index; | |
index = length - 1; | |
} | |
index++; | |
} while (index < length); | |
console.log(foundObject); // expected output: {name: 'Bravo', letter: 'B'}; | |
console.log(foundIndex); // expected output: 1; |
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
// Using array and variables from base code block above… (starter-code.js) | |
foundObject = arr.find((value, index) => { | |
if(value.name === OBJECTIVE_STRING) { | |
foundObject = index; | |
return true; | |
} | |
return false; | |
}); | |
console.log(foundObject); // expected output: {name: 'Bravo', letter: 'B'}; | |
console.log(foundIndex); // expected output: 1; |
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
// Using array and variables from base code block above… (starter-code.js) | |
foundIndex = arr.findIndex((value, index) => { | |
if(value.name === OBJECTIVE_STRING) { | |
foundObject = value; | |
return true; | |
} | |
return false; | |
}); | |
console.log(foundObject); // expected output: {name: 'Bravo', letter: 'B'}; | |
console.log(foundIndex); // expected output: 1; |
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
// Using array and variables from base code block above… (starter-code.js) | |
foundIndex = arr.lastIndexOf((value, index) => { | |
if(value.name === OBJECTIVE_STRING) { | |
foundObject = value; | |
return true; | |
} | |
return false; | |
}); | |
console.log(foundObject); // expected output: {name: 'Bravo', letter: 'B'}; | |
console.log(foundIndex); // expected output: 1; |
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
// Using array and variables from base code block above… (starter-code.js) | |
// This an anti-pattern when used ONLY to loop and find a value/index rather than the intended function of returning a new array. | |
arr.map((value, index) => { | |
if(value.name === OBJECTIVE_STRING && foundIndex !== -1) { | |
foundObject = value; | |
foundIndex = index; | |
} | |
}); | |
console.log(foundObject); // expected output: {name: 'Bravo', letter: 'B'}; | |
console.log(foundIndex); // expected output: 1; |
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
// reduce | |
// Using array and variables from base code block above… (starter-code.js) | |
// This an anti-pattern when used ONLY to loop and find a value/index rather than the intended function of returning a value. | |
foundObject = arr.filter((value, index) => { | |
if(value.name === OBJECTIVE_STRING && foundIndex !== -1) { | |
foundIndex = index; | |
} | |
})[0]; | |
console.log(foundObject); // expected output: {name: 'Bravo', letter: 'B'}; | |
console.log(foundIndex); // expected output: 1; |
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
// Using array and variables from base code block above… (starter-code.js) | |
// This an anti-pattern when used ONLY to loop and find a value/index rather than the intended function of returning a new value. | |
arr.reduce((accumulator, current, index) => { | |
if(current.name === OBJECTIVE_STRING && foundIndex !== -1) { | |
foundObject = current; | |
foundIndex = index; | |
} | |
return accumulator; | |
}); | |
console.log(foundObject); // expected output: {name: 'Bravo', letter: 'B'}; | |
console.log(foundIndex); // expected output: 1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment