From 9fddbcfca7d9ded40aff938544fce1a2a40399ef Mon Sep 17 00:00:00 2001 From: Francesca Calla Bruc Date: Sun, 22 Jan 2023 23:33:22 -0800 Subject: [PATCH] fixed for erros --- src/algorithms/math/is_prime.js | 16 ++++++++-------- src/algorithms/math/sum_all.js | 2 +- src/algorithms/search/naive_search.js | 21 ++++++++++----------- test/algorithms/math/testPrime.js | 2 +- test/algorithms/math/testSumAll.js | 8 ++++---- test/algorithms/search/testNaiveSearch.js | 6 +++--- 6 files changed, 27 insertions(+), 28 deletions(-) diff --git a/src/algorithms/math/is_prime.js b/src/algorithms/math/is_prime.js index da62d747..6ec45f1b 100644 --- a/src/algorithms/math/is_prime.js +++ b/src/algorithms/math/is_prime.js @@ -5,13 +5,13 @@ * * References: https://javascript.plainenglish.io/11-mathematical-algorithms-in-modern-javascript-bce71318e2da */ - const isprime = num => { +const isprime = num => { const limit = Math.floor(Math.sqrt(num)); - for (let i = 2; i <= limit; i++){ - if (n % i === 0) return false; + for (let i = 2; i <= limit; i++) { + if (num % i === 0) return false; } - return n >= 2; - }; - - module.exports = isprime; - \ No newline at end of file + return num >= 2; +}; + + +module.exports = isprime; diff --git a/src/algorithms/math/sum_all.js b/src/algorithms/math/sum_all.js index 43d94ba9..90fdd430 100644 --- a/src/algorithms/math/sum_all.js +++ b/src/algorithms/math/sum_all.js @@ -4,7 +4,7 @@ * @return {Number} sum sum of the range of numberse */ const sumall = (arr) => { - var sum =0; + var sum = 0; var min = arr[0]; var max = arr[1]; for (var i = min; i <= max; i++) { diff --git a/src/algorithms/search/naive_search.js b/src/algorithms/search/naive_search.js index d8790409..9b7a68b2 100644 --- a/src/algorithms/search/naive_search.js +++ b/src/algorithms/search/naive_search.js @@ -5,17 +5,16 @@ * @return {Number} count Frequency the pattern is in st */ -const naivesearch = (st,pattern) => - { - let count =0; - for(let i = 0; i < st.length; i++) { - for(let j = 0; j < pattern.length; j++) { - if(st[i + j] !== pattern[j]) break; - if(j === pattern.length - 1) count++; - } +const naivesearch = (st, pattern) => { + let count = 0; + for (let i = 0; i < st.length; i++) { + for (let j = 0; j < pattern.length; j++) { + if (st[i + j] !== pattern[j]) break; + if (j === pattern.length - 1) count = count + 1; } - - return count; } - module.exports = naivesearch; \ No newline at end of file + return count; +} + +module.exports = naivesearch; \ No newline at end of file diff --git a/test/algorithms/math/testPrime.js b/test/algorithms/math/testPrime.js index a2f2717a..8ab7c2fe 100644 --- a/test/algorithms/math/testPrime.js +++ b/test/algorithms/math/testPrime.js @@ -15,4 +15,4 @@ describe('isPrime', () => { assert.equal(isprime(36), false); assert.equal(isprime(100), false); }); -}); \ No newline at end of file +}); diff --git a/test/algorithms/math/testSumAll.js b/test/algorithms/math/testSumAll.js index ee049eca..46c666cc 100644 --- a/test/algorithms/math/testSumAll.js +++ b/test/algorithms/math/testSumAll.js @@ -5,10 +5,10 @@ const assert = require('assert'); describe('sumALL', () => { it('should return the sum of range', () => { - assert.equal(sumall(2,5), 14); - assert.equal(sumall(1,3), 6); - assert.equal(sumall(3,6), 18); + assert.equal(sumall(2, 5), 14); + assert.equal(sumall(1, 3), 6); + assert.equal(sumall(3, 6), 18); assert.equal(sumall(37), true); }); -}); \ No newline at end of file +}); diff --git a/test/algorithms/search/testNaiveSearch.js b/test/algorithms/search/testNaiveSearch.js index 801d487f..0950de2d 100644 --- a/test/algorithms/search/testNaiveSearch.js +++ b/test/algorithms/search/testNaiveSearch.js @@ -4,19 +4,19 @@ const assert = require('assert'); describe('Naive Search', () => { it('should return frequency of the pattern', () => { - const freq = naivesearch("akgjfjhuyutomatokajkhgsvkjrtomato", "tomato"); + const freq = naivesearch('akgjfjhuyutomatokajkhgsvkjrtomato', 'tomato'); assert.equal(freq, 1); }); it('should return frequency of the pattern', () => { - const freq = naivesearch("treeseebeetea", "ee"); + const freq = naivesearch('treeseebeetea', 'ee'); assert.equal(freq, 3); }); it('should return frequency of the pattern', () => { - const freq = naivesearch("applebottomjeans", "boots"); + const freq = naivesearch('applebottomjeans', 'boots'); assert.equal(freq, 0); });