Compare commits

...

19 Commits

Author SHA1 Message Date
Michael Frank d1c9c98366
Merge pull request #218 from trekitch/jest-solutions 2022-01-27 10:07:16 +13:00
Trevon Kitchen d091aef25d
Update calculator/calculator.js
accepted

Co-authored-by: Michael Frank <mfra109@gmail.com>
2022-01-26 16:04:46 -05:00
trekitch db4255dc92 changed right name this time 2022-01-26 15:04:37 -05:00
trekitch 10199f35a9 change to alternate solutions name 2022-01-26 15:03:53 -05:00
trekitch 894ae2d4ce added alternate solution to multiply using Exponentiation operator 2022-01-26 14:15:16 -05: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
17 changed files with 7363 additions and 42 deletions

View File

@ -1,5 +1,26 @@
const caesar = function() {
const caesar = function(string, shift) {
return string
.split("")
.map(char => shiftChar(char, shift))
.join("");
};
const codeSet = code => (code < 97 ? 65 : 97);
// this function is just a fancy way of doing % so that it works with negative numbers
// 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 shiftChar = (char, shift) => {
const code = char.charCodeAt();
if ((code >= 65 && code <= 90) || (code >= 97 && code <= 122)) {
return String.fromCharCode(
mod(code + shift - codeSet(code), 26) + codeSet(code)
);
}
return char;
};
module.exports = caesar;

View File

@ -1,25 +1,46 @@
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() {
//alternate solution using the Exponentiation operator
const altPower = function(a, b) {
return a ** b;
};
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 = {

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;

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;

View File

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

View File

@ -1,5 +1,5 @@
const helloWorld = function() {
return ''
return 'Hello, World!'
};
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;

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",
"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"

View File

@ -1,5 +1,11 @@
const palindromes = function () {};
const palindromes = function(string) {
processedString = string.toLowerCase().replace(/[^A-Za-z]/g, "");
return (
processedString
.split("")
.reverse()
.join("") == processedString
);
};
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;

View File

@ -1,5 +1,31 @@
const removeFromArray = function() {
// we have 2 solutions here, an easier one and a more advanced one.
// The easiest way to get an array of all of the arguments that are passed to a function
// is using the spread operator. If this is unfamiliar to you look it up!
const removeFromArray = function (...args) {
// the very first item in our list of arguments is the array, we pull it out with args[0]
const array = args[0];
// 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(...args) {
// const array = args[0]
// return array.filter(val => !args.includes(val))
// }
//
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;

View File

@ -1,5 +1,5 @@
const reverseString = function() {
const reverseString = function(string) {
return string.split('').reverse().join('')
};
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;

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;

View File

@ -1,11 +1,12 @@
const ftoc = function() {
const ftoc = function(f) {
return Math.round((f - 32) * (5/9) * 10) / 10
};
const ctof = function() {
const ctof = function(c) {
return Math.round(((c * 9/5) + 32) * 10) / 10
};
module.exports = {
ftoc,
ctof