From 9b3b5f210beff95e22a9d7c4dd8e52ff59c7c77a Mon Sep 17 00:00:00 2001 From: Brett Dawidowski Date: Mon, 11 Mar 2019 23:46:51 -0400 Subject: [PATCH] [Palindrome] #2 Completed --- exercises/palindrome/index.js | 32 +++++++++++++++++++++++++++++++- exercises/reversestring/index.js | 17 +++++++++++------ 2 files changed, 42 insertions(+), 7 deletions(-) diff --git a/exercises/palindrome/index.js b/exercises/palindrome/index.js index 58115ed243..8fe172f755 100644 --- a/exercises/palindrome/index.js +++ b/exercises/palindrome/index.js @@ -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; + + + diff --git a/exercises/reversestring/index.js b/exercises/reversestring/index.js index b448951556..5144b19d69 100644 --- a/exercises/reversestring/index.js +++ b/exercises/reversestring/index.js @@ -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(''); @@ -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, '');