repl.it
@mcuringa/

function-exercise-solutions

ES6

#thinkjs #functions #practice

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
// You can use this repl as a template
// for the chapter 3 function exercises
// from Think JS
// https://mcuringa.github.io/think-js/#exercises

// I've created a skeleton for the first 2
// functions. The rest you will have to do
// on your own.


// problem 1
// Write a void (non-fruitful) function to that prints out a “hello” message. Your function should declare 3 parameters: firstName, lastName, and title. title will be Mr., Ms., Dr., etc. The function should print a message like this one: Hello Dr. Matthew Curinga.

function hello(firstName, lastName, title) {

  firstName = "Matt";
  console.log("Hello " + title + " " + firstName  + " " +  lastName);

}

// hello("Diego", "Curinga", "Dr.");


// problem 2
// Write a function half(num) which
// returns the value of num divided by 2.
function half(num) {
  let x = num / 2;
  return x;
}

// half(4); // expects 2

// problem 3
// Write a function triple(num) which return num * 3.

function triple(num) {
  return num * 3;
}

// let x = triple(5);
// console.log(x * 2);


// problem 4
// Write a function areaOfACircle(r) which returns 
// the area of a circle of radius r. For the value
// of PI, use the constant Math.PI


function areaOfACircle(r) {
  let area = Math.PI * r**2;
  return area;
}

// areaOfACircle(10);

// problem 5
// Write a function hypotenuse(a, b) which calculates 
// the hypotonuse of a right triangle when given the length 
// of sides a and b. Use the Pythagorean theorem 
// a^2 + b^2 = c^2.
// Note, you will need to be able to calculate square roots
// to solve this problem. You can use the build in math
// function Math.sqrt.

function hypotenuse(a, b) {
  let c2 = a**2 + b**2;
  return Math.sqrt(c2);
}

// hypotenuse(5, 3);



// problem 6
// Write a function called distance(x1, y1, x2, y2) 
// which calculates the distance between the point
// at (x1, y1) and (x2, y2) on a Cartesian Plane.
// You can find the formula for this at
// http://www.mathsisfun.com/algebra/distance-2-points.html

function distance(x1, y1, x2, y2) {
  let a = x1 - x2;
  let b = y1 - y2;
  return hypotenuse(a, b);
}

distance(0,0, 3,4);
Babel Compiler v6.4.4 Copyright (c) 2014-2015 Sebastian McKenzie
?