Skip to content

Commit

Permalink
Merge pull request #3 from msach22/sentence
Browse files Browse the repository at this point in the history
solution and test for llipio#64
  • Loading branch information
msach22 authored Jun 6, 2017
2 parents 8b2beaf + d9f5c7e commit 1a096fd
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
40 changes: 40 additions & 0 deletions solutions/64.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Manik Sachdeva: msach22
// reverse word order in a sentence
// Input: "I am the king"
// Output: "king the am I"
/*
* @param {string} sentence - a sentence
* @returns {string} result - reveresed word order
*/

const reverseInput = (string) => {
let newString = '';
for (let i = string.length - 1; i >= 0; i--) {
newString += string[i];
};
return newString;
}
const solution = (sentence) => {
if (sentence.length < 2) {
return "Incorrect Data Format";
}
let newString = reverseInput(sentence);
let subString = '';
let result = [];
for (let j = 0; j < newString.length; j++) {
if (newString[j] === ' ' ) {
result.push(reverseInput(subString));
subString = '';
} else if (j === newString.length - 1) {
subString += newString[j];
result.push(reverseInput(subString));
} else {
subString += newString[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 IDF', () => {
expect(solution('I')).eql('Incorrect Data Format');
});
});

0 comments on commit 1a096fd

Please sign in to comment.