repl.it
@mcuringa/

conditionals-exercise-solutions

ES6

No description

fork
loading
main.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/**
 * These are the practice exercises from "Conditionals"
 * in _Think JS_
 * https://mcuringa.github.io/think-js/#conditional-exercises
 */

// 1. Assume the days of the week are numbered 0,1,2,3,4,5,6 from Sunday to Saturday. 
//    Write a function which is given the day number, and it returns the day name (a string).

function dayOfWeek(day) {
  
  if (day === 0) {
    return "Sunday";
  }

  if (day === 1) {
    return "Monday";
  }

  if (day === 2) {
    return "Tuesday";
  }

  if (day === 3) {
    return "Wednesday";
  }
  if (day === 4) {
    return "Thursday";
  }

  if (day === 5) {
    return "Friday";
  }

  if (day === 6) {
    return "Saturday";
  }

  return "Unknown Day";
}

function test_dayOfWeek() {
  console.assert(dayOfWeek(0) === "Sunday", "failed Sunday");
  console.assert(dayOfWeek(1) === "Monday", "failed Monday");
  console.assert(dayOfWeek(2) === "Tuesday", "failed Tuesday");
  console.assert(dayOfWeek(3) === "Wednesday", "failed Wednesday");
  console.assert(dayOfWeek(4) === "Thursday", "failed Thursday");
  console.assert(dayOfWeek(5) === "Friday", "failed Friday");
  console.assert(dayOfWeek(6) === "Saturday", "failed Saturday");

  console.assert(dayOfWeek(22) === "Unknown Day", "failed Unknown Day for 22");
  console.assert(dayOfWeek(-1) === "Unknown Day", "failed Unknown Day for -1");
}

test_dayOfWeek();

// 2. You go on a wonderful vacation leaving on day number 3 (a Wednesday). You
//    return home after 22 nights sleep. What day of the week is it? Write a general
//    version of the program which asks for the starting day number, and the length
//    of your stay, and it will tell you the name of day of the week you will return
//    on. You might want to use the ``%`` mod operator. You can compose this
//    function from the one you wrote in exercise 1.

function dayInFuture(start, nightsAway) {
  let dayHome = (start + nightsAway) % 7;

  return dayOfWeek(dayHome);
}

function test_dayInFuture() {
  console.assert(dayInFuture(0,0) === "Sunday", "failed 0 days");
  console.assert(dayInFuture(0,14) === "Sunday", "failed 14 days");
  console.assert(dayInFuture(6,2) === "Monday", "failed 2 days, starting on Sat");
  console.assert(dayInFuture(6,8) === "Sunday", "failed 8 days, starting on Sat");
  console.assert(dayInFuture(3,16) === "Friday", "failed 16 days, starting on Wed");
  
}


test_dayInFuture();

// 3. Give the logical opposites of these conditions
//     a. ``a > b`` 
//     b. ``a >= b``
//     c. ``a >= 18  &&  day === 3``
//     d. ``a >= 18  &&  day !== 3``
    
// 4. What do these expressions evaluate to?
//     a. ``3 === 3``
//     b. ``3 !== 3``
//     c. ``3 >= 4``
//     d. ``!(3 < 4)``
    
// 5. Write a function which is given an exam score, 
//    and it returns a string --- the letter grade for that mark --- according to this scheme:   
   
//     Score     Grade
//     -------   --------------
//     90-100    A
//     80-89     B
//     70-79     C
//     65-69     D
//     <65       F

function getGrade(score) {
  if (score > 100) {
    return "unknown score";
  }

  if (score >= 90) {
    return "A";
  }

  if (score >= 80) {
    return "B";
  }

  if(score >= 70) {
    return "C";
  }

  if(score >= 65) {
    return "D";
  }

  return "F";

}

function test_getGrade() {
  console.assert(getGrade(92) === "A", "92 failed");
  console.assert(getGrade(80) === "B", "80 failed");
  console.assert(getGrade(79) === "C", "79 failed");
  console.assert(getGrade(65) === "D", "65 failed");
  console.assert(getGrade(64) === "F", "64 failed");
  console.assert(getGrade(-10) === "F", "-10 failed");
  console.assert(getGrade(100) === "A", "100 failed");
  console.assert(getGrade(101) === "unknown score", "101 failed");
}

test_getGrade();

// 6. (hard bonus) Write a function ``isRightAngled`` which, given the length of three sides
//    of a triangle, will determine whether the triangle is right-angled. Assume
//    that the third argument to the function is always the longest side. It will
//    return ``true`` if the triangle  is right-angled, or ``false`` otherwise.
//    Hint: Floating point arithmetic is not always exactly accurate,
//    so it is not safe to test floating point numbers for equality. 
//    If a good programmer wants to know whether
//    ``x`` is equal or close enough to ``y``, they would probably code it up as:

//      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.javascript}    
//      if (Math.abs(x-y) < 0.000001) {
//        // x is approximately equal to y
//      }     
//      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//    If you're intrigued by why floating point arithmetic is sometimes inaccurate, on a piece
//    of paper, divide 10 by 3 and write down the decimal result. You'll find it does not terminate,
//    so you'll need an infinitely long sheet of paper. The *representation* of numbers in computer 
//    memory or on your calculator has similar problems: memory is finite, and some digits may have
//    to be  discarded, so small inaccuracies creep in. Try this script:

//      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.javascript .numberLines}   
//      let a = Math.sqrt(2.0);
//      console.log(a, a*a);
//      console.log(a*a === 2.0);
//      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~


function isRightAngled(a, b, c) {

  let delta = Math.abs( (a**2 + b**2) - c**2);
  let error = 0.000001;

  return delta < error;
}

function test_isRightAngled() {
  console.assert(isRightAngled(3, 4, 5) === true, "failed 3-4-5 right angle");
  console.assert(isRightAngled(5, 12, 13) === true, "failed 5-12-13 right angle");
  // test for rounding
  console.assert(isRightAngled(22, 47, 51.89412298) === true, "failed 22-47-51.89412298 right angle");
  //test detect non-right triangle
  console.assert(isRightAngled(5, 6, 7) === false, "failed 5-6-7 right angle");

}

test_isRightAngled();
Babel Compiler v6.4.4 Copyright (c) 2014-2015 Sebastian McKenzie
?