-
Notifications
You must be signed in to change notification settings - Fork 1
/
quiz.txt
112 lines (85 loc) · 3.54 KB
/
quiz.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Name: Charles Tirrell
//////////////////////////////////////////////////////////////// Question 1
const labs = [
{location: 'Bewkes 107', seats: 15},
{location: 'Bewkes 109', seats: 25},
{location: 'Bewkes 144', seats: 7}
];
// A. Generate a new array containing the seat numbers of all the labs (by invoking the built-in map method).
const seats = labs.map(x => x.seats);
//Same as:
//const seats = lab.map(function(x) {
// return x.seats;
//});
// B. Generate a new array containing only the labs with more than 10 seats (by invoking the built-in filter method).
const bigLabs = labs.filter(x => (x.seats > 10));
// C. Sort the existing array in order of (increasing) seat numbers (by invoking the built-in sort method).
labs.sort((a,b) => a.seats - b.seats);
//////////////////////////////////////////////////////////////// Question 2
const numbers = [5, 4, 3, 2, 1];
// A. Get the first even number (by invoking the built-in find method).
//find gets the first element in the array that satisfies the condition
const found = numbers.find(function(element){
return element % 2 === 0;
});
console.log(found);
// B. Define a standalone find function. The array is its first argument and the callback is its second argument.
const find = function(array, callback){
for (const element of array){
if (callback(element)){
return element;
}
}
}
console.log(find(numbers, function(element){
for (const element of numbers){
if (element % 2 === 0){
return element;
}
}
}));
//////////////////////////////////////////////////////////////// Question 3
// This creates the user object described on the quiz.
// It has a question method you can call in part A.
const readline = require('readline');
const user = readline.createInterface({
input: process.stdin,
output: process.stdout
});
// A. Make small talk, using traditional callbacks.
user.question("What is your name?", function(response){
console.log("Hello " + response + ".");
user.question("How are you doing?", function(response){
console.log("I am also " + response + ".")
});
});
// The user.question method doesn't actually return a promise, so here is a question function that does.
// Call this question function in part B instead of calling the user.question method.
const question = function(prompt) {
return new Promise(resolve => user.question(prompt, resolve));
};
// B. Make small talk again, using promises.
user.question("What is your name?")
.then(function(resolve){
console.log("Hello" + resolve + ".");
return user.question("How are you doing?");
})
.then(resolve => console.log("I am also" + resolve + "."))
.catch(error => console.log(error.stack));
//////////////////////////////////////////////////////////////// Question 4
// Question 4 is commented out because otherwise it would interfere with Question 3.
// When you're ready to work on Question 4, uncomment it and comment out Question 3.
// This function returns a promise, which produces 42 after an asynchronous delay of one second.
const f1 = function() {
return new Promise(resolve => setTimeout(() => resolve(42), 1000));
};
// This function returns a promise, which produces 24 after an asynchronous delay of one second.
const f2 = function() {
return new Promise(resolve => setTimeout(() => resolve(24), 1000));
};
// Run f1 and f2 in parallel and log 'f1', 'f2', or 'equal' to indicate which function returned the larger result.
const funcs = {f1, f2};
Promise.all(funcs)
.then(funcs.sort(a,b) => (a - b))
.then(console.log(funcs[0]))
.catch(error => console.log(error.stack));