From 353884775ec0cbb1783fdebe212ff543aad95579 Mon Sep 17 00:00:00 2001 From: octopusGarden Date: Sun, 30 Oct 2022 12:09:15 -0400 Subject: [PATCH] Updated exercises --- 02_repeatString/README.md | 2 +- 02_repeatString/repeatString.js | 6 +++++- 02_repeatString/repeatString.spec.js | 12 +++++------ 03_reverseString/README.md | 2 +- 03_reverseString/reverseString.spec.js | 6 +++--- 04_removeFromArray/removeFromArray.js | 4 ++-- 04_removeFromArray/removeFromArray.spec.js | 12 +++++------ 08_calculator/calculator.js | 23 ++++++++++++++------- package-lock.json | 24 +++++++++++++++------- package.json | 8 ++++---- 10 files changed, 61 insertions(+), 38 deletions(-) diff --git a/02_repeatString/README.md b/02_repeatString/README.md index 9c8123e..1b23911 100644 --- a/02_repeatString/README.md +++ b/02_repeatString/README.md @@ -10,7 +10,7 @@ This function will take two arguments, `string` and `num`. *Note:* The exercises after this one will not have arguments provided as this one does - you will need to provide them yourself from now on. So read each exercise's README carefully to see what kinds of arguments will be expected. -You will notice in this exercise that there are multiple tests (see in file `repeatString.spec.js`). Only the first test is currently enabled. So after making sure that this first one passes, enable the others one by one by deleting the `.skip` from the `test.skip()` function. +You will notice in this exercise that there are multiple tests (see in file `repeatString.spec.js`). Only the first test is currently enabled. So after making sure that this first one passes, enable the others one by one by deleting the `` from the `test()` function. ## Hints diff --git a/02_repeatString/repeatString.js b/02_repeatString/repeatString.js index ea61aa0..ae89f93 100644 --- a/02_repeatString/repeatString.js +++ b/02_repeatString/repeatString.js @@ -1,5 +1,9 @@ const repeatString = function(string, num) { - return string.repeat(num); + if (num < 0) { + return "ERROR"; + } else { + return string.repeat(num); + } }; // Do not edit below this line diff --git a/02_repeatString/repeatString.spec.js b/02_repeatString/repeatString.spec.js index 912ac20..4a8121b 100644 --- a/02_repeatString/repeatString.spec.js +++ b/02_repeatString/repeatString.spec.js @@ -4,19 +4,19 @@ describe('repeatString', () => { test('repeats the string', () => { expect(repeatString('hey', 3)).toEqual('heyheyhey'); }); - test.skip('repeats the string many times', () => { + test('repeats the string many times', () => { expect(repeatString('hey', 10)).toEqual('heyheyheyheyheyheyheyheyheyhey'); }); - test.skip('repeats the string 1 times', () => { + test('repeats the string 1 times', () => { expect(repeatString('hey', 1)).toEqual('hey'); }); - test.skip('repeats the string 0 times', () => { + test('repeats the string 0 times', () => { expect(repeatString('hey', 0)).toEqual(''); }); - test.skip('returns ERROR with negative numbers', () => { + test('returns ERROR with negative numbers', () => { expect(repeatString('hey', -1)).toEqual('ERROR'); }); - test.skip('repeats the string a random amount of times', function () { + test('repeats the string a random amount of times', function () { /*The number is generated by using Math.random to get a value from between 0 to 1, when this is multiplied by 1000 and rounded down with Math.floor it equals a number between 0 to 999 (this number will change everytime you run @@ -31,7 +31,7 @@ describe('repeatString', () => { was randomly generated. */ expect(repeatString('hey', number).match(/((hey))/g).length).toEqual(number); }); - test.skip('works with blank strings', () => { + test('works with blank strings', () => { expect(repeatString('', 10)).toEqual(''); }); }); diff --git a/03_reverseString/README.md b/03_reverseString/README.md index 05221ae..88c9cfe 100644 --- a/03_reverseString/README.md +++ b/03_reverseString/README.md @@ -6,7 +6,7 @@ Pretty simple, write a function called `reverseString` that returns its input, r reverseString('hello there') // returns 'ereht olleh' ``` -You will notice in this exercise that there are multiple tests, after making the first one pass, enable the others one by one by deleting the `.skip` in front the `test.skip()` function. +You will notice in this exercise that there are multiple tests, after making the first one pass, enable the others one by one by deleting the `` in front the `test()` function. ## Hints Strings in JavaScript cannot be reversed directly so you're going to have to split it into something else first.. do the reversal and then join it back together into a string. diff --git a/03_reverseString/reverseString.spec.js b/03_reverseString/reverseString.spec.js index 8adb887..b51c50e 100644 --- a/03_reverseString/reverseString.spec.js +++ b/03_reverseString/reverseString.spec.js @@ -5,14 +5,14 @@ describe('reverseString', () => { expect(reverseString('hello')).toEqual('olleh'); }); - test.skip('reverses multiple words', () => { + test('reverses multiple words', () => { expect(reverseString('hello there')).toEqual('ereht olleh') }) - test.skip('works with numbers and punctuation', () => { + test('works with numbers and punctuation', () => { expect(reverseString('123! abc!')).toEqual('!cba !321') }) - test.skip('works with blank strings', () => { + test('works with blank strings', () => { expect(reverseString('')).toEqual('') }) }); diff --git a/04_removeFromArray/removeFromArray.js b/04_removeFromArray/removeFromArray.js index da9cee3..1cc5f3d 100644 --- a/04_removeFromArray/removeFromArray.js +++ b/04_removeFromArray/removeFromArray.js @@ -1,6 +1,6 @@ -const removeFromArray = function(list) { +const removeFromArray = function(list, item) { //Remove third element from array - let remove = list.splice(2, 1); + let remove = list.filter(item); return list; }; diff --git a/04_removeFromArray/removeFromArray.spec.js b/04_removeFromArray/removeFromArray.spec.js index 21f34cf..c17239a 100644 --- a/04_removeFromArray/removeFromArray.spec.js +++ b/04_removeFromArray/removeFromArray.spec.js @@ -4,22 +4,22 @@ describe('removeFromArray', () => { test('removes a single value', () => { expect(removeFromArray([1, 2, 3, 4], 3)).toEqual([1, 2, 4]); }); - test.skip('removes multiple values', () => { + test('removes multiple values', () => { expect(removeFromArray([1, 2, 3, 4], 3, 2)).toEqual([1, 4]); }); - test.skip('ignores non present values', () => { + test('ignores non present values', () => { expect(removeFromArray([1, 2, 3, 4], 7, "tacos")).toEqual([1, 2, 3, 4]); }); - test.skip('ignores non present values, but still works', () => { + test('ignores non present values, but still works', () => { expect(removeFromArray([1, 2, 3, 4], 7, 2)).toEqual([1, 3, 4]); }); - test.skip('can remove all values', () => { + test('can remove all values', () => { expect(removeFromArray([1, 2, 3, 4], 1, 2, 3, 4)).toEqual([]); }); - test.skip('works with strings', () => { + test('works with strings', () => { expect(removeFromArray(["hey", 2, 3, "ho"], "hey", 3)).toEqual([2, "ho"]); }); - test.skip('only removes same type', () => { + test('only removes same type', () => { expect(removeFromArray([1, 2, 3], "1", 3)).toEqual([1, 2]); }); }); diff --git a/08_calculator/calculator.js b/08_calculator/calculator.js index c22e8d2..5808d01 100644 --- a/08_calculator/calculator.js +++ b/08_calculator/calculator.js @@ -1,17 +1,26 @@ -const add = function() { - +const add = function(num1, num2) { + let total = num1 + num2; + return total; }; -const subtract = function() { - +const subtract = function(num1, num2) { + let total = num1 - num2; + return total; }; const sum = function() { - + let total = 0; + + for (i = 0; i < numList.length; i++) { + total += parseInt(numList[i]); + } + + return total; }; -const multiply = function() { - +const multiply = function(num1, num2) { + let total = num1 * num2; + return total; }; const power = function() { diff --git a/package-lock.json b/package-lock.json index c754491..14e9dd6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1529,10 +1529,20 @@ } }, "node_modules/caniuse-lite": { - "version": "1.0.30001223", - "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001223.tgz", - "integrity": "sha512-k/RYs6zc/fjbxTjaWZemeSmOjO0JJV+KguOBA3NwPup8uzxM1cMhR2BD9XmO86GuqaqTCO8CgkgH9Rz//vdDiA==", - "dev": true + "version": "1.0.30001425", + "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001425.tgz", + "integrity": "sha512-/pzFv0OmNG6W0ym80P3NtapU0QEiDS3VuYAZMGoLLqiC7f6FJFe1MjpQDREGApeenD9wloeytmVDj+JLXPC6qw==", + "dev": true, + "funding": [ + { + "type": "opencollective", + "url": "https://opencollective.com/browserslist" + }, + { + "type": "tidelift", + "url": "https://tidelift.com/funding/github/npm/caniuse-lite" + } + ] }, "node_modules/capture-exit": { "version": "2.0.0", @@ -8739,9 +8749,9 @@ "dev": true }, "caniuse-lite": { - "version": "1.0.30001223", - "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001223.tgz", - "integrity": "sha512-k/RYs6zc/fjbxTjaWZemeSmOjO0JJV+KguOBA3NwPup8uzxM1cMhR2BD9XmO86GuqaqTCO8CgkgH9Rz//vdDiA==", + "version": "1.0.30001425", + "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001425.tgz", + "integrity": "sha512-/pzFv0OmNG6W0ym80P3NtapU0QEiDS3VuYAZMGoLLqiC7f6FJFe1MjpQDREGApeenD9wloeytmVDj+JLXPC6qw==", "dev": true }, "capture-exit": { diff --git a/package.json b/package.json index 3c203fe..896820c 100644 --- a/package.json +++ b/package.json @@ -14,11 +14,11 @@ }, "homepage": "https://github.com/TheOdinProject/javascript-exercises#readme", "devDependencies": { - "jest": "^26.6.3", - "jest-cli": "^26.6.3", "eslint": "^7.26.0", "eslint-config-airbnb-base": "^14.2.1", - "eslint-plugin-import": "^2.22.1" + "eslint-plugin-import": "^2.22.1", + "jest": "^26.6.3", + "jest-cli": "^26.6.3" }, "scripts": { "test": "jest" @@ -26,7 +26,7 @@ "eslintConfig": { "root": true }, - "jest": { + "jest": { "testPathIgnorePatterns": [ "generator-exercise/" ]