forked from codefellows/F2-JavaScript-lab2-adv-track
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lab2.js
154 lines (122 loc) · 5.34 KB
/
lab2.js
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
'use strict';
// LAB 2: SORTING AND CAMPY SCI-FI
// Welcome to Lab 2 =)
// Be sure to read all the comments!
// All of the instructions are inline with the assignment below.
// Look for the word TODO in comments. Each TODO will have a
// description of what is required.
// To run this file (in the terminal) use: node lab2.js
//*********************************************************
// SETUP
//*********************************************************
// We're going to use this special assert method again to
// test our code
function assert(expression, failureMessage) {
if (!expression) {
console.log('assertion failure: ', failureMessage);
}
}
//*********************************************************
// PROBLEM 1: The Blob. 20 points
//*********************************************************
/* Dowington, PA had 1000 citizens on the night the blob escaped
its meteorite. At first, the blob could only find and consume
Pennsylvanians at a rate of 1/hour. However, each time it digested
someone, it became faster and stronger: adding to its consumption
rate by 1 person/hour.
for the... | starting rate of | persons consumed |
| consumption | that hour |
--------------------|------------------|------------------|
first hour | 1/hour | 1 |
second hour | 2/hour | 2 |
third hour | 3/hour | 3 |
fourth hour | 4/hour | 4 |
TODO: First, make a constructor function, called Blob, that makes blobs.
TODO: Next, create an instance of Blob named blob.
TODO: Then, use a loop to calculate how long it took the blob to finish
with Dowington.
*/
var hoursSpentInDowington; // TODO: assign me the value of the
// above calculation (how long it took
// the blob to eat Dowington)
// Now, write a method that takes a population for an arbitrary
// town, and the starting consumption rate, and returns the number
// of hours the blob needs to ooze its way through that town.
function hoursToOoze(population, peoplePerHour) {
// TODO: implement me based on the instructions above.
// Be sure to then assign me to the Blob's prototype.
}
assert(blob.hoursToOoze(0, 1) === 0, 'no people means no time needed.');
assert(blob.hoursToOoze(1000, 1) === hoursSpentInDowington,
'hoursSpentInDowington should match hoursToOoze\'s result for 1000');
// TODO: write three more assertions like the two above, testing out
// the hoursToOoze method.
//*********************************************************
// PROBLEM 2: Universal Translator. 20 points
//*********************************************************
var hello = {
klingon: 'nuqneH', // home planet is Qo'noS
romulan: 'Jolan\'tru', // home planet is Romulus
'federation standard': 'hello' // home planet is Earth
};
// TODO: define a constructor that creates objects to represent
// sentient beings. They have a home planet, a language that they
// speak, and method (that you'll place on the prototype) called
// sayHello.
function SentientBeing () {
// TODO: specify a home planet and a language
// you'll need to add parameters to this constructor
}
// sb is a SentientBeing object
function sayHello (sb) {
// TODO: say hello prints out (console.log's) hello in the
// language of the speaker, but returns it in the language
// of the listener (the sb parameter above).
// use the 'hello' object at the beginning of this exercise
// to do the translating
//TODO: put this on the SentientBeing prototype
}
// TODO: create three subclasses of SentientBeing, one for each
// species above (Klingon, Human, Romulan).
assert((new Human()).sayHello(new Klingon()) === 'nuqneH',
'the klingon should hear nuqneH');
// TODO: write five more assertions, to complete all the possible
// greetings between the three types of sentient beings you created above.
//*********************************************************
// PROBLEM 3: Sorting. 20 points.
//
// Implement the following functions. Write at least 2
// assertions for each one (the assertions are how you
// will test your code)
//*********************************************************
function lastLetterSort(stringArray) {
function byLastLetter(a, b) {
//TODO: implement me. sort the strings in alphabetical
// order using their last letter
// Read this about how the sort function works:
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
// this byLastLetter function is a "compare function"
// And check out the "comparing strings" section here:
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String
}
stringArray.sort(byLastLetter);
}
function sumArray(numberArray) {
var sum = 0;
// TODO: implement me using forEach
return sum;
}
function sumSort(arrayOfArrays) {
arrayOfArrays.sort(function(item) {
// TODO: implement me using sumArray
// order the arrays based on the sum of the numbers
// inside each array
});
}
//*********************************************************
// PROBLEM 4: Cleanup: 10 points
// Makes sure this file passes jshint and jscs
//
// ./node_modules/.bin/grunt jshint
// ./node_modules/.bin/grunt jscs
//*********************************************************