Skip to content

Instantly share code, notes, and snippets.

@malisetti
Created December 19, 2018 11:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save malisetti/fe2d6c7747eab6d5f631aa7c642d5619 to your computer and use it in GitHub Desktop.
Save malisetti/fe2d6c7747eab6d5f631aa7c642d5619 to your computer and use it in GitHub Desktop.
"use strict";
const assert = require("assert");
(() => {
const inouts = {
Code: "CCoCodCode",
abc: "aababc",
ab: "aab"
};
for (const input in inouts) {
if (inouts.hasOwnProperty(input)) {
const output = inouts[input];
assert.equal(output, stringSplosion(input), "fail");
}
}
})();
/**
* Given a non-empty string like "Code" return a string like "CCoCodCode".
stringSplosion("Code") → "CCoCodCode"
stringSplosion("abc") → "aababc"
stringSplosion("ab") → "aab"
*/
function stringSplosion(str) {
let strx = "";
const l = str.length;
for (let i = 1; i <= l; i++) {
strx += str.substr(0, i);
}
return strx;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment