diff --git a/solutions/120.js b/solutions/120.js new file mode 100644 index 0000000..7e4727a --- /dev/null +++ b/solutions/120.js @@ -0,0 +1,25 @@ +// Natalie Lockhart +// Determine if array is a palindrome + +const solution = (arr) => { + + function palindrome(arr, index){ + if (index == 0) { + return true; + } + if (arr[index-1] != arr[arr.length - index]){ + return false; + } + return palindrome(arr, index-1); + } + + if (arr.length == 0 || arr.length == 1) { + return true; + } + var index = Math.floor(arr.length/2); + return palindrome(arr, index); +}; + +module.exports = { + solution, +}; \ No newline at end of file diff --git a/test/120.js b/test/120.js new file mode 100644 index 0000000..23cef91 --- /dev/null +++ b/test/120.js @@ -0,0 +1,29 @@ +const expect = require('chai').expect; +let solution = require('../solutions/120.js').solution; + +describe('palindrome', () => { + it('should return true for [1,2,3,4,4,3,2,1]', () => { + const result = solution([1,2,3,4,4,3,2,1]); + expect(result).to.equal(true); + }); + it('should return true for [10,11,12,11,10]', () => { + const result = solution([10,11,12,11,10]); + expect(result).to.equal(true); + }); + it('should return true for [1]', () => { + const result = solution([1]); + expect(result).to.equal(true); + }); + it('should return true for []', () => { + const result = solution([]); + expect(result).to.equal(true); + }); + it('should return false for [10,11]', () => { + const result = solution([10,11]); + expect(result).to.equal(false); + }); + it('should return false for [10,11,12])', () => { + const result = solution([10,11,12]); + expect(result).to.equal(false); + }); +}); \ No newline at end of file