Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test exercises #507

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions 01_helloWorld/helloWorld.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const helloWorld = function() {
return ''
const helloWorld = function () {
return "Hello, World!";
};

module.exports = helloWorld;
6 changes: 3 additions & 3 deletions 01_helloWorld/solution/helloWorld-solution.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const helloWorld = require('./helloWorld-solution');
const helloWorld = require("./helloWorld-solution");

describe('Hello World', function () {
describe("Hello World", function () {
test('says "Hello, World!"', function () {
expect(helloWorld()).toEqual('Hello, World!');
expect(helloWorld()).toEqual("Hello, World!");
});
});
8 changes: 6 additions & 2 deletions 02_repeatString/repeatString.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
const repeatString = function() {

const repeatString = function (string, num) {
let result = "";
for (let i = 0; i < num; i++) {
result += string;
}
return result;
};

// Do not edit below this line
Expand Down
5 changes: 3 additions & 2 deletions 03_reverseString/reverseString.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const reverseString = function() {

const reverseString = function (input) {
let text = input.split("").reverse().join("");
return text;
};

// Do not edit below this line
Expand Down
22 changes: 11 additions & 11 deletions 03_reverseString/reverseString.spec.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
const reverseString = require('./reverseString');
const reverseString = require("./reverseString");

describe('reverseString', () => {
test('reverses single word', () => {
expect(reverseString('hello')).toEqual('olleh');
describe("reverseString", () => {
test("reverses single word", () => {
expect(reverseString("hello")).toEqual("olleh");
});

test.skip('reverses multiple words', () => {
expect(reverseString('hello there')).toEqual('ereht olleh');
test("reverses multiple words", () => {
expect(reverseString("hello there")).toEqual("ereht olleh");
});

test.skip('works with numbers and punctuation', () => {
expect(reverseString('123! abc! Hello, Odinite.')).toEqual(
'.etinidO ,olleH !cba !321'
test("works with numbers and punctuation", () => {
expect(reverseString("123! abc! Hello, Odinite.")).toEqual(
".etinidO ,olleH !cba !321"
);
});
test.skip('works with blank strings', () => {
expect(reverseString('')).toEqual('');
test("works with blank strings", () => {
expect(reverseString("")).toEqual("");
});
});
4 changes: 3 additions & 1 deletion 04_removeFromArray/removeFromArray.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const removeFromArray = function() {
const removeFromArray = function (array, ...args) {
let result = array.filter((item) => !args.includes(item));
return result;
};

// Do not edit below this line
Expand Down
20 changes: 10 additions & 10 deletions 04_removeFromArray/removeFromArray.spec.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
const removeFromArray = require('./removeFromArray')
const removeFromArray = require("./removeFromArray");

describe('removeFromArray', () => {
test('removes a single value', () => {
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('removes multiple of the same value', () => {
test("removes multiple of the same value", () => {
expect(removeFromArray([1, 2, 2, 3], 2)).toEqual([1, 3]);
});
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]);
});
});
22 changes: 21 additions & 1 deletion 05_sumAll/sumAll.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
const sumAll = function() {
const sumAll = function (start, end) {
// The function first checks if the inputs are valid numbers and positive. If not, it returns 'ERROR'
if (
start < 0 ||
end < 0 ||
!Number.isInteger(start) ||
!Number.isInteger(end)
) {
return "ERROR";
}

// Ensure that start is always the smaller number
if (start > end) {
[start, end] = [end, start];
}

// Loop through numbers from start to end
let sum = 0;
for (let i = start; i <= end; i++) {
sum += i;
}
return sum;
};

// Do not edit below this line
Expand Down
26 changes: 13 additions & 13 deletions 05_sumAll/sumAll.spec.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
const sumAll = require('./sumAll')
const sumAll = require("./sumAll");

describe('sumAll', () => {
test('sums numbers within the range', () => {
describe("sumAll", () => {
test("sums numbers within the range", () => {
expect(sumAll(1, 4)).toEqual(10);
});
test.skip('works with large numbers', () => {
test("works with large numbers", () => {
expect(sumAll(1, 4000)).toEqual(8002000);
});
test.skip('works with larger number first', () => {
test("works with larger number first", () => {
expect(sumAll(123, 1)).toEqual(7626);
});
test.skip('returns ERROR with negative numbers', () => {
expect(sumAll(-10, 4)).toEqual('ERROR');
test("returns ERROR with negative numbers", () => {
expect(sumAll(-10, 4)).toEqual("ERROR");
});
test.skip('returns ERROR with non-integer parameters', () => {
expect(sumAll(2.5, 4)).toEqual('ERROR');
test("returns ERROR with non-integer parameters", () => {
expect(sumAll(2.5, 4)).toEqual("ERROR");
});
test.skip('returns ERROR with non-number parameters', () => {
expect(sumAll(10, "90")).toEqual('ERROR');
test("returns ERROR with non-number parameters", () => {
expect(sumAll(10, "90")).toEqual("ERROR");
});
test.skip('returns ERROR with non-number parameters', () => {
expect(sumAll(10, [90, 1])).toEqual('ERROR');
test("returns ERROR with non-number parameters", () => {
expect(sumAll(10, [90, 1])).toEqual("ERROR");
});
});
12 changes: 10 additions & 2 deletions 06_leapYears/leapYears.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
const leapYears = function() {

const leapYears = function (number) {
if (number % 400 === 0) {
return true;
} else if (number % 100 === 0) {
return false;
} else if (number % 4 === 0) {
return true;
} else {
return false;
}
};

// Do not edit below this line
Expand Down
16 changes: 8 additions & 8 deletions 06_leapYears/leapYears.spec.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
const leapYears = require('./leapYears')
const leapYears = require("./leapYears");

describe('leapYears', () => {
test('works with non century years', () => {
describe("leapYears", () => {
test("works with non century years", () => {
expect(leapYears(1996)).toBe(true);
});
test.skip('works with non century years', () => {
test("works with non century years", () => {
expect(leapYears(1997)).toBe(false);
});
test.skip('works with ridiculously futuristic non century years', () => {
test("works with ridiculously futuristic non century years", () => {
expect(leapYears(34992)).toBe(true);
});
test.skip('works with century years', () => {
test("works with century years", () => {
expect(leapYears(1900)).toBe(false);
});
test.skip('works with century years', () => {
test("works with century years", () => {
expect(leapYears(1600)).toBe(true);
});
test.skip('works with century years', () => {
test("works with century years", () => {
expect(leapYears(700)).toBe(false);
});
});
13 changes: 10 additions & 3 deletions 07_tempConversion/tempConversion.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
const convertToCelsius = function() {
const convertToCelsius = function (fahrenheit) {
const celciusConversionFormula = ((fahrenheit - 32) * 5) / 9;
const roundoff = Math.round(celciusConversionFormula * 10) / 10;

return roundoff;
};

const convertToFahrenheit = function() {
const convertToFahrenheit = function (celcius) {
const fahrenheitConversionFormula = (9 / 5) * celcius + 32;
const roundoff = Math.round(fahrenheitConversionFormula * 10) / 10;
return roundoff;
};

// Do not edit below this line
module.exports = {
convertToCelsius,
convertToFahrenheit
convertToFahrenheit,
};
18 changes: 9 additions & 9 deletions 07_tempConversion/tempConversion.spec.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
const {convertToCelsius, convertToFahrenheit} = require('./tempConversion')
const { convertToCelsius, convertToFahrenheit } = require("./tempConversion");

describe('convertToCelsius', () => {
test('works', () => {
describe("convertToCelsius", () => {
test("works", () => {
expect(convertToCelsius(32)).toEqual(0);
});
test.skip('rounds to 1 decimal', () => {
test("rounds to 1 decimal", () => {
expect(convertToCelsius(100)).toEqual(37.8);
});
test.skip('works with negatives', () => {
test.skip("works with negatives", () => {
expect(convertToCelsius(-100)).toEqual(-73.3);
});
});

describe('convertToFahrenheit', () => {
test.skip('works', () => {
describe("convertToFahrenheit", () => {
test("works", () => {
expect(convertToFahrenheit(0)).toEqual(32);
});
test.skip('rounds to 1 decimal', () => {
test("rounds to 1 decimal", () => {
expect(convertToFahrenheit(73.2)).toEqual(163.8);
});
test.skip('works with negatives', () => {
test("works with negatives", () => {
expect(convertToFahrenheit(-10)).toEqual(14);
});
});
33 changes: 20 additions & 13 deletions 08_calculator/calculator.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,32 @@
const add = function() {

};
// const { factorial } = require("./solution/calculator-solution");

const subtract = function() {
const add = function (a, b) {
return a + b;
};

const sum = function() {
const subtract = function (a, b) {
return a - b;
};

const multiply = function() {
const sum = function (arr) {
let sum = arr.reduce((sum, current) => sum + current, 0);
return sum;
};

const multiply = function (arr) {
let multiply = arr.reduce((accumulator, current) => accumulator * current, 1);
return multiply;
};

const power = function() {

const power = function (a, b) {
let value = Math.pow(a, b);
return value;
};

const factorial = function() {

const factorial = function (num) {
if (num < 0) return -1;
if (num === 0) return 1;
else return num * factorial(num - 1);
};

// Do not edit below this line
Expand All @@ -29,5 +36,5 @@ module.exports = {
sum,
multiply,
power,
factorial
factorial,
};
Loading