-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassignment1.js
More file actions
76 lines (60 loc) · 2.17 KB
/
assignment1.js
File metadata and controls
76 lines (60 loc) · 2.17 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
console.log('\n-- Assignment 1 --');
// Write a function power(x,n) such that power(x,n) returns x to the nth power (n is a *positive integer).
// Write four tests to show that the function is performing correctly.
// Part 1 - Function
// Fill in the following function definition:
function power(x, n) {
return Math.pow(x, n);
}
// Tests
console.log('\nPart 1 - Function:');
console.log('\n1^1:');
console.log(power(1,1));
console.log('\n2^2:');
console.log(power(2,2));
console.log('\n3^3:');
console.log(power(3,3));
console.log('\n4^4:');
console.log(power(4,4));
// Part 2 - Testing
// Add four more tests to show that the function is working properly
const errorMsg = 'Math assertion failed.';
console.log('\nPart 2 - Testing:');
console.log('\n4^0.5 = 2');
console.assert(power(4,0.5) == 2, errorMsg);
console.log('\n7^-2 = 0.02040816326530612 (1/49)');
console.assert(power(7,-2) == 0.02040816326530612, errorMsg);
console.log('\n5^2 = 25');
console.assert(power(5,2) == 25, errorMsg);
console.log('\n-7^0.5 = NaN');
// console.log(power(-7,0.5)); // NaN
console.assert(isNaN(power(-7,0.5)), errorMsg);
// Part 3 - Properties of Exponents
// Use your function to show examples of the product property and the power property of exponents.
//
// * Product Property of Exponents
// a^n * a^m == a^(n+m)
//
// * Power Property of Exponents
// (a^n)^m == a^(n*m)
console.log('\n\nPart 3 - Properties of Exponents:');
// Test 2^3 * 2^5 == 2^(3+5) - - Product Property of Exponents
console.log('\n2^3 * 2^5 == 2^(3+5)');
console.log('\n2^3:');
console.log(power(2,3));
console.log('\n2^5:');
console.log(power(2,5));
console.log('\n2^(3+5):');
console.log(power(2,8));
console.log('\n' + power(2,3) + ' * ' + power(2,5) + ' = ' + power(2,(3+5)))
console.assert((power(2,3) * power(2,5)) == power(2,(3+5)), errorMsg);
// (2^4)^6 == 2^(4*6) - - Power Property of Exponents
console.log('\n(2^4)^6 == 2^(4+6)');
console.log('\n2^4:');
console.log(power(2,4));
console.log('\n2^6:');
console.log(power(2,6));
console.log('\n2^(4*6):');
console.log(power(2,(4*6)));
console.log('\n(' + power(power(2,4),6) + ') = ' + power(2,(4*6)))
console.assert(power(power(2,4),6) == power(2,(4*6)), errorMsg);