From a80c616bd031737a91e7a79f51633c38ea63ee36 Mon Sep 17 00:00:00 2001 From: Jamison Date: Tue, 13 Mar 2018 10:35:47 -0700 Subject: [PATCH 1/3] Built out the isNumberPalindrome function as specified --- isNumberPalindrome.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/isNumberPalindrome.js b/isNumberPalindrome.js index 699a785..06c2921 100644 --- a/isNumberPalindrome.js +++ b/isNumberPalindrome.js @@ -32,3 +32,20 @@ const isNumberPalindrome = (number) => { return reversed === number; } */ + + const isNumberPalindrome = (num) => { + num = num.toString(); + let rev = []; + for (let i = num.length; i > 0; i--) { + let copy = num.slice(0, i); + rev.push(copy % 10); + } + if (rev.join('') === num) { + return true; + } else { + return false; + } + + } + + console.log(isNumberPalindrome(1331)) From 50e01eccdcfba69b8101232e26bbf8231f4baa25 Mon Sep 17 00:00:00 2001 From: Jamison Date: Tue, 13 Mar 2018 11:22:45 -0700 Subject: [PATCH 2/3] Built the longestCommonPrefix --- longestCommonPrefix.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/longestCommonPrefix.js b/longestCommonPrefix.js index 99bd9f6..a3a22a7 100644 --- a/longestCommonPrefix.js +++ b/longestCommonPrefix.js @@ -39,3 +39,20 @@ const longestCommonPrefix = arrayOfStrings => { is empty) }; */ + +const longestCommonPrefix = arrayofStrings => { + let prefix = arrayofStrings[0]; + let i = prefix.length; + for (let j = 1; j < arrayofStrings.length; j++) { + for (let i = prefix.length; i > 0; i--) { + if (arrayofStrings[j].indexOf(prefix, 0, prefix.length - 1) >= 0) { + return "match on prefix " + prefix + ' with word ' + arrayofStrings[j]; + } else if (arrayofStrings[j].indexOf(prefix, 0, prefix.length - 1) === -1) { + j++; + } + } + } +} + +console.log(longestCommonPrefix(['fl', 'flatulance', 'zoo'])) +console.log(longestCommonPrefix(['zoo', 'flatulance', 'zoo'])) \ No newline at end of file From 82bd848e543b2d195481072bfef6052b15767ee2 Mon Sep 17 00:00:00 2001 From: Jamison Date: Tue, 13 Mar 2018 11:55:12 -0700 Subject: [PATCH 3/3] Updated the logic to conform to the test examples --- longestCommonPrefix.js | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/longestCommonPrefix.js b/longestCommonPrefix.js index a3a22a7..06f927e 100644 --- a/longestCommonPrefix.js +++ b/longestCommonPrefix.js @@ -43,16 +43,18 @@ const longestCommonPrefix = arrayOfStrings => { const longestCommonPrefix = arrayofStrings => { let prefix = arrayofStrings[0]; let i = prefix.length; - for (let j = 1; j < arrayofStrings.length; j++) { - for (let i = prefix.length; i > 0; i--) { - if (arrayofStrings[j].indexOf(prefix, 0, prefix.length - 1) >= 0) { - return "match on prefix " + prefix + ' with word ' + arrayofStrings[j]; - } else if (arrayofStrings[j].indexOf(prefix, 0, prefix.length - 1) === -1) { - j++; + for (let j = arrayofStrings.length - 1; j > 0; j--) { + for (let i = prefix.length; i >= 0; i--) { + if (arrayofStrings[j].indexOf(prefix, 0, prefix.length - 1) === -1) { + prefix = prefix.slice(0, i); + } else if (arrayofStrings[j].indexOf(prefix, 0, prefix.length - 1) >= 0) { + if (arrayofStrings[j - 1].indexOf(prefix, 0, prefix.length - 1) >= 0) { + return "match on prefix " + prefix + ' with word ' + arrayofStrings[j]; + } } } } } -console.log(longestCommonPrefix(['fl', 'flatulance', 'zoo'])) -console.log(longestCommonPrefix(['zoo', 'flatulance', 'zoo'])) \ No newline at end of file +console.log(longestCommonPrefix(['flat', 'flatulance', 'flair'])) +console.log(longestCommonPrefix(['abcdefgh', 'aefghijk', 'abcefgh'])) \ No newline at end of file