first commit

This commit is contained in:
MortezaTavasoly 2023-10-23 04:59:32 -08:00
parent 157972d135
commit 50238a402c
21 changed files with 262 additions and 166 deletions

View File

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

View File

@ -1,6 +1,14 @@
const repeatString = function() {
const repeatString = function (word, times) {
if (times < 0) return "ERROR";
let myWord = "";
if (times > 0) {
for (let i = 0; i < times; i++) {
myWord += word;
}
}
return myWord;
};
repeatString("hey", 0);
// Do not edit below this line
module.exports = repeatString;

View File

@ -1,22 +1,22 @@
const repeatString = require('./repeatString')
const repeatString = require("./repeatString");
describe('repeatString', () => {
test('repeats the string', () => {
expect(repeatString('hey', 3)).toEqual('heyheyhey');
describe("repeatString", () => {
test("repeats the string", () => {
expect(repeatString("hey", 3)).toEqual("heyheyhey");
});
test.skip('repeats the string many times', () => {
expect(repeatString('hey', 10)).toEqual('heyheyheyheyheyheyheyheyheyhey');
test("repeats the string many times", () => {
expect(repeatString("hey", 10)).toEqual("heyheyheyheyheyheyheyheyheyhey");
});
test.skip('repeats the string 1 times', () => {
expect(repeatString('hey', 1)).toEqual('hey');
test("repeats the string 1 times", () => {
expect(repeatString("hey", 1)).toEqual("hey");
});
test.skip('repeats the string 0 times', () => {
expect(repeatString('hey', 0)).toEqual('');
test("repeats the string 0 times", () => {
expect(repeatString("hey", 0)).toEqual("");
});
test.skip('returns ERROR with negative numbers', () => {
expect(repeatString('hey', -1)).toEqual('ERROR');
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
@ -25,13 +25,15 @@ describe('repeatString', () => {
// 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.
// 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
in the result, which if your function works correctly will equal the number that
was randomly generated. */
expect(repeatString('hey', number).match(/((hey))/g).length).toEqual(number);
expect(repeatString("hey", number).match(/((hey))/g).length).toEqual(
number
);
});
test.skip('works with blank strings', () => {
expect(repeatString('', 10)).toEqual('');
test("works with blank strings", () => {
expect(repeatString("", 10)).toEqual("");
});
});

View File

@ -1,5 +1,6 @@
const reverseString = function() {
const reverseString = function (word) {
const string = word.split("").reverse().join("");
return string;
};
// Do not edit below this line

View File

@ -1,18 +1,18 @@
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!')).toEqual('!cba !321')
})
test.skip('works with blank strings', () => {
expect(reverseString('')).toEqual('')
})
test("works with numbers and punctuation", () => {
expect(reverseString("123! abc!")).toEqual("!cba !321");
});
test("works with blank strings", () => {
expect(reverseString("")).toEqual("");
});
});

View File

@ -1,5 +1,11 @@
const removeFromArray = function() {
const removeFromArray = function (array, ...members) {
let result = array;
members.map((member) => {
result = result.filter((i) => {
return member !== i;
});
});
return result;
};
// Do not edit below this line

View File

@ -1,25 +1,25 @@
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('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]);
});
});

View File

@ -1,5 +1,21 @@
const sumAll = function() {
const sumAll = function (num1, num2) {
let result = 0;
// if (num1.typeof !== "number" || num2.typeof !== "number") return "ERROR";
if (!Number.isInteger(num1) || !Number.isInteger(num2)) return "ERROR";
if (num1 < 0 || num2 < 0) {
result = "ERROR";
} else if (num1 > num2) {
for (let i = num2; i <= num1; i++) {
result += i;
}
} else if (num1 < num2) {
for (let i = num1; i <= num2; i++) {
result += i;
}
}
return result;
};
// Do not edit below this line

View File

@ -1,22 +1,22 @@
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-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");
});
});

View File

@ -1,5 +1,12 @@
const leapYears = function() {
const leapYears = function (year) {
if (year % 4 === 0 || year % 400 === 0) {
if (year % 100 === 0 && year % 400 !== 0) {
return false;
}
return true;
} else {
return false;
}
};
// Do not edit below this line

View File

@ -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);
});
});

View File

@ -1,11 +1,17 @@
const convertToCelsius = function() {
const convertToCelsius = function (fahrenheit) {
let result = (fahrenheit - 32) * (5 / 9);
result = Math.round(result * 10) / 10;
return result;
};
const convertToFahrenheit = function() {
const convertToFahrenheit = function (celsius) {
let result = celsius * (9 / 5) + 32;
result = Math.round(result * 10) / 10;
return result;
};
// Do not edit below this line
module.exports = {
convertToCelsius,
convertToFahrenheit
convertToFahrenheit,
};

View File

@ -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("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);
});
});

View File

@ -1,25 +1,39 @@
const add = function() {
const add = function (num1, num2) {
return num1 + num2;
};
const subtract = function() {
const subtract = function (num1, num2) {
return num1 - num2;
};
const sum = function() {
const sum = function (array) {
let result = 0;
array.map((i) => {
result += i;
});
return result;
};
const multiply = function() {
const multiply = function (array) {
let result = 0;
array.reduce((acc, val) => {
return (result = acc * val);
});
return result;
};
const power = function() {
const power = function (num1, num2) {
return Math.pow(num1, num2);
};
const factorial = function() {
const factorial = function (number) {
if (number === 0) {
return 1;
} else if (number === 1) {
return 1;
} else {
return number * factorial(number - 1);
}
};
// Do not edit below this line
@ -29,5 +43,5 @@ module.exports = {
sum,
multiply,
power,
factorial
factorial,
};

View File

@ -1,77 +1,77 @@
const calculator = require('./calculator');
const calculator = require("./calculator");
describe('add', () => {
test('adds 0 and 0', () => {
describe("add", () => {
test("adds 0 and 0", () => {
expect(calculator.add(0, 0)).toBe(0);
});
test.skip('adds 2 and 2', () => {
test("adds 2 and 2", () => {
expect(calculator.add(2, 2)).toBe(4);
});
test.skip('adds positive numbers', () => {
test("adds positive numbers", () => {
expect(calculator.add(2, 6)).toBe(8);
});
});
describe('subtract', () => {
test.skip('subtracts numbers', () => {
describe("subtract", () => {
test("subtracts numbers", () => {
expect(calculator.subtract(10, 4)).toBe(6);
});
});
describe('sum', () => {
test.skip('computes the sum of an empty array', () => {
describe("sum", () => {
test("computes the sum of an empty array", () => {
expect(calculator.sum([])).toBe(0);
});
test.skip('computes the sum of an array of one number', () => {
test("computes the sum of an array of one number", () => {
expect(calculator.sum([7])).toBe(7);
});
test.skip('computes the sum of an array of two numbers', () => {
test("computes the sum of an array of two numbers", () => {
expect(calculator.sum([7, 11])).toBe(18);
});
test.skip('computes the sum of an array of many numbers', () => {
test("computes the sum of an array of many numbers", () => {
expect(calculator.sum([1, 3, 5, 7, 9])).toBe(25);
});
});
describe('multiply', () => {
test.skip('multiplies two numbers', () => {
describe("multiply", () => {
test("multiplies two numbers", () => {
expect(calculator.multiply([2, 4])).toBe(8);
});
test.skip('multiplies several numbers', () => {
test("multiplies several numbers", () => {
expect(calculator.multiply([2, 4, 6, 8, 10, 12, 14])).toBe(645120);
});
});
describe('power', () => {
test.skip('raises one number to the power of another number', () => {
describe("power", () => {
test("raises one number to the power of another number", () => {
expect(calculator.power(4, 3)).toBe(64); // 4 to third power is 64
});
});
describe('factorial', () => {
test.skip('computes the factorial of 0', () => {
describe("factorial", () => {
test("computes the factorial of 0", () => {
expect(calculator.factorial(0)).toBe(1); // 0! = 1
});
test.skip('computes the factorial of 1', () => {
test("computes the factorial of 1", () => {
expect(calculator.factorial(1)).toBe(1);
});
test.skip('computes the factorial of 2', () => {
test("computes the factorial of 2", () => {
expect(calculator.factorial(2)).toBe(2);
});
test.skip('computes the factorial of 5', () => {
test("computes the factorial of 5", () => {
expect(calculator.factorial(5)).toBe(120);
});
test.skip('computes the factorial of 10', () => {
test("computes the factorial of 10", () => {
expect(calculator.factorial(10)).toBe(3628800);
});
});

View File

@ -1,5 +1,24 @@
const palindromes = function () {
const palindromes = function (string) {
let result = string
.toUpperCase()
.replace(/[_\W]+/g, "")
.split(" ")
.join("")
.split("")
.join("");
let reverseResult = string
.toUpperCase()
.replace(/[_\W]+/g, "")
.split(" ")
.join("")
.split("")
.reverse()
.join("");
if (result === reverseResult) {
return true;
} else {
return false;
}
};
// Do not edit below this line

View File

@ -1,28 +1,30 @@
const palindromes = require('./palindromes')
const palindromes = require("./palindromes");
describe('palindromes', () => {
test('works with single words', () => {
expect(palindromes('racecar')).toBe(true);
describe("palindromes", () => {
test("works with single words", () => {
expect(palindromes("racecar")).toBe(true);
});
test.skip('works with punctuation ', () => {
expect(palindromes('racecar!')).toBe(true);
test("works with punctuation ", () => {
expect(palindromes("racecar!")).toBe(true);
});
test.skip('works with upper-case letters ', () => {
expect(palindromes('Racecar!')).toBe(true);
test("works with upper-case letters ", () => {
expect(palindromes("Racecar!")).toBe(true);
});
test.skip('works with multiple words', () => {
expect(palindromes('A car, a man, a maraca.')).toBe(true);
test("works with multiple words", () => {
expect(palindromes("A car, a man, a maraca.")).toBe(true);
});
test.skip('works with multiple words', () => {
expect(palindromes('Animal loots foliated detail of stool lamina.')).toBe(true);
test("works with multiple words", () => {
expect(palindromes("Animal loots foliated detail of stool lamina.")).toBe(
true
);
});
test.skip('doesn\'t just always return true', () => {
expect(palindromes('ZZZZ car, a man, a maracaz.')).toBe(false);
test("doesn't just always return true", () => {
expect(palindromes("ZZZZ car, a man, a maracaz.")).toBe(false);
});
test.skip('works with numbers in a string', () => {
expect(palindromes('rac3e3car')).toBe(true);
test("works with numbers in a string", () => {
expect(palindromes("rac3e3car")).toBe(true);
});
test.skip('works with unevenly spaced numbers in a string', () => {
expect(palindromes('r3ace3car')).toBe(false);
test("works with unevenly spaced numbers in a string", () => {
expect(palindromes("r3ace3car")).toBe(false);
});
});

View File

@ -1,5 +1,9 @@
const getTheTitles = function() {
const getTheTitles = function (array) {
let result = [];
array.map((obj) => {
result.push(obj.title);
});
return result;
};
// Do not edit below this line

View File

@ -1,18 +1,18 @@
const getTheTitles = require('./getTheTitles')
const getTheTitles = require("./getTheTitles");
describe('getTheTitles', () => {
const books = [
{
title: 'Book',
author: 'Name'
},
{
title: 'Book2',
author: 'Name2'
}
]
describe("getTheTitles", () => {
const books = [
{
title: "Book",
author: "Name",
},
{
title: "Book2",
author: "Name2",
},
];
test('gets titles', () => {
expect(getTheTitles(books)).toEqual(['Book','Book2']);
test("gets titles", () => {
expect(getTheTitles(books)).toEqual(["Book", "Book2"]);
});
});

View File

@ -1,5 +1,16 @@
const findTheOldest = function() {
const findTheOldest = function (array) {
return array.reduce((prev, curr) => {
const prevAge = getAge(prev.yearOfBirth, prev.yearOfDeath);
const currentAge = getAge(curr.yearOfBirth, curr.yearOfDeath);
return prevAge < currentAge ? curr : prev;
});
};
const getAge = function (birth, death) {
if (!death) {
death = new Date().getFullYear();
}
return death - birth;
};
// Do not edit below this line

View File

@ -1,7 +1,7 @@
const findTheOldest = require('./findTheOldest')
const findTheOldest = require("./findTheOldest");
describe('findTheOldest', () => {
test('finds the person with the greatest age!', () => {
describe("findTheOldest", () => {
test("finds the person with the greatest age!", () => {
const people = [
{
name: "Carly",
@ -18,10 +18,10 @@ describe('findTheOldest', () => {
yearOfBirth: 1912,
yearOfDeath: 1941,
},
]
expect(findTheOldest(people).name).toBe('Ray');
];
expect(findTheOldest(people).name).toBe("Ray");
});
test.skip('finds the person with the greatest age if someone is still living', () => {
test("finds the person with the greatest age if someone is still living", () => {
const people = [
{
name: "Carly",
@ -37,10 +37,10 @@ describe('findTheOldest', () => {
yearOfBirth: 1912,
yearOfDeath: 1941,
},
]
expect(findTheOldest(people).name).toBe('Ray');
];
expect(findTheOldest(people).name).toBe("Ray");
});
test.skip('finds the person with the greatest age if the OLDEST is still living', () => {
test("finds the person with the greatest age if the OLDEST is still living", () => {
const people = [
{
name: "Carly",
@ -56,7 +56,7 @@ describe('findTheOldest', () => {
yearOfBirth: 1912,
yearOfDeath: 1941,
},
]
expect(findTheOldest(people).name).toBe('Carly');
];
expect(findTheOldest(people).name).toBe("Carly");
});
});