Sha256: e3299d547b100f92c84a0e07ff8033dfc59dc0288d01bd751347fa735a7f887e

Contents?: true

Size: 873 Bytes

Versions: 118

Compression:

Stored size: 873 Bytes

Contents

var Diamond = function () {
  this.makeDiamond = function (input) {
    var inputIndex =  input.charCodeAt() - 65;
    var output = '';
    for (var i = 0; i <= inputIndex; i++) {
      output += getLine(inputIndex, i);
    }
    for (var j = inputIndex - 1; j >= 0; j--) {
      output += getLine(inputIndex, j);
    }
    return output;
  };

  var getLine = function (inputIndex, index) {
    var difference = inputIndex - index;
    return spaceTimes(difference) + printAlphabets(index) + spaceTimes(difference) + '\n';
  };

  var printAlphabets = function (index) {
    var character = 65 + index;
    if (index === 0) {
      return 'A';
    }

    return String.fromCharCode(character) + spaceTimes(((index - 1) * 2) + 1) + String.fromCharCode(character);
  };

  var spaceTimes = function (times) {
    return ' '.repeat(times);
  };
};

module.exports = Diamond;

Version data entries

118 entries across 118 versions & 1 rubygems

Version Path
trackler-2.2.1.139 tracks/javascript/exercises/diamond/example.js
trackler-2.2.1.138 tracks/javascript/exercises/diamond/example.js
trackler-2.2.1.137 tracks/javascript/exercises/diamond/example.js
trackler-2.2.1.136 tracks/javascript/exercises/diamond/example.js
trackler-2.2.1.135 tracks/javascript/exercises/diamond/example.js
trackler-2.2.1.134 tracks/javascript/exercises/diamond/example.js
trackler-2.2.1.133 tracks/javascript/exercises/diamond/example.js
trackler-2.2.1.132 tracks/javascript/exercises/diamond/example.js
trackler-2.2.1.131 tracks/javascript/exercises/diamond/example.js
trackler-2.2.1.130 tracks/javascript/exercises/diamond/example.js
trackler-2.2.1.129 tracks/javascript/exercises/diamond/example.js
trackler-2.2.1.128 tracks/javascript/exercises/diamond/example.js
trackler-2.2.1.127 tracks/javascript/exercises/diamond/example.js
trackler-2.2.1.126 tracks/javascript/exercises/diamond/example.js
trackler-2.2.1.125 tracks/javascript/exercises/diamond/example.js
trackler-2.2.1.124 tracks/javascript/exercises/diamond/example.js
trackler-2.2.1.123 tracks/javascript/exercises/diamond/example.js
trackler-2.2.1.122 tracks/javascript/exercises/diamond/example.js
trackler-2.2.1.121 tracks/javascript/exercises/diamond/example.js
trackler-2.2.1.120 tracks/javascript/exercises/diamond/example.js