Skip to content

Instantly share code, notes, and snippets.

@jnewman12
Last active June 13, 2018 23:04
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 jnewman12/0a0a5538bc1effe6e6d85abdc0ba613b to your computer and use it in GitHub Desktop.
Save jnewman12/0a0a5538bc1effe6e6d85abdc0ba613b to your computer and use it in GitHub Desktop.
WDI Week #1 JS Practice Problems
/*
1. Counting Sheep
Consider an array of sheep where some sheep may be missing from their place.
We need a function that counts the number of sheep present in the array (true means present).
*/
var arr = [true, true, true, false,
true, true, true, true ,
true, false, true, false,
true, false, false, true ,
true, true, true, true ,
false, false, true, true];
/*
2. Even or Odd
Create a function that takes an integer as an argument and returns "Even" for even numbers or "Odd" for odd numbers.
evenOrOdd(2) => 'Even'
*/
/*
3. Alphabet Checker
When provided with a letter, return its position in the alphabet. start at 0, not 1
alphabetChecker(a) => 0
*/
/*
4. Needle in Haystack
Write a function findNeedle() that takes an array full of junk but containing one "needle"
After your function finds the needle it should return a message (as a string) that says:
"found the needle at position " plus the index it found the needle
findNeedle(['hay', 'junk', 'hay', 'hay', 'moreJunk', 'needle', 'randomJunk']) => 'found the needle at position 5'
*/
var hayStack = ['hay', 'junk', 'hay', 'hay', 'moreJunk', 'needle', 'randomJunk'];
/* 5. Nested Array to Object
Take a multi-dimensional array (an array of arrays) and turn it into a hash
arrToObj([['other', 'thing'], ['final', 'string']]) => {'other': 'thing', 'final': 'string'}
*/
var multi_arr = [['other', 'thing'], ['final', 'string']];
/* 6. Object to Nested Array
Take an object, and transform it to a nested array
objToArr({name: 'James', age: 26}) => [[name, 'James'], [age, 26]]
*/
/* 7. Double array to object
Take an array of numbers and transform it to an object with it's value doubled
doubleNum([1, 2, 3, 4, 5]) => {1: 2, 2: 4, 3: 6, 4: 8, 5: 10}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment