From ed5387f268f09195050192f7f46f8681303c872e Mon Sep 17 00:00:00 2001 From: Roberra0 Date: Fri, 7 Jul 2023 18:03:28 -0700 Subject: [PATCH] Completed palindrome --- 08_calculator/calculator.js | 21 ++----------- 09_palindromes/palindromes.js | 49 +++++++++++++++++++++++++++++- 09_palindromes/palindromes.spec.js | 14 ++++----- 10_fibonacci/fibonacci.js | 14 +++++++-- 10_fibonacci/fibonacci.spec.js | 6 ++-- 5 files changed, 71 insertions(+), 33 deletions(-) diff --git a/08_calculator/calculator.js b/08_calculator/calculator.js index 4f32260..db729ac 100644 --- a/08_calculator/calculator.js +++ b/08_calculator/calculator.js @@ -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; diff --git a/09_palindromes/palindromes.js b/09_palindromes/palindromes.js index 8d21018..5157e93 100644 --- a/09_palindromes/palindromes.js +++ b/09_palindromes/palindromes.js @@ -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; diff --git a/09_palindromes/palindromes.spec.js b/09_palindromes/palindromes.spec.js index 90d53e4..8ce5aac 100644 --- a/09_palindromes/palindromes.spec.js +++ b/09_palindromes/palindromes.spec.js @@ -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); }); }); diff --git a/10_fibonacci/fibonacci.js b/10_fibonacci/fibonacci.js index bb2c8cc..f03a1e8 100644 --- a/10_fibonacci/fibonacci.js +++ b/10_fibonacci/fibonacci.js @@ -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; diff --git a/10_fibonacci/fibonacci.spec.js b/10_fibonacci/fibonacci.spec.js index 7f62213..0f9f66a 100644 --- a/10_fibonacci/fibonacci.spec.js +++ b/10_fibonacci/fibonacci.spec.js @@ -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', () => {