Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions solutions/120.js
Original file line number Diff line number Diff line change
@@ -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,
};
29 changes: 29 additions & 0 deletions test/120.js
Original file line number Diff line number Diff line change
@@ -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);
});
});