#!/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) } );