Assertions
This page is under construction. Please come back later.
#!/usr/bin/env node; const Utils = require('./Utils'); const assert = require('assert'); const TEST_BASE = 0.9; function exponentiation(base, powerArg) { assert(powerArg >= 0, "powerArg >= 0"); let power = powerArg; let result = 1; while (power > 0) { result *= base; --power; } return result; } const main = async () => { for (let p = 10; p >= -1; --p) { console.log(Utils.format("{0:f} ^ {1:d} = {2:f}", TEST_BASE, p, exponentiation(TEST_BASE, p))); } } main().catch( e => { console.error(e) } );
Output
$ node Assertions1.js
0.9 ^ 10 = 0.34867844010000015
0.9 ^ 9 = 0.38742048900000015
0.9 ^ 8 = 0.43046721000000016
0.9 ^ 7 = 0.47829690000000014
0.9 ^ 6 = 0.5314410000000002
0.9 ^ 5 = 0.5904900000000002
0.9 ^ 4 = 0.6561000000000001
0.9 ^ 3 = 0.7290000000000001
0.9 ^ 2 = 0.81
0.9 ^ 1 = 0.9
0.9 ^ 0 = 1
AssertionError [ERR_ASSERTION]: powerArg >= 0
at exponentiation (/Users/rich/Desktop/Resources/pureprogrammer/js/examples/Assertions1.js:8:2)
at main (/Users/rich/Desktop/Resources/pureprogrammer/js/examples/Assertions1.js:20:67)
at Object.<anonymous> (/Users/rich/Desktop/Resources/pureprogrammer/js/examples/Assertions1.js:23:1)
at Module._compile (node:internal/modules/cjs/loader:1256:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
at Module.load (node:internal/modules/cjs/loader:1119:32)
at Module._load (node:internal/modules/cjs/loader:960:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
at node:internal/main/run_main_module:23:47 {
generatedMessage: false,
code: 'ERR_ASSERTION',
actual: false,
expected: true,
operator: '=='
}
#!/usr/bin/env node; const Utils = require('./Utils'); const assert = require('assert'); const TEST_BASE = 0.9; function exponentiation(base, powerArg) { assert(powerArg >= 0, "exponentiation() power must be non-negative, was " + powerArg); let power = powerArg; let result = 1; while (power > 0) { result *= base; --power; } return result; } const main = async () => { for (let p = 10; p >= -1; --p) { console.log(Utils.format("{0:f} ^ {1:d} = {2:f}", TEST_BASE, p, exponentiation(TEST_BASE, p))); } } main().catch( e => { console.error(e) } );
Output
$ node Assertions2.js
0.9 ^ 10 = 0.34867844010000015
0.9 ^ 9 = 0.38742048900000015
0.9 ^ 8 = 0.43046721000000016
0.9 ^ 7 = 0.47829690000000014
0.9 ^ 6 = 0.5314410000000002
0.9 ^ 5 = 0.5904900000000002
0.9 ^ 4 = 0.6561000000000001
0.9 ^ 3 = 0.7290000000000001
0.9 ^ 2 = 0.81
0.9 ^ 1 = 0.9
0.9 ^ 0 = 1
AssertionError [ERR_ASSERTION]: exponentiation() power must be non-negative, was -1
at exponentiation (/Users/rich/Desktop/Resources/pureprogrammer/js/examples/Assertions2.js:8:2)
at main (/Users/rich/Desktop/Resources/pureprogrammer/js/examples/Assertions2.js:20:67)
at Object.<anonymous> (/Users/rich/Desktop/Resources/pureprogrammer/js/examples/Assertions2.js:23:1)
at Module._compile (node:internal/modules/cjs/loader:1256:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
at Module.load (node:internal/modules/cjs/loader:1119:32)
at Module._load (node:internal/modules/cjs/loader:960:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
at node:internal/main/run_main_module:23:47 {
generatedMessage: false,
code: 'ERR_ASSERTION',
actual: false,
expected: true,
operator: '=='
}
#!/usr/bin/env node; const Utils = require('./Utils'); const assert = require('assert'); const TEST_BASE = 0.9; function exponentiation(base, powerArg) { assert(powerArg >= 0, "exponentiation() power must be non-negative, was " + powerArg); let power = powerArg; let result = 1; while (power > 0) { result *= base; --power; } // This line will make the result incorrect. result = -result; // If power is odd and base is negative, then result should be negative. // Otherwise result should be positive. assert((power & 1) === 1 && (base < 0) ? result < 0 : result >= 0, "Postcondition Failed: Result is wrong sign"); return result; } const main = async () => { for (let p = 10; p >= -1; --p) { console.log(Utils.format("{0:f} ^ {1:d} = {2:f}", TEST_BASE, p, exponentiation(TEST_BASE, p))); } } main().catch( e => { console.error(e) } );
Output
$ node Assertions3.js
AssertionError [ERR_ASSERTION]: Postcondition Failed: Result is wrong sign
at exponentiation (/Users/rich/Desktop/Resources/pureprogrammer/js/examples/Assertions3.js:19:2)
at main (/Users/rich/Desktop/Resources/pureprogrammer/js/examples/Assertions3.js:25:67)
at Object.<anonymous> (/Users/rich/Desktop/Resources/pureprogrammer/js/examples/Assertions3.js:28:1)
at Module._compile (node:internal/modules/cjs/loader:1256:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
at Module.load (node:internal/modules/cjs/loader:1119:32)
at Module._load (node:internal/modules/cjs/loader:960:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
at node:internal/main/run_main_module:23:47 {
generatedMessage: false,
code: 'ERR_ASSERTION',
actual: false,
expected: true,
operator: '=='
}
Questions
- {{Who's on first?}}
- {{Who's on second?}}
- {{Who's on third?}}
Projects
More ★'s indicate higher difficulty level.
- Base Conversion
- Fast Exponentiation
- Fibonacci Numbers with Memoization
- Football Passer Rating (with Assertions)
- Julian Day Number
- Number of Primes (Estimate)
- Ordinal Date
- Ordinal Date (Revisited)
- Shuffle Deck
- Significant Digits
References
- [[JavaScript Language Reference]], Mozilla Developer Network
- [[Mozilla Developer Network]]
- Download [[node.js]]
- [[w3schools.com]]