repl.it
@mcuringa/

conditionals-exercises

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
/**
 * 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 day(d) {
  if(d === 0) {
    return "Sunday";
  }
  else if ( d === 1) {
    return "Monday";
  }


  return "Invalid number for day.";
  
}

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

}

test_day();

// 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.
   
// 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

// 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.<br>
//    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:<br>

//      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.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);
//      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Babel Compiler v6.4.4 Copyright (c) 2014-2015 Sebastian McKenzie
?