Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- - Review: `for` loops
- + Write a function `foo(n)` such that when you do `foo(3)`, it prints out:
- 1
- 2
- 3
- Each on its own line.
- + Write a function `bar(n, m)` such that when you do `bar(4, 6)`, it prints out:
- 4
- 5
- 6
- Each on its own line.
- - Basic loop reading array:
- + write a function `printList(arr)` such that `printList([1,2,3])` prints out:
- 1
- 2
- 3
- Each on its own line.
- + write a function `sum(arr)` such that `sum([1,2,3])` returns `6`
- + write a function `max(arr)` such that `max([1,3,2])` returns `3`
- - Double-push: write a function `doublePush(arr, x, y)` such that when you do the following:
- var arr = [1,2,3];
- doublePush(arr, "apples", "oranges");
- console.log(arr) // => [1, 2, 3, "apples", "oranges"]
- Then you get the indicated output.
- - Do the `range()` exercise from Chapter 4 without hints.
- - Double each: write a function `doubleEach(arr)` such that `doubleEach([1,2,3])` returns `[2,4,6]`
- - Component-wise addition: write a function `vectorAdd(v, w)` such that:
- vectorAdd([1,2,3], [1,1,1]) // => [2,3,4]
- vectorAdd([1,2,3], [2,0,-1]) // => [3,2,2]
- // bonus: trims to shorter of the two arrays
- vectorAdd([1,2], [1,2,3]) // => [2,4]
- vectorAdd([1,2, 3], [1,2]) // => [2,4]
- - The `arguments` object:
- - write a function that always returns the last argument:
- last(1,2,3) // => 3
- last(1) // => 1
- last("apples", "oranges") // => "oranges"
- last([1,2,3], "why") // => "why"
- last("why", "not", [1,2]) // => [1,2]
- - write a function that sums its arguments, so `sum(1,2,3)` returns `6`, `sum(4,4,5,5)` returns `18`, etc
- - somewhat advanced, uses techniques from all of the above: write a function `zip(arr, ...)` that takes any number of arrays, and returns one array of each of their contents zipped together
- zip([1,3,5], [2,4,6]) // => [[1,2], [3,4], [5,6]]
- zip([1,2], [3,4], [5,6]) // => [[1,3,5], [2,4,6]]
- // extends to the length of longest array, fill in with undefined
- zip([1,2], [1,2,3], [1]) // => [[1,1,1], [2,2,undefined], [undefined,3,undefined]]
- - array of arrays: write a function `averages(arr, ...)` that takes any number of arrays, and returns an array of their averages
- averages([1,2,3], [1,1,1], [1,1,2,2]) // => [2, 1, 1.5]
- - left-pad ragged 2D array of numbers: write a function `leftPadZeroes(arrOfArrays)` that fills in zeroes on the left of the array so that they'll all be the same length (don't know how to prepend something to an array? Google it)
- leftPadZeroes([[1,2,3], [4], [5,6]]) // => [[1,2,3], [0,0,4], [0,5,6]]
- leftPadZeroes([[1,2], [4], [5,6,7,8]]) // => [[0,0,1,2], [0,0,0,4], [5,6,7,8]]
- - also advanced but similar to `zip()`, matrix multiplication: write a function `matrixMultiply(P, Q)` that takes a _n_×_m_ array of arrays and a _ℓ_×_n_ array of arrays, and matrix-multiplies them to return a _ℓ_×_m_ array of arrays:
- matrixMultiply(
- [[1],
- [2],
- [3]],
- [[1,1,1],
- [1,1,1]]
- )
- // => [[6],[6]]
- matrixMultiply(
- [[1],
- [2],
- [3]],
- [[1,2,1],
- [2,1,2]]
- )
- // => [[8],[10]]
- matrixMultiply(
- [[1,2,3]],
- [[1],
- [1],
- [1]]
- )
- // => [[1,2,3],
- // [1,2,3],
- // [1,2,3]]
- matrixMultiply(
- [[1,2],
- [3,4]],
- [[1,1],
- [1,1]]
- )
- // => [[4,6],
- // [4,6]]
- matrixMultiply(
- [[1,2],
- [3,4]],
- [[0,1],
- [1,0]]
- )
- // => [[1,2],
- // [3,4]]
- matrixMultiply(
- [[1,2],
- [3,4]],
- [[1,0],
- [0,1]]
- )
- // => [[3,4],
- // [1,2]]
- ### Objects
- - Underscore.js
- + Write a function `keys(obj)` that retrives an array of the keys (fun fact: you're reimplementing `Object.keys()`. Don't use it):
- var obj = {
- lol: 1,
- wut: 2
- };
- var obj2 = {
- haha: 'omg',
- 1: 2
- };
- keys(obj); // => ['lol', 'wut'] (possibly in a different order)
- keys(obj2); // => ['haha', '2'] (possibly in a different order)
- + Write a function `values(obj)` that retrives an array of the values:
- var obj = {
- lol: 1,
- wut: 2
- };
- var obj2 = {
- haha: 'omg',
- 1: 2
- };
- keys(obj); // => ['1', '2'] (possibly in a different order)
- keys(obj2); // => ['omg', '2'] (possibly in a different order)
- + (Non-Underscore interstitial) Write a function `sumValues(obj)` that sums the values:
- var points = {
- rick: 101,
- morty: 2,
- summer: 45,
- jerry: -20,
- beth: 99.99
- };
- sumValues(points); // => 227.99
- + (Non-Underscore interstitial) Write a function `avgValue(obj)` that returns the average of the value:
- var points = {
- team_alpha_ninja: 26,
- team_master_blaster: 20,
- team_wypipo: 20,
- team_untitled2: 18
- };
- avgValue(points); // => 21
- + (Non-Underscore interstitial) Write a function `teamNames(obj)` that retrieves an array of keys that start with `'team_'`:
- var points = {
- team_alpha_ninja: 26,
- team_master_blaster: 20,
- slice: function(){},
- team_wypipo: 20,
- team_untitled2: 18,
- ignore_me: 'lol'
- };
- teamNames(points); // => ['team_alpha_ninja', 'team_master_blaster', 'team_wypipo', 'team_untitled2'] (possibly in a different order)
- + Write a function `pairs(obj)` that retrieves key-value pairs:
- var obj = {
- yes: 'no',
- omg: 'oh my god',
- };
- pairs(obj); // => [['yes', 'no'], ['omg', 'oh my god']] (possibly in a different order)
- + Write a function `invert(obj)` that returns a copy of the object where the keys have become the values and the values the keys:
- var hotNCold = {
- hot: 'cold',
- yes: 'no',
- wrong: 'right',
- black: 'white',
- fight: 'break up',
- kiss: 'make up',
- stay: 'go'
- };
- invert(hotNCold); /* => {
- cold: 'hot',
- no: 'yes',
- right: 'wrong',
- white: 'black',
- 'break up': 'fight',
- 'make up': 'kiss',
- go: 'stay'
- }
- (possibly in a different order) */
- // bonus: if any of the values aren't unique, combine into an array
- var hotNColder = {
- hot: 'cold',
- yes: 'no',
- wrong: 'right',
- black: 'white',
- fight: 'break up',
- kiss: 'make up',
- stay: 'go',
- 'ice cream': 'cold',
- 'break down': 'break up',
- left: 'right',
- antarctica: 'cold',
- 'ice inside your soul': 'cold'
- };
- invert(hotNColder); /* => {
- cold: ['hot', 'ice cream', 'antarctica', 'ice inside your soul'],
- no: 'yes',
- right: ['wrong', 'left'],
- white: 'black',
- 'break up': ['fight', 'break down'],
- 'make up': 'kiss',
- go: 'stay'
- }
- (possibly in a different order) */
- + Write a function `assign(destination, source)` to copy all properties in `source` onto `destination` (fun fact: you're reimplementing `Object.assign()`. Don't use it. Also FYI this is called `_.extend()` on Underscore/Lodash):
- assign({name: 'Abbi'}, {age: 26}); // => {name: 'moe', age: 50}
- // mutates, doesn't copy:
- var obj = {name: 'Abbi', age: 26};
- assign(obj, {job: 'trainer', city: 'NYC'});
- obj // => {name: 'Abbi', age: 26, job: 'trainer', city: 'NYC'}
- // bonus: multiple source objects
- assign({name: 'Abbi'}, {age: 26}, {job: 'trainer', city: 'NYC'}, {bestFriend: 'Ilana'});
- // => {name: 'Abbi', age: 26, job: 'trainer', city: 'NYC'}
- + Write a function `pick(obj, keys)` that returns a copy of `obj`, filtered to only have values for the whitelisted `keys` (an array of strings):
- pick({name: 'Abbi', age: 26, job: 'trainer', city: 'NYC'}, ['name', 'city']) // => {name: 'Abbi', city: 'NYC'}
- + Write a function `omit(obj, keys)` that returns a copy of `obj`, filtered to omit the blacklisted `keys` (an array of strings):
- pick({name: 'Abbi', age: 26, job: 'trainer', city: 'NYC'}, ['age', 'job']) // => {name: 'Abbi', city: 'NYC'}
- + Write a function `defaults(obj, defaults)` that fills in `undefined` properties in `obj` with the value in `defaults`:
- defaults({name: 'Abbi', age: 26, city: 'NYC'}, {job: 'unknown', city: 'nowhere'})
- // => {name: 'Abbi', age: 26, job: 'unknown', city: 'NYC'}
- // should mutate the object
- var abbi = {name: 'Abbi', age: 26, city: 'NYC'};
- defaults(abbi, {job: 'unknown', city: 'nowhere'});
- abbi // => {name: 'Abbi', age: 26, job: 'unknown', city: 'NYC'}
- // bonus: multiple defaults objects
- defaults({name: 'Abbi', age: 26, city: 'NYC'}, {job: 'unknown', city: 'nowhere'}, {website: 'example.com'})
- // => {name: 'Abbi', age: 26, job: 'unknown', city: 'NYC', website: 'example.com'}
- + Write a function `isMatch(obj, props)` that tells you if the keys and values in `props` are contained in `obj`:
- isMatch({name: 'Abbi', age: 26, city: 'NYC', job: 'trainer'}, {name: 'Abbi'}) // => true
- isMatch({name: 'Abbi', age: 26, city: 'NYC', job: 'trainer'}, {name: 'Ilana'}) // => false
- isMatch({name: 'Abbi', age: 26, city: 'NYC', job: 'trainer'}, {}) // => true
- isMatch({name: 'Abbi', age: 26, city: 'NYC', job: 'trainer'}, {bestFriend: 'Ilana'}) // => false
- isMatch({name: 'Abbi', age: 26, city: 'NYC', job: 'trainer'}, {city: 'NYC', bestFriend: 'Ilana'}) // => false
- isMatch({name: 'Abbi', age: 26, city: 'NYC', job: 'trainer'}, {city: 'NYC', age: 26}) // => true
- isMatch({name: 'Abbi', age: 26, city: 'NYC', job: 'trainer'}, {city: 'NYC', age: 27}) // => false
- + Write a function `isEmpty(obj)` that tells you if the object has no keys:
- isEmpty({}) // => true
- isEmpty({yep: 'nope'}) // => false
- + Write a function `pluck(objs, key)` that takes an array of objects and property name and extracts an array of property values:
- var stooges = [{name: 'moe', age: 40}, {name: 'larry', age: 50}, {name: 'curly', age: 60}];
- pluck(stooges, 'name') // => ["moe", "larry", "curly"]
- - Basic object key string access: write a function `sumOfTwoKeys(obj, key1, key2, newKey)` such that
- var obj = {
- lol: 1,
- wut: 100,
- irrelevant: 2
- }
- sumOfTwoKeys(obj, "lol", "wut", "lolwut")
- obj // => { lol: 1, wut: 100, irrelevant: 2, lolwut: 101 }
- var obj2 = {
- lol: 1,
- wut: 2,
- thing: 10
- }
- sumOfTwoKeys(obj, "lol", "wut", "thing")
- obj2 // => { lol: 1, wut: 2, thing: 3 }
- - Histogram (okay, now we're veering into the territory of realistic, useful code/realistic easy interview questions!): write a function `histogram(arrayOfStrings)` such that
- histogram(["aardvark", "words", "best", "words"])
- // => { aardvark: 1, best: 1, words: 2 }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement