Skip to content

Commit

Permalink
solution and test llipio#64
Browse files Browse the repository at this point in the history
  • Loading branch information
msach22 committed Jun 7, 2017
1 parent 8b2beaf commit 389a99c
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
43 changes: 43 additions & 0 deletions solutions/64.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Manik Sachdeva: msach22
// reverse word order in a sentence
// Input: "I am the king"
// Output: "king the am I"

/**
* @param {string} sentence - string
* @returns {string} newString - reversed string
*/

const reverseInput = (string) => {
let newString = '';
for (let i = string.length - 1; i >= 0; i--) {
newString += string[i];
};
return newString;
}

/**
* @param {string} sentence - a sentence
* @returns {string} result - reveresed word order
*/

const solution = (sentence) => {
if (sentence.length < 2) {
return sentence;
}
let subString = '';
let result = [];
for (let j = sentence.length - 1; j >= -1; j--) {
if (sentence[j] === ' ' || j === -1) {
result.push(reverseInput(subString));
subString = '';
} else {
subString += sentence[j];
}
}
return result.join(' ');
};

module.exports = {
solution
};
12 changes: 12 additions & 0 deletions test/64.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const expect = require('chai').expect;
let solution = require('../solutions/64').solution;
// solution = require('./yourSolution').solution;

describe('reverse word order in a sentence', () => {
it('should reverse the word order', () => {
expect(solution('I am the king')).eql('king the am I');
});
it('should return I', () => {
expect(solution('I')).eql('I');
});
});

0 comments on commit 389a99c

Please sign in to comment.