Skip to content
This repository has been archived by the owner on Nov 7, 2020. It is now read-only.

Add answers for strings #103

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions app/strings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
exports = (typeof window === 'undefined') ? global : window;

exports.stringsAnswers = {
reduceString: function(str, amount) {
var newStr = [];
var prevLetter;
var curLetter;
var curCount = 0;
for (var i=0; i<str.length; i++) {
curLetter = str[i];
// Looking at a currently repeated letter
// or the first letter
if (curLetter === prevLetter || !prevLetter) {
if (curCount < amount) {
newStr.push(curLetter);
}
curCount++;
} else { // Looking at a new letter
newStr.push(curLetter);
curCount = 1;
}

prevLetter = curLetter;
}
return newStr.join('');
},

wordWrap: function(str, cols) {
var newStr = [];
var words = str.split(' ');
var curLine = [];

for (var i=0; i<words.length; i++) {
// first word in the line
if (!curLine.length) {
curLine.push(words[i]);
} else {
// What the current line length would be if the current word as added
// including the space in between the words
var curLineLetterCount = curLine.join('').length;
var lineLengthWithWord = curLineLetterCount + words[i].length + 1;
if (lineLengthWithWord <= cols) {
curLine.push(' ');
curLine.push(words[i]);
} else { // current word needs to go on a new line
curLine.push('\n');
newStr.push(curLine.join(''));

curLine = [];
curLine.push(words[i]);
}
}
}

// handle the last word
if (curLine.length) {
newStr.push(curLine.join(''));
}

return newStr.join('');
},

reverseString: function(str) {
var revStrLetters = [];
for (var i=0; i<str.length; i++) {
revStrLetters.unshift(str[i]);
}
return revStrLetters.join('');
}
};