Skip to content

Instantly share code, notes, and snippets.

@KDCinfo
Created April 30, 2017 08:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KDCinfo/f1d6544cc116964f6d383713e7860a31 to your computer and use it in GitHub Desktop.
Save KDCinfo/f1d6544cc116964f6d383713e7860a31 to your computer and use it in GitHub Desktop.
Coding Challenge - Removing Values from an Array

Coding Challenge - Removing Values from Array

Language / Framework

  • Vanilla JavaScript

Attribution

This project derived from: Programming Challenges: [Remove False Values From Array]

Project Timeline

Notes I took for working on this coding challenge

  • 2017-04-29 @ 11:43pm - 12:08am = 25 min

  • + 5-10 min (refactoring) == 30-35 min Total

    • Although I stumbled quite a bit along the way, I did not resort to looking anything up.
  • + 12:18am - 12:26am = 8 min === 40-45 min Grand Total

    • Added for adding .reduce() (which I did have to look up on MDN)

Project Details

Results provided in console output.

JavaScript Used:

  • Array.fill()
  • Array.map()
  • Array.filter()
  • Array.reduce() (had to look this up)
  • Math

Overall project took about 45 min to fully complete.

This Code on CodePen

Learning Gotchas

Still struggling with writing core syntax from memory (without as much trial/error); Repetition.

// This project derived from:
// http://code.startupinacar.com/programming-challenges/
// Programming Challenges: [Remove False Values From Array]
// --- --- --- --- --- --- --- --- --- --- --- ---
console.clear()
const arrayCount = 20 // Set a length for your array
const myArray = Array(arrayCount).fill(1).map( () => getRnd() ),
myArray2T = myArray.filter( (key, val) => key === true ),
myArray2F = myArray.filter( (key, val) => key === false ),
myArray2R = myArray.reduce( (acc, val, idx, myArray) => {
return val === true ? acc + 1 : acc
})
console.log('Original Array:', myArray)
console.log('Filtered to True:', myArray2T.length, myArray2T)
console.log('Filtered to False:', myArray2F.length, myArray2F)
console.log('Reduced to True count:', myArray2R)
function getRnd() {
return Math.round(Math.random(), 0) === 1 ? true : false
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment