Completed palindrome
This commit is contained in:
parent
454d897d0e
commit
ed5387f268
|
@ -6,26 +6,9 @@ const subtract = function(a,b) {
|
|||
return a-b;
|
||||
};
|
||||
|
||||
const sum = (nums)=>{
|
||||
let total=0;
|
||||
for(let i in nums){
|
||||
total += nums[i];
|
||||
}
|
||||
return total;
|
||||
};
|
||||
const sum = (nums)=> nums.reduce((total, num)=> total+= num);
|
||||
|
||||
const multiply = (...args) => {
|
||||
let total=1;
|
||||
let i=0;
|
||||
for(let i in args){
|
||||
// console.log("i: "+i+" args[i]: "+args[i]);
|
||||
total *= args[i];
|
||||
i++;
|
||||
}
|
||||
console.log(total);
|
||||
return total;
|
||||
|
||||
};
|
||||
const multiply = (...args) => args.reduce((total,arg)=> total *= arg);
|
||||
|
||||
const power = (a,b)=> {
|
||||
let total=1;
|
||||
|
|
|
@ -1,6 +1,53 @@
|
|||
const palindromes = function () {
|
||||
// Problem: Write a function that determines whether or not a given string is a palindrome.
|
||||
// Framework
|
||||
// 1. Understand the problem
|
||||
// ○ Given: string
|
||||
// ○ Output: True/False
|
||||
// ○ Examples:
|
||||
// § Racecar
|
||||
// § A car, a man, a maraca.
|
||||
// ○ Assumptions:
|
||||
// § Ignore punctuations
|
||||
// § Ignore capitalization
|
||||
// 2. Strategize: Visually
|
||||
//
|
||||
// 3. Strategize: Written
|
||||
// ○ Create array A for given string
|
||||
// ○ Sanitize array
|
||||
// § Remove punctuation, spacing, capitalization
|
||||
// ○ Create a copy of array A reversed
|
||||
// ○ Store it in Array B
|
||||
// ○ Compare Array A & B
|
||||
|
||||
|
||||
|
||||
const palindromes = function (input){
|
||||
let startString =input.toLowerCase().replace(/[ ,.!?;:'"()\-_]/g,'').split("");
|
||||
const endString = startString.map(x=>x); //If did = startString, endString REFERENCES start string, we dont want that so use map
|
||||
console.log(startString);
|
||||
endString.reverse();
|
||||
console.log(startString);
|
||||
console.log(endString);
|
||||
return startString.toString() === endString.toString();
|
||||
};
|
||||
|
||||
palindromes('ZZZZ car, a man, a maracaz.')
|
||||
// palindrome("racecar");
|
||||
// palindrome("Racecar");
|
||||
// palindrome("A car, a man, a maraca.");
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// Do not edit below this line
|
||||
module.exports = palindromes;
|
||||
|
|
|
@ -4,25 +4,25 @@ describe('palindromes', () => {
|
|||
test('works with single words', () => {
|
||||
expect(palindromes('racecar')).toBe(true);
|
||||
});
|
||||
test.skip('works with punctuation ', () => {
|
||||
test('works with punctuation ', () => {
|
||||
expect(palindromes('racecar!')).toBe(true);
|
||||
});
|
||||
test.skip('works with upper-case letters ', () => {
|
||||
test('works with upper-case letters ', () => {
|
||||
expect(palindromes('Racecar!')).toBe(true);
|
||||
});
|
||||
test.skip('works with multiple words', () => {
|
||||
test('works with multiple words', () => {
|
||||
expect(palindromes('A car, a man, a maraca.')).toBe(true);
|
||||
});
|
||||
test.skip('works with multiple words', () => {
|
||||
test('works with multiple words', () => {
|
||||
expect(palindromes('Animal loots foliated detail of stool lamina.')).toBe(true);
|
||||
});
|
||||
test.skip('doesn\'t just always return true', () => {
|
||||
test('doesn\'t just always return true', () => {
|
||||
expect(palindromes('ZZZZ car, a man, a maracaz.')).toBe(false);
|
||||
});
|
||||
test.skip('works with numbers in a string', () => {
|
||||
test('works with numbers in a string', () => {
|
||||
expect(palindromes('rac3e3car')).toBe(true);
|
||||
});
|
||||
test.skip('works with unevenly spaced numbers in a string', () => {
|
||||
test('works with unevenly spaced numbers in a string', () => {
|
||||
expect(palindromes('r3ace3car')).toBe(false);
|
||||
});
|
||||
});
|
||||
|
|
|
@ -1,6 +1,14 @@
|
|||
const fibonacci = function() {
|
||||
|
||||
};
|
||||
const fibonacci = function(n) {
|
||||
//1, 1, 2, 3, 5, 8
|
||||
//n1 n2 n3 n4 n5
|
||||
//n4 = n3+n2
|
||||
//n3 = n2+n1
|
||||
//n2 = n1
|
||||
if(n<=1){
|
||||
return n;
|
||||
}
|
||||
return fibonacci(n-1)+fibonacci(n-2);
|
||||
};
|
||||
|
||||
// Do not edit below this line
|
||||
module.exports = fibonacci;
|
||||
|
|
|
@ -4,13 +4,13 @@ describe('fibonacci', () => {
|
|||
test('4th fibonacci number is 3', () => {
|
||||
expect(fibonacci(4)).toBe(3);
|
||||
});
|
||||
test.skip('6th fibonacci number is 8', () => {
|
||||
test('6th fibonacci number is 8', () => {
|
||||
expect(fibonacci(6)).toBe(8);
|
||||
});
|
||||
test.skip('10th fibonacci number is 55', () => {
|
||||
test('10th fibonacci number is 55', () => {
|
||||
expect(fibonacci(10)).toBe(55);
|
||||
});
|
||||
test.skip('15th fibonacci number is 610', () => {
|
||||
test('15th fibonacci number is 610', () => {
|
||||
expect(fibonacci(15)).toBe(610);
|
||||
});
|
||||
test.skip('25th fibonacci number is 75025', () => {
|
||||
|
|
Loading…
Reference in New Issue