Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
The Nth power array[N] OR -1 solution in Clear category for [old] Index Power by alexander.gorelyshev
"use strict";
// Given an out of bounds index, JS arrays return null, which is a falsey value.
// Remember that no number produces a 0 when risen to any power.
// Use these facts to return either a positive integer (which is truthy), or a -1.
const indexPower = (array, n) => array[n] ** n || -1;
var assert = require('assert');
if (!global.is_checking) {
console.log('Example:')
console.log(indexPower([1, 2, 3, 4], 2))
assert.equal(indexPower([1, 2, 3, 4], 2), 9, "Square");
assert.equal(indexPower([1, 3, 10, 100], 3), 1000000, "Cube");
assert.equal(indexPower([0, 1], 0), 1, "Zero power");
assert.equal(indexPower([1, 2], 3), -1, "IndexError");
console.log("Coding complete? Click 'Check' to review your tests and earn cool rewards!");
}
June 3, 2019
Comments: