Sha256: ccdd392d87c6cdecc62fc5e6cd3d63f8a5ac78049cc30517a692f98a002df225

Contents?: true

Size: 864 Bytes

Versions: 116

Compression:

Stored size: 864 Bytes

Contents

var BigInt = require('./big-integer');

/**
 * @author github.com/nonsensery
 * @class Grains
 *
 * @classdesc Computes the number of grains on the squares of a
 * chess board, starting with one grain on the first
 * square, and doubling with each successive square.
 */
function Grains() {
  // no op!
}

/**
 * Gets the number of grains on the nth square.
 * @param {number} num - the value to square
 * @return {number} the square of num
 */
Grains.prototype.square = function (num) {
  return new BigInt(2).pow(num - 1).toString();
};

/**
 * Gets the total number of grains on all squares.
 * @return {string} the total
 */
Grains.prototype.total = function () {
  var total = new BigInt(0);

  for (var squareNum = 1; squareNum <= 64; ++squareNum) {
    total = total.add(this.square(squareNum));
  }

  return total.toString();
};

module.exports = Grains;

Version data entries

116 entries across 116 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/javascript/exercises/grains/example.js
trackler-2.2.1.179 tracks/javascript/exercises/grains/example.js
trackler-2.2.1.178 tracks/javascript/exercises/grains/example.js
trackler-2.2.1.177 tracks/javascript/exercises/grains/example.js
trackler-2.2.1.176 tracks/javascript/exercises/grains/example.js
trackler-2.2.1.175 tracks/javascript/exercises/grains/example.js
trackler-2.2.1.174 tracks/javascript/exercises/grains/example.js
trackler-2.2.1.173 tracks/javascript/exercises/grains/example.js
trackler-2.2.1.172 tracks/javascript/exercises/grains/example.js
trackler-2.2.1.171 tracks/javascript/exercises/grains/example.js
trackler-2.2.1.170 tracks/javascript/exercises/grains/example.js
trackler-2.2.1.169 tracks/javascript/exercises/grains/example.js
trackler-2.2.1.167 tracks/javascript/exercises/grains/example.js
trackler-2.2.1.166 tracks/javascript/exercises/grains/example.js
trackler-2.2.1.165 tracks/javascript/exercises/grains/example.js
trackler-2.2.1.164 tracks/javascript/exercises/grains/example.js
trackler-2.2.1.163 tracks/javascript/exercises/grains/example.js
trackler-2.2.1.162 tracks/javascript/exercises/grains/example.js
trackler-2.2.1.161 tracks/javascript/exercises/grains/example.js
trackler-2.2.1.160 tracks/javascript/exercises/grains/example.js