This repository was archived by the owner on May 18, 2021. It is now read-only.
forked from stephanielingwood/lab2-beg-track
-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathlab2.js
More file actions
150 lines (117 loc) · 5.18 KB
/
lab2.js
File metadata and controls
150 lines (117 loc) · 5.18 KB
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
'use strict';
/* ********************************************************
LAB 2: LOOPY 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 SentientBeings, one for each language in the
// 'hello' object above.
var klingon = new SentientBeing(); // TODO: make a klingon
var romulan = new SentientBeing(); // TODO: make a romulan
var human = new SentientBeing(); // TODO: make a human
assert(human.sayHello(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: Moar Loops. 20 points.
//
// Implement the following functions. Write at least 3
// assertions for each one
//*********************************************************
function max(array) {
// TODO: return the largest number in the given array
}
// TODO: write three more assertions
assert(max([ 1, 3, 2 ]) === 3, '[1,3,2]');
function variablify(string) {
// TODO: you are given a string with several words in it
// return a corresponding variable name that follows
// javascript conventions
// HINT:
// you might want to use these string methods:
// split(), charAt(), toUpperCase()
// and this array method: join()
}
// TODO: write three more assertions
assert(variablify('one two three') === 'oneTwoThree',
'variablify(\'one two three\')');
//*********************************************************
// PROBLEM 4: Cleanup: 10 points
// Makes sure this file passes jshint and jscs
//
// ./node_modules/.bin/grunt jshint
// ./node_modules/.bin/grunt jscs
//*********************************************************