Update files
This commit is contained in:
parent
fb1a2db8d7
commit
4a112362c8
|
@ -1,5 +1,5 @@
|
||||||
const helloWorld = function () {
|
const helloWorld = function () {
|
||||||
return 'Hello, World!';
|
return "Hello, World!";
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = helloWorld;
|
module.exports = helloWorld;
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
const helloWorld = require('./helloWorld-solution');
|
const helloWorld = require("./helloWorld");
|
||||||
|
|
||||||
describe('Hello World', function () {
|
describe("Hello World", function () {
|
||||||
test('says "Hello, World!"', function () {
|
test('says "Hello, World!"', function () {
|
||||||
expect(helloWorld()).toEqual('Hello, World!');
|
expect(helloWorld()).toEqual("Hello, World!");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
const repeatString = function (word, times) {
|
const repeatString = function (word, times) {
|
||||||
if (times < 0) return 'ERROR';
|
if (times < 0) return "ERROR";
|
||||||
let string = '';
|
let string = "";
|
||||||
for (let i = 0; i < times; i++) {
|
for (let i = 0; i < times; i++) {
|
||||||
string += word;
|
string += word;
|
||||||
}
|
}
|
||||||
return string;
|
return string;
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = repeatString;
|
module.exports = repeatString;
|
||||||
|
|
|
@ -1,41 +1,39 @@
|
||||||
const repeatString = require('./repeatString-solution');
|
const repeatString = require("./repeatString");
|
||||||
|
|
||||||
describe('repeatString', () => {
|
describe("repeatString", () => {
|
||||||
test('repeats the string', () => {
|
test("repeats the string", () => {
|
||||||
expect(repeatString('hey', 3)).toEqual('heyheyhey');
|
expect(repeatString("hey", 3)).toEqual("heyheyhey");
|
||||||
});
|
});
|
||||||
test.skip('repeats the string many times', () => {
|
test.skip("repeats the string many times", () => {
|
||||||
expect(repeatString('hey', 10)).toEqual(
|
expect(repeatString("hey", 10)).toEqual("heyheyheyheyheyheyheyheyheyhey");
|
||||||
'heyheyheyheyheyheyheyheyheyhey'
|
});
|
||||||
);
|
test.skip("repeats the string 1 times", () => {
|
||||||
});
|
expect(repeatString("hey", 1)).toEqual("hey");
|
||||||
test.skip('repeats the string 1 times', () => {
|
});
|
||||||
expect(repeatString('hey', 1)).toEqual('hey');
|
test.skip("repeats the string 0 times", () => {
|
||||||
});
|
expect(repeatString("hey", 0)).toEqual("");
|
||||||
test.skip('repeats the string 0 times', () => {
|
});
|
||||||
expect(repeatString('hey', 0)).toEqual('');
|
test.skip("returns ERROR with negative numbers", () => {
|
||||||
});
|
expect(repeatString("hey", -1)).toEqual("ERROR");
|
||||||
test.skip('returns ERROR with negative numbers', () => {
|
});
|
||||||
expect(repeatString('hey', -1)).toEqual('ERROR');
|
test.skip("repeats the string a random amount of times", function () {
|
||||||
});
|
/*The number is generated by using Math.random to get a value from between
|
||||||
test.skip('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
|
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
|
equals a number between 0 to 999 (this number will change everytime you run
|
||||||
the test).*/
|
the test).*/
|
||||||
|
|
||||||
// DO NOT use Math.floor(Math.random() * 1000) in your code,
|
// DO NOT use Math.floor(Math.random() * 1000) in your code,
|
||||||
// this test generates a random number, then passes it into your code with a function parameter.
|
// this test generates a random number, then passes it into your code with a function parameter.
|
||||||
// If this doesn't make sense, you should go read about functions here: https://www.theodinproject.com/paths/foundations/courses/foundations/lessons/fundamentals-part-3
|
// If this doesn't make sense, you should go read about functions here: https://www.theodinproject.com/paths/foundations/courses/foundations/lessons/fundamentals-part-3
|
||||||
const number = Math.floor(Math.random() * 1000);
|
const number = Math.floor(Math.random() * 1000);
|
||||||
/*The .match(/((hey))/g).length is a regex that will count the number of heys
|
/*The .match(/((hey))/g).length is a regex that will count the number of heys
|
||||||
in the result, which if your function works correctly will equal the number that
|
in the result, which if your function works correctly will equal the number that
|
||||||
was randomaly generated. */
|
was randomaly generated. */
|
||||||
expect(repeatString('hey', number).match(/((hey))/g).length).toEqual(
|
expect(repeatString("hey", number).match(/((hey))/g).length).toEqual(
|
||||||
number
|
number
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
test.skip('works with blank strings', () => {
|
test.skip("works with blank strings", () => {
|
||||||
expect(repeatString('', 10)).toEqual('');
|
expect(repeatString("", 10)).toEqual("");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
const reverseString = function (string) {
|
const reverseString = function (string) {
|
||||||
return string.split('').reverse().join('');
|
return string.split("").reverse().join("");
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = reverseString;
|
module.exports = reverseString;
|
||||||
|
|
|
@ -1,18 +1,18 @@
|
||||||
const reverseString = require('./reverseString-solution');
|
const reverseString = require("./reverseString");
|
||||||
|
|
||||||
describe('reverseString', () => {
|
describe("reverseString", () => {
|
||||||
test('reverses single word', () => {
|
test("reverses single word", () => {
|
||||||
expect(reverseString('hello')).toEqual('olleh');
|
expect(reverseString("hello")).toEqual("olleh");
|
||||||
});
|
});
|
||||||
|
|
||||||
test.skip('reverses multiple words', () => {
|
test.skip("reverses multiple words", () => {
|
||||||
expect(reverseString('hello there')).toEqual('ereht olleh');
|
expect(reverseString("hello there")).toEqual("ereht olleh");
|
||||||
});
|
});
|
||||||
|
|
||||||
test.skip('works with numbers and punctuation', () => {
|
test.skip("works with numbers and punctuation", () => {
|
||||||
expect(reverseString('123! abc!')).toEqual('!cba !321');
|
expect(reverseString("123! abc!")).toEqual("!cba !321");
|
||||||
});
|
});
|
||||||
test.skip('works with blank strings', () => {
|
test.skip("works with blank strings", () => {
|
||||||
expect(reverseString('')).toEqual('');
|
expect(reverseString("")).toEqual("");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,29 +1,26 @@
|
||||||
// we have 2 solutions here, an easier one and a more advanced one.
|
// 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
|
// 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!
|
// is using the rest operator. If this is unfamiliar to you look it up!
|
||||||
const removeFromArray = function (...args) {
|
const removeFromArray = function (array, ...args) {
|
||||||
// the very first item in our list of arguments is the array, we pull it out with args[0]
|
// create a new empty array
|
||||||
const array = args[0];
|
const newArray = [];
|
||||||
// create a new empty array
|
// use forEach to go through the array
|
||||||
const newArray = [];
|
array.forEach((item) => {
|
||||||
// use forEach to go through the array
|
// push every element into the new array
|
||||||
array.forEach((item) => {
|
// UNLESS it is included in the function arguments
|
||||||
// push every element into the new array
|
// so we create a new array with every item, except those that should be removed
|
||||||
// UNLESS it is included in the function arguments
|
if (!args.includes(item)) {
|
||||||
// so we create a new array with every item, except those that should be removed
|
newArray.push(item);
|
||||||
if (!args.includes(item)) {
|
}
|
||||||
newArray.push(item);
|
});
|
||||||
}
|
// and return that array
|
||||||
});
|
return newArray;
|
||||||
// and return that array
|
|
||||||
return newArray;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// A simpler, but more advanced way to do it is to use the 'filter' function,
|
// 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.
|
// which basically does what we did with the forEach above.
|
||||||
|
|
||||||
// var removeFromArray = function(...args) {
|
// var removeFromArray = function(array, ...args) {
|
||||||
// const array = args[0]
|
|
||||||
// return array.filter(val => !args.includes(val))
|
// return array.filter(val => !args.includes(val))
|
||||||
// }
|
// }
|
||||||
//
|
//
|
||||||
|
|
|
@ -1,28 +1,25 @@
|
||||||
const removeFromArray = require('./removeFromArray-solution');
|
const removeFromArray = require("./removeFromArray");
|
||||||
|
|
||||||
describe('removeFromArray', () => {
|
describe("removeFromArray", () => {
|
||||||
test('removes a single value', () => {
|
test("removes a single value", () => {
|
||||||
expect(removeFromArray([1, 2, 3, 4], 3)).toEqual([1, 2, 4]);
|
expect(removeFromArray([1, 2, 3, 4], 3)).toEqual([1, 2, 4]);
|
||||||
});
|
});
|
||||||
test.skip('removes multiple values', () => {
|
test.skip("removes multiple values", () => {
|
||||||
expect(removeFromArray([1, 2, 3, 4], 3, 2)).toEqual([1, 4]);
|
expect(removeFromArray([1, 2, 3, 4], 3, 2)).toEqual([1, 4]);
|
||||||
});
|
});
|
||||||
test.skip('ignores non present values', () => {
|
test.skip("ignores non present values", () => {
|
||||||
expect(removeFromArray([1, 2, 3, 4], 7, 'tacos')).toEqual([1, 2, 3, 4]);
|
expect(removeFromArray([1, 2, 3, 4], 7, "tacos")).toEqual([1, 2, 3, 4]);
|
||||||
});
|
});
|
||||||
test.skip('ignores non present values, but still works', () => {
|
test.skip("ignores non present values, but still works", () => {
|
||||||
expect(removeFromArray([1, 2, 3, 4], 7, 2)).toEqual([1, 3, 4]);
|
expect(removeFromArray([1, 2, 3, 4], 7, 2)).toEqual([1, 3, 4]);
|
||||||
});
|
});
|
||||||
test.skip('can remove all values', () => {
|
test.skip("can remove all values", () => {
|
||||||
expect(removeFromArray([1, 2, 3, 4], 1, 2, 3, 4)).toEqual([]);
|
expect(removeFromArray([1, 2, 3, 4], 1, 2, 3, 4)).toEqual([]);
|
||||||
});
|
});
|
||||||
test.skip('works with strings', () => {
|
test.skip("works with strings", () => {
|
||||||
expect(removeFromArray(['hey', 2, 3, 'ho'], 'hey', 3)).toEqual([
|
expect(removeFromArray(["hey", 2, 3, "ho"], "hey", 3)).toEqual([2, "ho"]);
|
||||||
2,
|
});
|
||||||
'ho',
|
test.skip("only removes same type", () => {
|
||||||
]);
|
expect(removeFromArray([1, 2, 3], "1", 3)).toEqual([1, 2]);
|
||||||
});
|
});
|
||||||
test.skip('only removes same type', () => {
|
|
||||||
expect(removeFromArray([1, 2, 3], '1', 3)).toEqual([1, 2]);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,16 +1,16 @@
|
||||||
const sumAll = function (min, max) {
|
const sumAll = function (min, max) {
|
||||||
if (!Number.isInteger(min) || !Number.isInteger(max)) return 'ERROR';
|
if (!Number.isInteger(min) || !Number.isInteger(max)) return "ERROR";
|
||||||
if (min < 0 || max < 0) return 'ERROR';
|
if (min < 0 || max < 0) return "ERROR";
|
||||||
if (min > max) {
|
if (min > max) {
|
||||||
const temp = min;
|
const temp = min;
|
||||||
min = max;
|
min = max;
|
||||||
max = temp;
|
max = temp;
|
||||||
}
|
}
|
||||||
let sum = 0;
|
let sum = 0;
|
||||||
for (let i = min; i < max + 1; i++) {
|
for (let i = min; i < max + 1; i++) {
|
||||||
sum += i;
|
sum += i;
|
||||||
}
|
}
|
||||||
return sum;
|
return sum;
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = sumAll;
|
module.exports = sumAll;
|
||||||
|
|
|
@ -1,22 +1,25 @@
|
||||||
const sumAll = require('./sumAll-solution');
|
const sumAll = require("./sumAll");
|
||||||
|
|
||||||
describe('sumAll', () => {
|
describe("sumAll", () => {
|
||||||
test('sums numbers within the range', () => {
|
test("sums numbers within the range", () => {
|
||||||
expect(sumAll(1, 4)).toEqual(10);
|
expect(sumAll(1, 4)).toEqual(10);
|
||||||
});
|
});
|
||||||
test.skip('works with large numbers', () => {
|
test.skip("works with large numbers", () => {
|
||||||
expect(sumAll(1, 4000)).toEqual(8002000);
|
expect(sumAll(1, 4000)).toEqual(8002000);
|
||||||
});
|
});
|
||||||
test.skip('works with larger number first', () => {
|
test.skip("works with larger number first", () => {
|
||||||
expect(sumAll(123, 1)).toEqual(7626);
|
expect(sumAll(123, 1)).toEqual(7626);
|
||||||
});
|
});
|
||||||
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-number parameters', () => {
|
test.skip("returns ERROR with non-integer parameters", () => {
|
||||||
expect(sumAll(10, '90')).toEqual('ERROR');
|
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, 1])).toEqual('ERROR');
|
expect(sumAll(10, "90")).toEqual("ERROR");
|
||||||
});
|
});
|
||||||
|
test.skip("returns ERROR with non-number parameters", () => {
|
||||||
|
expect(sumAll(10, [90, 1])).toEqual("ERROR");
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
const leapYears = function (year) {
|
const leapYears = function (year) {
|
||||||
return year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0);
|
return year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0);
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = leapYears;
|
module.exports = leapYears;
|
||||||
|
|
|
@ -1,22 +1,22 @@
|
||||||
const leapYears = require('./leapYears-solution');
|
const leapYears = require("./leapYears");
|
||||||
|
|
||||||
describe('leapYears', () => {
|
describe("leapYears", () => {
|
||||||
test('works with non century years', () => {
|
test("works with non century years", () => {
|
||||||
expect(leapYears(1996)).toBe(true);
|
expect(leapYears(1996)).toBe(true);
|
||||||
});
|
});
|
||||||
test.skip('works with non century years', () => {
|
test.skip("works with non century years", () => {
|
||||||
expect(leapYears(1997)).toBe(false);
|
expect(leapYears(1997)).toBe(false);
|
||||||
});
|
});
|
||||||
test.skip('works with ridiculously futuristic non century years', () => {
|
test.skip("works with ridiculously futuristic non century years", () => {
|
||||||
expect(leapYears(34992)).toBe(true);
|
expect(leapYears(34992)).toBe(true);
|
||||||
});
|
});
|
||||||
test.skip('works with century years', () => {
|
test.skip("works with century years", () => {
|
||||||
expect(leapYears(1900)).toBe(false);
|
expect(leapYears(1900)).toBe(false);
|
||||||
});
|
});
|
||||||
test.skip('works with century years', () => {
|
test.skip("works with century years", () => {
|
||||||
expect(leapYears(1600)).toBe(true);
|
expect(leapYears(1600)).toBe(true);
|
||||||
});
|
});
|
||||||
test.skip('works with century years', () => {
|
test.skip("works with century years", () => {
|
||||||
expect(leapYears(700)).toBe(false);
|
expect(leapYears(700)).toBe(false);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,12 +1,12 @@
|
||||||
const ftoc = function (f) {
|
const convertToCelsius = function (fahrenheit) {
|
||||||
return Math.round((f - 32) * (5 / 9) * 10) / 10;
|
return Math.round((fahrenheit - 32) * (5 / 9) * 10) / 10;
|
||||||
};
|
};
|
||||||
|
|
||||||
const ctof = function (c) {
|
const convertToFahrenheit = function (celsius) {
|
||||||
return Math.round(((c * 9) / 5 + 32) * 10) / 10;
|
return Math.round(((celsius * 9) / 5 + 32) * 10) / 10;
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
ftoc,
|
convertToCelsius,
|
||||||
ctof,
|
convertToFahrenheit,
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,25 +1,25 @@
|
||||||
const { ftoc, ctof } = require('./tempConversion-solution');
|
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);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,48 +1,48 @@
|
||||||
const add = function (a, b) {
|
const add = function (a, b) {
|
||||||
return a + b;
|
return a + b;
|
||||||
};
|
};
|
||||||
|
|
||||||
const subtract = function (a, b) {
|
const subtract = function (a, b) {
|
||||||
return a - b;
|
return a - b;
|
||||||
};
|
};
|
||||||
|
|
||||||
const sum = function (array) {
|
const sum = function (array) {
|
||||||
return array.reduce((total, current) => total + current, 0);
|
return array.reduce((total, current) => total + current, 0);
|
||||||
};
|
};
|
||||||
|
|
||||||
const multiply = function (array) {
|
const multiply = function (array) {
|
||||||
return array.length
|
return array.length
|
||||||
? array.reduce((accumulator, nextItem) => accumulator * nextItem)
|
? array.reduce((accumulator, nextItem) => accumulator * nextItem)
|
||||||
: 0;
|
: 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
const power = function (a, b) {
|
const power = function (a, b) {
|
||||||
return Math.pow(a, b);
|
return Math.pow(a, b);
|
||||||
};
|
};
|
||||||
|
|
||||||
const factorial = function (n) {
|
const factorial = function (n) {
|
||||||
if (n === 0) return 1;
|
if (n === 0) return 1;
|
||||||
let product = 1;
|
let product = 1;
|
||||||
for (let i = n; i > 0; i--) {
|
for (let i = n; i > 0; i--) {
|
||||||
product *= i;
|
product *= i;
|
||||||
}
|
}
|
||||||
return product;
|
return product;
|
||||||
};
|
};
|
||||||
|
|
||||||
// This is another implementation of Factorial that uses recursion
|
// This is another implementation of Factorial that uses recursion
|
||||||
// THANKS to @ThirtyThreeB!
|
// THANKS to @ThirtyThreeB!
|
||||||
const recursiveFactorial = function (n) {
|
const recursiveFactorial = function (n) {
|
||||||
if (n === 0) {
|
if (n === 0) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
return n * recursiveFactorial(n - 1);
|
return n * recursiveFactorial(n - 1);
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
add,
|
add,
|
||||||
subtract,
|
subtract,
|
||||||
sum,
|
sum,
|
||||||
multiply,
|
multiply,
|
||||||
power,
|
power,
|
||||||
factorial,
|
factorial,
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,77 +1,77 @@
|
||||||
const calculator = require('./calculator-solution');
|
const calculator = require("./calculator");
|
||||||
|
|
||||||
describe('add', () => {
|
describe("add", () => {
|
||||||
test('adds 0 and 0', () => {
|
test("adds 0 and 0", () => {
|
||||||
expect(calculator.add(0, 0)).toBe(0);
|
expect(calculator.add(0, 0)).toBe(0);
|
||||||
});
|
});
|
||||||
|
|
||||||
test.skip('adds 2 and 2', () => {
|
test.skip("adds 2 and 2", () => {
|
||||||
expect(calculator.add(2, 2)).toBe(4);
|
expect(calculator.add(2, 2)).toBe(4);
|
||||||
});
|
});
|
||||||
|
|
||||||
test.skip('adds positive numbers', () => {
|
test.skip("adds positive numbers", () => {
|
||||||
expect(calculator.add(2, 6)).toBe(8);
|
expect(calculator.add(2, 6)).toBe(8);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('subtract', () => {
|
describe("subtract", () => {
|
||||||
test.skip('subtracts numbers', () => {
|
test.skip("subtracts numbers", () => {
|
||||||
expect(calculator.subtract(10, 4)).toBe(6);
|
expect(calculator.subtract(10, 4)).toBe(6);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('sum', () => {
|
describe("sum", () => {
|
||||||
test.skip('computes the sum of an empty array', () => {
|
test.skip("computes the sum of an empty array", () => {
|
||||||
expect(calculator.sum([])).toBe(0);
|
expect(calculator.sum([])).toBe(0);
|
||||||
});
|
});
|
||||||
|
|
||||||
test.skip('computes the sum of an array of one number', () => {
|
test.skip("computes the sum of an array of one number", () => {
|
||||||
expect(calculator.sum([7])).toBe(7);
|
expect(calculator.sum([7])).toBe(7);
|
||||||
});
|
});
|
||||||
|
|
||||||
test.skip('computes the sum of an array of two numbers', () => {
|
test.skip("computes the sum of an array of two numbers", () => {
|
||||||
expect(calculator.sum([7, 11])).toBe(18);
|
expect(calculator.sum([7, 11])).toBe(18);
|
||||||
});
|
});
|
||||||
|
|
||||||
test.skip('computes the sum of an array of many numbers', () => {
|
test.skip("computes the sum of an array of many numbers", () => {
|
||||||
expect(calculator.sum([1, 3, 5, 7, 9])).toBe(25);
|
expect(calculator.sum([1, 3, 5, 7, 9])).toBe(25);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('multiply', () => {
|
describe("multiply", () => {
|
||||||
test.skip('multiplies two numbers', () => {
|
test.skip("multiplies two numbers", () => {
|
||||||
expect(calculator.multiply([2, 4])).toBe(8);
|
expect(calculator.multiply([2, 4])).toBe(8);
|
||||||
});
|
});
|
||||||
|
|
||||||
test.skip('multiplies several numbers', () => {
|
test.skip("multiplies several numbers", () => {
|
||||||
expect(calculator.multiply([2, 4, 6, 8, 10, 12, 14])).toBe(645120);
|
expect(calculator.multiply([2, 4, 6, 8, 10, 12, 14])).toBe(645120);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('power', () => {
|
describe("power", () => {
|
||||||
test.skip('raises one number to the power of another number', () => {
|
test.skip("raises one number to the power of another number", () => {
|
||||||
expect(calculator.power(4, 3)).toBe(64); // 4 to third power is 64
|
expect(calculator.power(4, 3)).toBe(64); // 4 to third power is 64
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('factorial', () => {
|
describe("factorial", () => {
|
||||||
test.skip('computes the factorial of 0', () => {
|
test.skip("computes the factorial of 0", () => {
|
||||||
expect(calculator.factorial(0)).toBe(1); // 0! = 1
|
expect(calculator.factorial(0)).toBe(1); // 0! = 1
|
||||||
});
|
});
|
||||||
|
|
||||||
test.skip('computes the factorial of 1', () => {
|
test.skip("computes the factorial of 1", () => {
|
||||||
expect(calculator.factorial(1)).toBe(1);
|
expect(calculator.factorial(1)).toBe(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
test.skip('computes the factorial of 2', () => {
|
test.skip("computes the factorial of 2", () => {
|
||||||
expect(calculator.factorial(2)).toBe(2);
|
expect(calculator.factorial(2)).toBe(2);
|
||||||
});
|
});
|
||||||
|
|
||||||
test.skip('computes the factorial of 5', () => {
|
test.skip("computes the factorial of 5", () => {
|
||||||
expect(calculator.factorial(5)).toBe(120);
|
expect(calculator.factorial(5)).toBe(120);
|
||||||
});
|
});
|
||||||
|
|
||||||
test.skip('computes the factorial of 10', () => {
|
test.skip("computes the factorial of 10", () => {
|
||||||
expect(calculator.factorial(10)).toBe(3628800);
|
expect(calculator.factorial(10)).toBe(3628800);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
const palindromes = function (string) {
|
const palindromes = function (string) {
|
||||||
processedString = string.toLowerCase().replace(/[^a-z]/g, '');
|
const processedString = string.toLowerCase().replace(/[^a-z]/g, "");
|
||||||
return processedString.split('').reverse().join('') == processedString;
|
return processedString.split("").reverse().join("") == processedString;
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = palindromes;
|
module.exports = palindromes;
|
||||||
|
|
|
@ -1,24 +1,24 @@
|
||||||
const palindromes = require('./palindromes-solution');
|
const palindromes = require("./palindromes");
|
||||||
|
|
||||||
describe('palindromes', () => {
|
describe("palindromes", () => {
|
||||||
test('works with single words', () => {
|
test("works with single words", () => {
|
||||||
expect(palindromes('racecar')).toBe(true);
|
expect(palindromes("racecar")).toBe(true);
|
||||||
});
|
});
|
||||||
test.skip('works with punctuation ', () => {
|
test.skip("works with punctuation ", () => {
|
||||||
expect(palindromes('racecar!')).toBe(true);
|
expect(palindromes("racecar!")).toBe(true);
|
||||||
});
|
});
|
||||||
test.skip('works with upper-case letters ', () => {
|
test.skip("works with upper-case letters ", () => {
|
||||||
expect(palindromes('Racecar!')).toBe(true);
|
expect(palindromes("Racecar!")).toBe(true);
|
||||||
});
|
});
|
||||||
test.skip('works with multiple words', () => {
|
test.skip("works with multiple words", () => {
|
||||||
expect(palindromes('A car, a man, a maraca.')).toBe(true);
|
expect(palindromes("A car, a man, a maraca.")).toBe(true);
|
||||||
});
|
});
|
||||||
test.skip('works with multiple words', () => {
|
test.skip("works with multiple words", () => {
|
||||||
expect(
|
expect(palindromes("Animal loots foliated detail of stool lamina.")).toBe(
|
||||||
palindromes('Animal loots foliated detail of stool lamina.')
|
true
|
||||||
).toBe(true);
|
);
|
||||||
});
|
});
|
||||||
test.skip("doesn't just always return true", () => {
|
test.skip("doesn't just always return true", () => {
|
||||||
expect(palindromes('ZZZZ car, a man, a maraca.')).toBe(false);
|
expect(palindromes("ZZZZ car, a man, a maraca.")).toBe(false);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,14 +1,14 @@
|
||||||
const fibonacci = function (count) {
|
const fibonacci = function (count) {
|
||||||
if (count < 0) return 'OOPS';
|
if (count < 0) return "OOPS";
|
||||||
if (count === 0) return 0;
|
if (count === 0) return 0;
|
||||||
let a = 0;
|
let a = 0;
|
||||||
let b = 1;
|
let b = 1;
|
||||||
for (let i = 1; i < count; i++) {
|
for (let i = 1; i < count; i++) {
|
||||||
const temp = b;
|
const temp = b;
|
||||||
b = a + b;
|
b = a + b;
|
||||||
a = temp;
|
a = temp;
|
||||||
}
|
}
|
||||||
return b;
|
return b;
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = fibonacci;
|
module.exports = fibonacci;
|
||||||
|
|
|
@ -1,31 +1,31 @@
|
||||||
const fibonacci = require('./fibonacci-solution');
|
const fibonacci = require("./fibonacci");
|
||||||
|
|
||||||
describe('fibonacci', () => {
|
describe("fibonacci", () => {
|
||||||
test('4th fibonacci number is 3', () => {
|
test("4th fibonacci number is 3", () => {
|
||||||
expect(fibonacci(4)).toBe(3);
|
expect(fibonacci(4)).toBe(3);
|
||||||
});
|
});
|
||||||
test.skip('6th fibonacci number is 8', () => {
|
test.skip("6th fibonacci number is 8", () => {
|
||||||
expect(fibonacci(6)).toBe(8);
|
expect(fibonacci(6)).toBe(8);
|
||||||
});
|
});
|
||||||
test.skip('10th fibonacci number is 55', () => {
|
test.skip("10th fibonacci number is 55", () => {
|
||||||
expect(fibonacci(10)).toBe(55);
|
expect(fibonacci(10)).toBe(55);
|
||||||
});
|
});
|
||||||
test.skip('15th fibonacci number is 610', () => {
|
test.skip("15th fibonacci number is 610", () => {
|
||||||
expect(fibonacci(15)).toBe(610);
|
expect(fibonacci(15)).toBe(610);
|
||||||
});
|
});
|
||||||
test.skip('25th fibonacci number is 75025', () => {
|
test.skip("25th fibonacci number is 75025", () => {
|
||||||
expect(fibonacci(25)).toBe(75025);
|
expect(fibonacci(25)).toBe(75025);
|
||||||
});
|
});
|
||||||
test.skip("doesn't accept negatives", () => {
|
test.skip("doesn't accept negatives", () => {
|
||||||
expect(fibonacci(-25)).toBe('OOPS');
|
expect(fibonacci(-25)).toBe("OOPS");
|
||||||
});
|
});
|
||||||
test.skip('DOES accept strings', () => {
|
test.skip("DOES accept strings", () => {
|
||||||
expect(fibonacci('1')).toBe(1);
|
expect(fibonacci("1")).toBe(1);
|
||||||
});
|
});
|
||||||
test.skip('DOES accept strings', () => {
|
test.skip("DOES accept strings", () => {
|
||||||
expect(fibonacci('2')).toBe(1);
|
expect(fibonacci("2")).toBe(1);
|
||||||
});
|
});
|
||||||
test.skip('DOES accept strings', () => {
|
test.skip("DOES accept strings", () => {
|
||||||
expect(fibonacci('8')).toBe(21);
|
expect(fibonacci("8")).toBe(21);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
const getTheTitles = function (array) {
|
const getTheTitles = function (array) {
|
||||||
return array.map((book) => book.title);
|
return array.map((book) => book.title);
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = getTheTitles;
|
module.exports = getTheTitles;
|
||||||
|
|
|
@ -1,18 +1,18 @@
|
||||||
const getTheTitles = require('./getTheTitles-solution');
|
const getTheTitles = require("./getTheTitles");
|
||||||
|
|
||||||
describe('getTheTitles', () => {
|
describe("getTheTitles", () => {
|
||||||
const books = [
|
const books = [
|
||||||
{
|
{
|
||||||
title: 'Book',
|
title: "Book",
|
||||||
author: 'Name',
|
author: "Name",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: 'Book2',
|
title: "Book2",
|
||||||
author: 'Name2',
|
author: "Name2",
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
test('gets titles', () => {
|
test("gets titles", () => {
|
||||||
expect(getTheTitles(books)).toEqual(['Book', 'Book2']);
|
expect(getTheTitles(books)).toEqual(["Book", "Book2"]);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,19 +1,19 @@
|
||||||
const findTheOldest = function (array) {
|
const findTheOldest = function (array) {
|
||||||
return array.reduce((oldest, currentPerson) => {
|
return array.reduce((oldest, currentPerson) => {
|
||||||
const oldestAge = getAge(oldest.yearOfBirth, oldest.yearOfDeath);
|
const oldestAge = getAge(oldest.yearOfBirth, oldest.yearOfDeath);
|
||||||
const currentAge = getAge(
|
const currentAge = getAge(
|
||||||
currentPerson.yearOfBirth,
|
currentPerson.yearOfBirth,
|
||||||
currentPerson.yearOfDeath
|
currentPerson.yearOfDeath
|
||||||
);
|
);
|
||||||
return oldestAge < currentAge ? currentPerson : oldest;
|
return oldestAge < currentAge ? currentPerson : oldest;
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
const getAge = function (birth, death) {
|
const getAge = function (birth, death) {
|
||||||
if (!death) {
|
if (!death) {
|
||||||
death = new Date().getFullYear();
|
death = new Date().getFullYear();
|
||||||
}
|
}
|
||||||
return death - birth;
|
return death - birth;
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = findTheOldest;
|
module.exports = findTheOldest;
|
||||||
|
|
|
@ -1,62 +1,62 @@
|
||||||
const findTheOldest = require('./findTheOldest-solution');
|
const findTheOldest = require("./findTheOldest");
|
||||||
|
|
||||||
describe('findTheOldest', () => {
|
describe("findTheOldest", () => {
|
||||||
test('finds the oldest person!', () => {
|
test("finds the oldest person!", () => {
|
||||||
const people = [
|
const people = [
|
||||||
{
|
{
|
||||||
name: 'Carly',
|
name: "Carly",
|
||||||
yearOfBirth: 1942,
|
yearOfBirth: 1942,
|
||||||
yearOfDeath: 1970,
|
yearOfDeath: 1970,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'Ray',
|
name: "Ray",
|
||||||
yearOfBirth: 1962,
|
yearOfBirth: 1962,
|
||||||
yearOfDeath: 2011,
|
yearOfDeath: 2011,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'Jane',
|
name: "Jane",
|
||||||
yearOfBirth: 1912,
|
yearOfBirth: 1912,
|
||||||
yearOfDeath: 1941,
|
yearOfDeath: 1941,
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
expect(findTheOldest(people).name).toBe('Ray');
|
expect(findTheOldest(people).name).toBe("Ray");
|
||||||
});
|
});
|
||||||
test.skip('finds the oldest person if someone is still living', () => {
|
test.skip("finds the oldest person if someone is still living", () => {
|
||||||
const people = [
|
const people = [
|
||||||
{
|
{
|
||||||
name: 'Carly',
|
name: "Carly",
|
||||||
yearOfBirth: 2018,
|
yearOfBirth: 2018,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'Ray',
|
name: "Ray",
|
||||||
yearOfBirth: 1962,
|
yearOfBirth: 1962,
|
||||||
yearOfDeath: 2011,
|
yearOfDeath: 2011,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'Jane',
|
name: "Jane",
|
||||||
yearOfBirth: 1912,
|
yearOfBirth: 1912,
|
||||||
yearOfDeath: 1941,
|
yearOfDeath: 1941,
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
expect(findTheOldest(people).name).toBe('Ray');
|
expect(findTheOldest(people).name).toBe("Ray");
|
||||||
});
|
});
|
||||||
test.skip('finds the oldest person if the OLDEST is still living', () => {
|
test.skip("finds the oldest person if the OLDEST is still living", () => {
|
||||||
const people = [
|
const people = [
|
||||||
{
|
{
|
||||||
name: 'Carly',
|
name: "Carly",
|
||||||
yearOfBirth: 1066,
|
yearOfBirth: 1066,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'Ray',
|
name: "Ray",
|
||||||
yearOfBirth: 1962,
|
yearOfBirth: 1962,
|
||||||
yearOfDeath: 2011,
|
yearOfDeath: 2011,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'Jane',
|
name: "Jane",
|
||||||
yearOfBirth: 1912,
|
yearOfBirth: 1912,
|
||||||
yearOfDeath: 1941,
|
yearOfDeath: 1941,
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
expect(findTheOldest(people).name).toBe('Carly');
|
expect(findTheOldest(people).name).toBe("Carly");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
const caesar = function (string, shift) {
|
const caesar = function (string, shift) {
|
||||||
return string
|
return string
|
||||||
.split('')
|
.split("")
|
||||||
.map((char) => shiftChar(char, shift))
|
.map((char) => shiftChar(char, shift))
|
||||||
.join('');
|
.join("");
|
||||||
};
|
};
|
||||||
|
|
||||||
const codeSet = (code) => (code < 97 ? 65 : 97);
|
const codeSet = (code) => (code < 97 ? 65 : 97);
|
||||||
|
@ -13,14 +13,14 @@ const codeSet = (code) => (code < 97 ? 65 : 97);
|
||||||
const mod = (n, m) => ((n % m) + m) % m;
|
const mod = (n, m) => ((n % m) + m) % m;
|
||||||
|
|
||||||
const shiftChar = (char, shift) => {
|
const shiftChar = (char, shift) => {
|
||||||
const code = char.charCodeAt();
|
const code = char.charCodeAt();
|
||||||
|
|
||||||
if ((code >= 65 && code <= 90) || (code >= 97 && code <= 122)) {
|
if ((code >= 65 && code <= 90) || (code >= 97 && code <= 122)) {
|
||||||
return String.fromCharCode(
|
return String.fromCharCode(
|
||||||
mod(code + shift - codeSet(code), 26) + codeSet(code)
|
mod(code + shift - codeSet(code), 26) + codeSet(code)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
return char;
|
return char;
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = caesar;
|
module.exports = caesar;
|
||||||
|
|
|
@ -1,23 +1,23 @@
|
||||||
const caesar = require('./caesar-solution');
|
const caesar = require("./caesar");
|
||||||
|
|
||||||
test('works with single letters', () => {
|
test("works with single letters", () => {
|
||||||
expect(caesar('A', 1)).toBe('B');
|
expect(caesar("A", 1)).toBe("B");
|
||||||
});
|
});
|
||||||
test.skip('works with words', () => {
|
test.skip("works with words", () => {
|
||||||
expect(caesar('Aaa', 1)).toBe('Bbb');
|
expect(caesar("Aaa", 1)).toBe("Bbb");
|
||||||
});
|
});
|
||||||
test.skip('works with phrases', () => {
|
test.skip("works with phrases", () => {
|
||||||
expect(caesar('Hello, World!', 5)).toBe('Mjqqt, Btwqi!');
|
expect(caesar("Hello, World!", 5)).toBe("Mjqqt, Btwqi!");
|
||||||
});
|
});
|
||||||
test.skip('works with negative shift', () => {
|
test.skip("works with negative shift", () => {
|
||||||
expect(caesar('Mjqqt, Btwqi!', -5)).toBe('Hello, World!');
|
expect(caesar("Mjqqt, Btwqi!", -5)).toBe("Hello, World!");
|
||||||
});
|
});
|
||||||
test.skip('wraps', () => {
|
test.skip("wraps", () => {
|
||||||
expect(caesar('Z', 1)).toBe('A');
|
expect(caesar("Z", 1)).toBe("A");
|
||||||
});
|
});
|
||||||
test.skip('works with large shift factors', () => {
|
test.skip("works with large shift factors", () => {
|
||||||
expect(caesar('Hello, World!', 75)).toBe('Ebiil, Tloia!');
|
expect(caesar("Hello, World!", 75)).toBe("Ebiil, Tloia!");
|
||||||
});
|
});
|
||||||
test.skip('works with large negative shift factors', () => {
|
test.skip("works with large negative shift factors", () => {
|
||||||
expect(caesar('Hello, World!', -29)).toBe('Ebiil, Tloia!');
|
expect(caesar("Hello, World!", -29)).toBe("Ebiil, Tloia!");
|
||||||
});
|
});
|
||||||
|
|
23
README.md
23
README.md
|
@ -10,17 +10,22 @@ If you have a suggestion to improve an exercise, an idea for a new exercise, or
|
||||||
|
|
||||||
## How To Use These Exercises
|
## How To Use These Exercises
|
||||||
|
|
||||||
1. Fork and clone this repository. To learn how to fork a repository, see the GitHub documentation on how to [fork a repo](https://docs.github.com/en/get-started/quickstart/fork-a-repo).
|
1. Fork and clone this repository. To learn how to fork a repository, see the GitHub documentation on how to [fork a repo](https://docs.github.com/en/get-started/quickstart/fork-a-repo).
|
||||||
* Copies of repositories on your machine are called clones. If you need help cloning to your local environment you can learn how from the GitHub documentation on [cloning a repository](https://docs.github.com/en/github/creating-cloning-and-archiving-repositories/cloning-a-repository-from-github/cloning-a-repository).
|
- Copies of repositories on your machine are called clones. If you need help cloning to your local environment you can learn how from the GitHub documentation on [cloning a repository](https://docs.github.com/en/github/creating-cloning-and-archiving-repositories/cloning-a-repository-from-github/cloning-a-repository).
|
||||||
2. Before you start working on any execises, you should first ensure you have the following installed:
|
2. Before you start working on any execises, you should first ensure you have the following installed:
|
||||||
* **NPM**. You should have installed NPM already in our [Installing Node.js](https://www.theodinproject.com/paths/foundations/courses/foundations/lessons/installing-node-js) lesson. Just in case you need to check, type `npm --version` in your terminal. If you get back `Command 'npm' not found, but can be installed with:`, **do not follow the instructions in the terminal** to install with `apt-get` as this causes permission issues. Instead, go back to the installation lesson and install Node with NVM by following the instructions there.
|
- **NPM**. You should have installed NPM already in our [Installing Node.js](https://www.theodinproject.com/paths/foundations/courses/foundations/lessons/installing-node-js) lesson. Just in case you need to check, type `npm --version` in your terminal. If you get back `Command 'npm' not found, but can be installed with:`, **do not follow the instructions in the terminal** to install with `apt-get` as this causes permission issues. Instead, go back to the installation lesson and install Node with NVM by following the instructions there.
|
||||||
* **Jest**. After cloning this repository to your local machine and installing NPM, go into the newly created directory (`cd javascript-exercises`) and run `npm install`. This will install Jest and set up the testing platform based on our preconfigured settings.
|
- **Jest**. After cloning this repository to your local machine and installing NPM, go into the newly created directory (`cd javascript-exercises`) and run `npm install`. This will install Jest and set up the testing platform based on our preconfigured settings.
|
||||||
3. Each exercise includes 3 files: a markdown file with a description of the task, an empty (or mostly empty) JavaScript file, and a set of tests. To complete an exercise, you'll need to go to the exercise directory with `cd exerciseName` in the terminal and run `npm test exerciseName.spec.js`. This should run the test file and show you the output.
|
3. Each exercise includes the following:
|
||||||
* When you first run a test, it will fail. This is by design! You must open the exercise file and write the code needed to get the test to pass.
|
|
||||||
|
- A markdown file with a description of the task, an empty (or mostly empty) JavaScript file, and a set of tests.
|
||||||
|
- A `solutions` directory that contains a solution and the same test file with all of the tests unskipped.
|
||||||
|
|
||||||
|
To complete an exercise, you'll need to go to the exercise directory with `cd exerciseName` in the terminal and run `npm test exerciseName.spec.js`. This should run the test file and show you the output. When you first run a test, it will fail. This is by design! You must open the exercise file and write the code needed to get the test to pass.
|
||||||
|
|
||||||
4. Some of the exercises have test conditions defined in their spec file as `test.skip` compared to `test`. This is purposeful. After you pass one `test`, you will change the next `test.skip` to `test` and test your code again. You'll do this until all conditions are satisfied. **All tests must pass at the same time**, and you should not have any `test.skip` instances by the time you finish an exercise.
|
4. Some of the exercises have test conditions defined in their spec file as `test.skip` compared to `test`. This is purposeful. After you pass one `test`, you will change the next `test.skip` to `test` and test your code again. You'll do this until all conditions are satisfied. **All tests must pass at the same time**, and you should not have any `test.skip` instances by the time you finish an exercise.
|
||||||
5. Once you successfully finish an exercise, check TOP's `solutions` branch to compare it with yours.
|
5. Once you successfully finish an exercise, check the `solutions` directory within each exercise to compare it with yours.
|
||||||
* You should not be checking the solution for an exercise until you finish it!
|
- You should not be checking the solution for an exercise until you finish it!
|
||||||
* Keep in mind that TOP's solution is not the only solution. Generally as long as all of the tests pass, your solution should be fine.
|
- Keep in mind that TOP's solution is not the only solution. Generally as long as all of the tests pass, your solution should be fine.
|
||||||
6. Do not submit your solutions to this repo, as any PRs that do so will be closed without merging.
|
6. Do not submit your solutions to this repo, as any PRs that do so will be closed without merging.
|
||||||
|
|
||||||
**Note**: Due to the way Jest handles failed tests, it may return an exit code of 1 if any tests fail. NPM will interpret this as an error and you may see some `npm ERR!` messages after Jest runs. You can ignore these, or run your test with `npm test exerciseName.spec.js --silent` to supress the errors.
|
**Note**: Due to the way Jest handles failed tests, it may return an exit code of 1 if any tests fail. NPM will interpret this as an error and you may see some `npm ERR!` messages after Jest runs. You can ignore these, or run your test with `npm test exerciseName.spec.js --silent` to supress the errors.
|
||||||
|
|
|
@ -0,0 +1,21 @@
|
||||||
|
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;
|
|
@ -0,0 +1,56 @@
|
||||||
|
const pigLatin = require("./pigLatin");
|
||||||
|
|
||||||
|
// Topics
|
||||||
|
|
||||||
|
// * modules
|
||||||
|
// * strings
|
||||||
|
|
||||||
|
// Pig Latin
|
||||||
|
|
||||||
|
// Pig Latin is a made-up children's language that's intended to be confusing. test obeys a few simple rules (below) but when test's spoken quickly test's really difficult for non-children (and non-native speakers) to understand.
|
||||||
|
|
||||||
|
// Rule 1: If a word begins with a vowel sound, add an "ay" sound to the end of the word.
|
||||||
|
|
||||||
|
// Rule 2: If a word begins with a consonant sound, move test to the end of the word, and then add an "ay" sound to the end of the word.
|
||||||
|
|
||||||
|
// (There are a few more rules for edge cases, and there are regional variants too, but that should be enough to understand the tests.)
|
||||||
|
|
||||||
|
// See https://en.wikipedia.org/wiki/Pig_Latin for more details.
|
||||||
|
|
||||||
|
describe("translate", () => {
|
||||||
|
test("translates a word beginning with a vowel", () => {
|
||||||
|
expect(pigLatin("apple")).toBe("appleay");
|
||||||
|
});
|
||||||
|
|
||||||
|
test.skip("translates a word beginning with a consonant", () => {
|
||||||
|
expect(pigLatin("banana")).toBe("ananabay");
|
||||||
|
});
|
||||||
|
|
||||||
|
test.skip("translates a word beginning with two consonants", () => {
|
||||||
|
expect(pigLatin("cherry")).toBe("errychay");
|
||||||
|
});
|
||||||
|
|
||||||
|
test.skip("translates two words", () => {
|
||||||
|
expect(pigLatin("eat pie")).toBe("eatay iepay");
|
||||||
|
});
|
||||||
|
|
||||||
|
test.skip("translates a word beginning with three consonants", () => {
|
||||||
|
expect(pigLatin("three")).toBe("eethray");
|
||||||
|
});
|
||||||
|
|
||||||
|
test.skip('counts "sch" as a single phoneme', () => {
|
||||||
|
expect(pigLatin("school")).toBe("oolschay");
|
||||||
|
});
|
||||||
|
|
||||||
|
test.skip('counts "qu" as a single phoneme', () => {
|
||||||
|
expect(pigLatin("quiet")).toBe("ietquay");
|
||||||
|
});
|
||||||
|
|
||||||
|
test.skip('counts "qu" as a consonant even when its preceded by a consonant', () => {
|
||||||
|
expect(pigLatin("square")).toBe("aresquay");
|
||||||
|
});
|
||||||
|
|
||||||
|
test.skip("translates many words", () => {
|
||||||
|
expect(pigLatin("the quick brown fox")).toBe("ethay ickquay ownbray oxfay");
|
||||||
|
});
|
||||||
|
});
|
|
@ -0,0 +1,19 @@
|
||||||
|
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;
|
|
@ -0,0 +1,26 @@
|
||||||
|
const snakeCase = require("./snakeCase");
|
||||||
|
|
||||||
|
describe("snakeCase", () => {
|
||||||
|
test("works with simple lowercased phrases", () => {
|
||||||
|
expect(snakeCase("hello world")).toEqual("hello_world");
|
||||||
|
});
|
||||||
|
test.skip("works with Caps and punctuation", () => {
|
||||||
|
expect(snakeCase("Hello, World???")).toEqual("hello_world");
|
||||||
|
});
|
||||||
|
test.skip("works with longer phrases", () => {
|
||||||
|
expect(snakeCase("This is the song that never ends....")).toEqual(
|
||||||
|
"this_is_the_song_that_never_ends"
|
||||||
|
);
|
||||||
|
});
|
||||||
|
test.skip("works with camel case", () => {
|
||||||
|
expect(snakeCase("snakeCase")).toEqual("snake_case");
|
||||||
|
});
|
||||||
|
test.skip("works with kebab case", () => {
|
||||||
|
expect(snakeCase("snake-case")).toEqual("snake_case");
|
||||||
|
});
|
||||||
|
test.skip("works with WTF case", () => {
|
||||||
|
expect(snakeCase("SnAkE..CaSe..Is..AwEsOmE")).toEqual(
|
||||||
|
"snake_case_is_awesome"
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
Loading…
Reference in New Issue