Skip to content

Commit

Permalink
[Palindrome] StephenGrider#2 Completed
Browse files Browse the repository at this point in the history
  • Loading branch information
Brett Dawidowski committed Mar 12, 2019
1 parent c25bee3 commit 9b3b5f2
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
32 changes: 31 additions & 1 deletion exercises/palindrome/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,36 @@
// palindrome("abba") === true
// palindrome("abcdefg") === false

function palindrome(str) {}
/**
* Given a string, return true if the string is a palindrome
* or false if it is not.
* @param {string} str
* @returns {boolean}
*/
function palindrome(str) {

/**
* Solution #1:
* `Array.prototype.reverse()` === str
*/
const reversed = str
.split('')
.reverse()
.join('');
return str === reversed;

/**
* Solution #2:
* `Array.prototype.every()` not ideal because it does twice the work.
* [@link](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every)
*/

return str.split('').every((char, i) => char === str[str.length - i - 1])


}

module.exports = palindrome;



17 changes: 11 additions & 6 deletions exercises/reversestring/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,20 @@
// --- Examples
// reverse('apple') === 'leppa'
// reverse('hello') === 'olleh'
reverse('Greetings!') === '!sgniteerG'

// reverse('Greetings!') === '!sgniteerG'


/**
* Given a string, return a new string with the reversed
* order of characters
* @param {string} str
* @returns {string}
*/
function reverse(str) {
/**
* Solution #1:
* Array.prototype.reverse()
* @link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reverse
* `Array.prototype.reverse()`
* [@link](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reverse)
*/
return str.split('').reverse().join('');

Expand All @@ -32,8 +37,8 @@ function reverse(str) {

/**
* Solution #3:
* Array.prototype.reduce()
* @link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce
* `Array.prototype.reduce()`
* [@link](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce)
*/
return str.split('').reduce((rev, char) => char + rev, '');

Expand Down

0 comments on commit 9b3b5f2

Please sign in to comment.