Practice code questions Flashcards

1
Q

Find the Smallest and Biggest Numbers
Create a function that takes an array of numbers and return both the minimum and maximum numbers, in that order.

Examples
minMax([1, 2, 3, 4, 5]) ➞ [1, 5]

minMax([2334454, 5]) ➞ [5, 2334454]

minMax([1]) ➞ [1, 1]
Notes
All test arrays will have at least one element and are valid.

A
function minMax(arr) {
  return [Math.min(...arr), Math.max(...arr)];
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Promises III: Native Promise, Introducing the Executor
Promises are just objects that contain the outcome of asynchronous operations. So when do you use one? When you want to control the outcome of an asynchronous operation. All you have to do is wrap the asynchronous function with a promise constructor.

The promise constructor requires you to pass a function called the executor which takes two parameters, resolve and reject. Both are functions that you use to pass or reject a value that is usually the result of the async operation. Here’s an example of a simple promise:

setTimeout is a browser API that is very commonly used in tutorials to represent async operations. After 1000ms has passed, we call the callback function in setTimeout() and pass a string “edabit” to the resolve function.

Create a simple promise and pass the resolve function a string value of your choice. Use the setTimeout function as your asynchronous operation. Your setTimeout() function should not exceed 1000ms. Store the promise inside a variable named promise.

Notes
Check the Resources tab for more info on promises.

A
let promise = new Promise( (resolve, reject) => {
  setTimeout(( ) => {
     resolve("edabit")
  }, 1000)
})
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Is it Time for Milk and Cookies?
Christmas Eve is almost upon us, so naturally we need to prepare some milk and cookies for Santa! Create a function that accepts a Date object and returns true if it’s Christmas Eve (December 24th) and false otherwise. Keep in mind JavaScript’s Date month is 0 based, meaning December is the 11th month while January is 0.

Examples
timeForMilkAndCookies(new Date(2013, 11, 24)) ➞ true

timeForMilkAndCookies(new Date(2013, 0, 23)) ➞ false

timeForMilkAndCookies(new Date(3000, 11, 24)) ➞ true
Notes
Dates are zero zero based (see resources).
All test cases contain valid dates.

A
function timeForMilkAndCookies(date) {
	return date.getDate() == 24 && date.getMonth() == 11;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly