Compare commits

...

37 Commits

Author SHA1 Message Date
Hifilo 9eb942909e
09 Palindromes: Replace Regex Solution (#319)
* Replaces the original regex method, with more familiar methods
* Add descriptive comments for clarity
2023-03-22 20:42:17 -05:00
c-auri 77c5d64b37
Alter caesar solution with clean code practices (#302) 2023-03-18 17:32:20 -05:00
Eric Olkowski c4301bbd9c
Merge pull request #253 from yossirise/patch-1
JS exercises solutions - removeFromArray: Make `array` an independent parameter
2023-01-21 09:43:08 -05:00
Eric Olkowski 04ced679a3
Merge pull request #304 from jamhog/new-sumAll-test-solution
05_sumAll: Add new test - update solutions
2023-01-21 08:41:16 -05:00
Yossi Rise adf2bf93bc
Update removeFromArray/removeFromArray.js
Co-authored-by: Eric Olkowski <70952936+thatblindgeye@users.noreply.github.com>
2022-11-23 15:53:38 +02:00
James Hogan 0f57a2afcd Add sumAll test 2022-11-20 12:37:51 +11:00
Eric Olkowski 44a95b8e89
Merge pull request #300 from Asartea/asartea-patch-1-solutions
tempConversion: Update function names to account for changes in the base branch
2022-11-19 10:39:59 -05:00
Asartea 171ac956cf fix spacing 2022-11-19 15:09:51 +01:00
Eric Olkowski 2518281b10
Merge pull request #243 from jamhog/add-declaration
palindromes: Add mising variable declaration
2022-11-19 07:40:26 -05:00
Asartea e94742d58c Missed another README.md reference 2022-11-12 21:11:40 +01:00
Asartea 6bafdb5094 Accidently got rid of the function parameters 2022-11-12 21:03:08 +01:00
Asartea c037ad5bf5 Update tempConversion function naming to keep in sync with base branch 2022-11-12 20:56:14 +01:00
Yossi Rise b52b2ace26
Make `array` an independent argument
This spares us from having to extract `array` from `args` and is more idiomatic (I think).
2022-05-28 23:19:49 +03:00
James Hogan 10cb98c038 Add missing variable declaration 2022-03-31 09:06:19 +11:00
Tatiana db998d7279
Merge pull request #219 from tunsmart/solutions
Improve the readability of the RegExp
2022-01-30 10:28:53 -08:00
Babatunde Alli 4b13066369 Improve the readability of the RegExp 2022-01-28 18:23:16 -05:00
Michael Frank 7df108c5de
Merge pull request #216 from icorvus/solutions 2022-01-27 10:15:39 +13:00
icorvus de40b279df fixed semicolon 2022-01-23 21:17:39 +01:00
icorvus c09df52d6a fixed semicolon 2022-01-23 20:50:36 +01:00
Tatiana 8150be944b
Merge pull request #189 from Yasikhorram/fix-comment-contribution
correct the term spread operator to rest operator + reduce the space between . and If
2021-10-27 13:55:48 -07:00
Yasikhorram 5b5687862a correct the term spread operator to rest operator + reduce the space between . and If 2021-10-27 14:46:13 -05:00
Marvin Gay 56ebfc0f7d Merge branch 'main' into 'solutions' 2021-07-25 13:25:16 -04:00
Kevin Mulhern 4fca2e249c
Merge pull request #150 from TheOdinProject/jest-solutions
Implement Jest testing platform for all exercises (Solutions branch)
2021-05-20 00:03:29 +01:00
Kevin Mulhern fda681965e
Merge branch 'chore/switch-to-jest' into jest-solutions 2021-05-18 00:45:34 +01:00
Kevin Mulhern a1c56276ea Merge branch 'chore/switch-to-jest' into jest-solutions 2021-05-18 00:42:57 +01:00
Michael Frank 085fd89ec8 added new lines at end of file 2021-05-12 21:38:10 +12:00
Michael Frank 06e90f66c8 pulled eslint files from jester-tester and updated exercise numbers 2021-05-12 21:37:19 +12:00
Michael Frank 8546794b47 Remove lint script and add .DS_Store to .gitignore 2021-05-11 14:34:12 +12:00
Michael Frank ac4d2a94e5 Grammar cleanups, trailing semicolon for function expressions 2021-05-10 20:08:31 +12:00
Michael Frank 276b60a3c3 .eslitrc.json: Added support for airbnb-base linting 2021-05-10 19:03:38 +12:00
Michael Frank 435c1d3779 package(-lock).json: Update to include version and repsitory data based on old solution branch 2021-05-10 16:22:10 +12:00
Michael Frank 9916e343a3 repeatString/README.md: Rewrite hints to refer to Jest "skipped" tests. 2021-05-10 16:20:47 +12:00
Michael Frank cced031981 helloWorld/README.md: Remove mention of Jasmine in favour of Jest 2021-05-10 16:19:53 +12:00
Michael Frank 2d6c505096 calculator/README.md: Removed references to Jasmine.
calculator.spec.js: Set all tests except the first to test.skip
2021-05-10 14:57:23 +12:00
Michael Frank a5a0284ac1 update var to const in function declarations 2021-05-10 00:06:18 +12:00
Michael Frank ce0b9fade1 Revert package-lock.json back to node lts version 2021-05-08 23:54:40 +12:00
Michael Frank 749a48adee Tweak matchers in removeFromArray, pull pigLatin solution from old branch 2021-04-30 22:12:52 +12:00
20 changed files with 7378 additions and 56 deletions

View File

@ -1,5 +1,29 @@
const caesar = function() { const caesar = function(string, shiftValue) {
return string
.split("")
.map(char => shift(char, shiftValue))
.join("");
};
const isLetter = code => {
return (code >= 65 && code <= 90) || (code >= 97 && code <= 122);
}
// This function implements a version of the modulo operator
// that returns the smallest positive remainder even for negative inputs.
// See this link for details:
// https://stackoverflow.com/questions/4467539/javascript-modulo-gives-a-negative-result-for-negative-numbers
const mod = (n, m) => (n % m + m) % m;
const shift = (char, shiftValue) => {
const code = char.charCodeAt();
if (isLetter(code)) {
const base = code < 97 ? 65 : 97;
const shiftedCode = mod(code + shiftValue - base, 26) + base;
return String.fromCharCode(shiftedCode);
}
return char;
}; };
module.exports = caesar; module.exports = caesar;

View File

@ -1,25 +1,41 @@
const add = function() { const add = function(a, b) {
return a + b;
}; };
const subtract = function() { const subtract = function(a, b) {
return a - b;
}; };
const sum = function() { const sum = function(array) {
return array.reduce((total, current) => total + current, 0);
}; };
const multiply = function() { const multiply = function(array) {
return array.length
? array.reduce((accumulator, nextItem) => accumulator * nextItem)
: 0;
}; };
const power = function() { const power = function(a, b) {
return Math.pow(a, b);
}; };
const factorial = function() { const factorial = function(n) {
if (n === 0) return 1;
let product = 1;
for (let i = n; i > 0; i--) {
product *= i;
}
return product;
};
// This is another implementation of Factorial that uses recursion
// THANKS to @ThirtyThreeB!
const recursiveFactorial = function(n) {
if (n === 0) {
return 1;
}
return n * recursiveFactorial (n-1);
}; };
module.exports = { module.exports = {

View File

@ -1,5 +1,14 @@
const fibonacci = function() { const fibonacci = function(count) {
if (count < 0) return "OOPS";
if (count === 0) return 0;
let a = 0;
let b = 1;
for (let i = 1; i < count; i++) {
const temp = b;
b = a + b;
a = temp;
}
return b;
}; };
module.exports = fibonacci; module.exports = fibonacci;

View File

@ -1,5 +1,16 @@
const findTheOldest = function() { const findTheOldest = function(array) {
return array.reduce((oldest, currentPerson) => {
const oldestAge = getAge(oldest.yearOfBirth, oldest.yearOfDeath);
const currentAge = getAge(currentPerson.yearOfBirth, currentPerson.yearOfDeath);
return oldestAge < currentAge ? currentPerson : oldest;
})
};
const getAge = function(birth, death) {
if (!death) {
death = new Date().getFullYear();
}
return death - birth;
}; };
module.exports = findTheOldest; module.exports = findTheOldest;

View File

@ -1,5 +1,5 @@
const getTheTitles = function() { const getTheTitles = function(array) {
return array.map(book => book.title);
}; };
module.exports = getTheTitles; module.exports = getTheTitles;

View File

@ -1,5 +1,5 @@
const helloWorld = function() { const helloWorld = function() {
return '' return 'Hello, World!';
}; };
module.exports = helloWorld; module.exports = helloWorld;

View File

@ -1,5 +1,5 @@
const leapYears = function() { const leapYears = function(year) {
return year % 4 === 0 && ( year % 100 !== 0 || year % 400 === 0);
}; };
module.exports = leapYears; module.exports = leapYears;

7182
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -14,11 +14,11 @@
}, },
"homepage": "https://github.com/TheOdinProject/javascript-exercises#readme", "homepage": "https://github.com/TheOdinProject/javascript-exercises#readme",
"devDependencies": { "devDependencies": {
"jest": "^26.6.3",
"jest-cli": "^26.6.3",
"eslint": "^7.26.0", "eslint": "^7.26.0",
"eslint-config-airbnb-base": "^14.2.1", "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": { "scripts": {
"test": "jest" "test": "jest"

View File

@ -1,5 +1,14 @@
const palindromes = function () { // Non regex
const palindromes = function (string) {
let alphabet = 'abcdefghijklmnopqrstuvwxyz'; //Create a variable that holds all the letters of the alphabet
const cleanedString = string // Convert to lowercase, split, & filter only letters, rejoin as new const
.toLowerCase()
.split('')
.filter((letter) => alphabet.includes(letter))
.join('');
const reversedString = cleanedString.split('').reverse().join(''); //Create a new const that holds reversed string
return cleanedString === reversedString; //Compare cleanedString & reversedString which returns true/false
}; };
// Do not edit below this line
module.exports = palindromes; module.exports = palindromes;

View File

@ -1,5 +1,21 @@
function pigLatin(string) { const pigLatin = function(string) {
return string
.split(" ")
.map(word => {
const index = firstVowelIndex(word);
const beginning = word.slice(0, index);
const ending = word.slice(index);
return `${ending}${beginning}ay`;
})
.join(" ");
};
const firstVowelIndex = function(string) {
const vowels = string.match(/[aeiou]/g);
if (vowels[0] == "u" && string[string.indexOf(vowels[0]) - 1] == "q") {
return string.indexOf(vowels[1]);
}
return string.indexOf(vowels[0]);
}; };
module.exports = pigLatin; module.exports = pigLatin;

View File

@ -1,5 +1,28 @@
const removeFromArray = function() { // we have 2 solutions here, an easier one and a more advanced one.
// The easiest way to get an array of the rest of the arguments that are passed to a function
// is using the rest operator. If this is unfamiliar to you look it up!
const removeFromArray = function (array, ...args) {
// create a new empty array
const newArray = [];
// use forEach to go through the array
array.forEach((item) => {
// push every element into the new array
// UNLESS it is included in the function arguments
// so we create a new array with every item, except those that should be removed
if (!args.includes(item)) {
newArray.push(item);
}
});
// and return that array
return newArray;
}; };
// A simpler, but more advanced way to do it is to use the 'filter' function,
// which basically does what we did with the forEach above.
// var removeFromArray = function(array, ...args) {
// return array.filter(val => !args.includes(val))
// }
//
module.exports = removeFromArray; module.exports = removeFromArray;

View File

@ -1,5 +1,10 @@
const repeatString = function() { const repeatString = function(word, times) {
if (times < 0) return 'ERROR';
let string = '';
for (let i = 0; i < times; i++) {
string += word;
}
return string;
}; };
module.exports = repeatString; module.exports = repeatString;

View File

@ -1,5 +1,5 @@
const reverseString = function() { const reverseString = function(string) {
return string.split('').reverse().join('');
}; };
module.exports = reverseString; module.exports = reverseString;

View File

@ -1,5 +1,19 @@
const snakeCase = function() { const snakeCase = function(string) {
// wtf case
string = string.replace(/\.\./g, " ");
// this splits up camelcase IF there are no spaces in the word
if (string.indexOf(" ") < 0) {
string = string.replace(/([A-Z])/g, " $1");
}
return string
.trim()
.toLowerCase()
.replace(/[,\?\.]/g, "")
.replace(/\-/g, " ")
.split(" ")
.join("_");
}; };
module.exports = snakeCase; module.exports = snakeCase;

View File

@ -1,5 +1,16 @@
const sumAll = function() { const sumAll = function(min, max) {
if (!Number.isInteger(min) || !Number.isInteger(max)) return "ERROR";
if (min < 0 || max < 0) return "ERROR";
if (min > max) {
const temp = min;
min = max;
max = temp;
}
let sum = 0;
for (let i = min; i < max + 1; i++) {
sum += i;
}
return sum;
}; };
module.exports = sumAll; module.exports = sumAll;

View File

@ -13,6 +13,9 @@ describe('sumAll', () => {
test.skip('returns ERROR with negative numbers', () => { test.skip('returns ERROR with negative numbers', () => {
expect(sumAll(-10, 4)).toEqual('ERROR'); expect(sumAll(-10, 4)).toEqual('ERROR');
}); });
test.skip('returns ERROR with non-integer parameters', () => {
expect(sumAll(2.5, 4)).toEqual('ERROR');
});
test.skip('returns ERROR with non-number parameters', () => { test.skip('returns ERROR with non-number parameters', () => {
expect(sumAll(10, "90")).toEqual('ERROR'); expect(sumAll(10, "90")).toEqual('ERROR');
}); });

View File

@ -2,12 +2,12 @@
Write two functions that convert temperatures from Fahrenheit to Celsius, and vice versa: Write two functions that convert temperatures from Fahrenheit to Celsius, and vice versa:
``` ```
ftoc(32) // fahrenheit to celsius, should return 0 convertToCelsius(32) // fahrenheit to celsius, should return 0
ctof(0) // celsius to fahrenheit, should return 32 convertToFahrenheit(0) // celsius to fahrenheit, should return 32
``` ```
Because we are human, we want the result temperature to be rounded to one decimal place: i.e., `ftoc(100)` should return `37.8` and not `37.77777777777778`. Because we are human, we want the result temperature to be rounded to one decimal place: i.e., `convertToCelsius(100)` should return `37.8` and not `37.77777777777778`.
This exercise asks you to create more than one function so the `module.exports` section of the spec file looks a little different this time. Nothing to worry about, we're just packaging both functions into a single object to be exported. This exercise asks you to create more than one function so the `module.exports` section of the spec file looks a little different this time. Nothing to worry about, we're just packaging both functions into a single object to be exported.

View File

@ -1,12 +1,13 @@
const ftoc = function() { const convertToCelsius = function(fahrenheit) {
return Math.round((fahrenheit - 32) * (5/9) * 10) / 10;
}; };
const ctof = function() { const convertToFahrenheit = function(celsius) {
return Math.round(((celsius * 9/5) + 32) * 10) / 10;
}; };
module.exports = { module.exports = {
ftoc, convertToCelsius,
ctof convertToFahrenheit
}; };

View File

@ -1,25 +1,25 @@
const {ftoc, ctof} = require('./tempConversion') const {convertToCelsius, convertToFahrenheit} = require('./tempConversion')
describe('ftoc', () => { describe('convertToCelsius', () => {
test('works', () => { test('works', () => {
expect(ftoc(32)).toEqual(0); expect(convertToCelsius(32)).toEqual(0);
}); });
test.skip('rounds to 1 decimal', () => { test.skip('rounds to 1 decimal', () => {
expect(ftoc(100)).toEqual(37.8); expect(convertToCelsius(100)).toEqual(37.8);
}); });
test.skip('works with negatives', () => { test.skip('works with negatives', () => {
expect(ftoc(-100)).toEqual(-73.3); expect(convertToCelsius(-100)).toEqual(-73.3);
}); });
}); });
describe('ctof', () => { describe('convertToFahrenheit', () => {
test.skip('works', () => { test.skip('works', () => {
expect(ctof(0)).toEqual(32); expect(convertToFahrenheit(0)).toEqual(32);
}); });
test.skip('rounds to 1 decimal', () => { test.skip('rounds to 1 decimal', () => {
expect(ctof(73.2)).toEqual(163.8); expect(convertToFahrenheit(73.2)).toEqual(163.8);
}); });
test.skip('works with negatives', () => { test.skip('works with negatives', () => {
expect(ctof(-10)).toEqual(14); expect(convertToFahrenheit(-10)).toEqual(14);
}); });
}); });