diff --git a/kelcy-hunter/01/exercise.test.js b/kelcy-hunter/01/exercise.test.js new file mode 100644 index 0000000..caa1b76 --- /dev/null +++ b/kelcy-hunter/01/exercise.test.js @@ -0,0 +1,21 @@ +describe('Exercise 01', () => { + test('01. Check if a positive number is a multiple of 5 or 9', () => { + /** + * You are expected to return a boolean value + */ + + function multipleOf5Or9(positiveNumber) { + // Your code here + //this.positiveNumber = Number(positiveNumber); + return true ? positiveNumber % 5==0 || positiveNumber % 9==0 : false; + } + + // Test cases + expect(multipleOf5Or9(10)).toBe(true); + expect(multipleOf5Or9(20)).toBe(true); + expect(multipleOf5Or9(21)).toBe(false); + expect(multipleOf5Or9(36)).toBe(true); + expect(multipleOf5Or9(22)).toBe(false); + expect(multipleOf5Or9(23)).toBe(false); + }); +}); \ No newline at end of file diff --git a/kelcy-hunter/02/exercise.test.js b/kelcy-hunter/02/exercise.test.js new file mode 100644 index 0000000..b548422 --- /dev/null +++ b/kelcy-hunter/02/exercise.test.js @@ -0,0 +1,24 @@ +describe('02', () => { + it('02. Create a new string from any string by taking the last 3 letters of that string and adding it to the front and back.', () => { + /** + * If the length of the string is less than 3, return false. + * + * Remember we discussed splice() method in the previous exercise. + */ + + function lastThree(str){ + // Your code here + if (str.length < 3){ + return false; + }else{ + let lastThree = str.slice(-3, str.length); + return `${lastThree}${str}${lastThree}` + } + } + + // Test cases + expect(lastThree('hello')).toBe('llohellollo'); + expect(lastThree('wor')).toBe('worworwor'); + expect(lastThree('qw')).toBe(false); + }); +}); \ No newline at end of file diff --git a/kelcy-hunter/03/exercise.test.js b/kelcy-hunter/03/exercise.test.js new file mode 100644 index 0000000..7486ebd --- /dev/null +++ b/kelcy-hunter/03/exercise.test.js @@ -0,0 +1,19 @@ +describe('03', () => { + it('03. Check if any of two number is 6, if the sum of them makes 6 or if the difference is 6', () => { + + function isSix(x, y) { + // Your code here + return true ? x==6 || y==6 || x+ y==6 || y-x ==6 || x-y==6 : false; + + } + + // Test cases + expect(isSix(6, 0)).toBe(true); + expect(isSix(0, 6)).toBe(true); + expect(isSix(3, 3)).toBe(true); + expect(isSix(3, 4)).toBe(false); + expect(isSix(3, 9)).toBe(true); + expect(isSix(3, 3)).toBe(true); + expect(isSix(3, 3)).toBe(true); + }); +}); \ No newline at end of file diff --git a/kelcy-hunter/04/exercise.test.js b/kelcy-hunter/04/exercise.test.js new file mode 100644 index 0000000..2e46d42 --- /dev/null +++ b/kelcy-hunter/04/exercise.test.js @@ -0,0 +1,18 @@ +describe('04', () => { + test('Check if an array of 3 items, contains a 5 or 2', () => { + + const arrayIncludes5Or2 = (array) => { + // Your code here + return true ? array.includes(5) || array.includes(2) : false; + } + + // Test cases + expect(arrayIncludes5Or2([1, 2, 3])).toBe(true); + expect(arrayIncludes5Or2([1, 4, 3])).toBe(false); + expect(arrayIncludes5Or2([1, 4, 5])).toBe(true); + expect(arrayIncludes5Or2([1, 4, 6])).toBe(false); + expect(arrayIncludes5Or2([1, 4, 7])).toBe(false); + expect(arrayIncludes5Or2([1, 4, 8])).toBe(false); + expect(arrayIncludes5Or2([1, 4, 9])).toBe(false); + }); +}); \ No newline at end of file diff --git a/kelcy-hunter/05/exercise.test.js b/kelcy-hunter/05/exercise.test.js new file mode 100644 index 0000000..39cdbec --- /dev/null +++ b/kelcy-hunter/05/exercise.test.js @@ -0,0 +1,22 @@ +describe('05', () => { + it('Take an array of any length and add all elements to return a sum', () => { + + + const sum = (array) => { + // Your code here + let sum = 0; + for (const i of array){ + sum += i; + } + return sum; + } + + // Test cases + expect(sum([1, 2, 3])).toBe(6); + expect(sum([1, 4, 3, 50, 10])).toBe(68); + expect(sum([1, 4, 5, 6, 7, 8, 9, 10])).toBe(50); + expect(sum([1, 4, 6, 7, 8, 9, 10])).toBe(45); + expect(sum([1, 4, 7, 8, 9, 10])).toBe(39); + expect(sum([1, 4, 8, 9, 10])).toBe(32); + }); +}) \ No newline at end of file diff --git a/kelcy-hunter/06/exercise.test.js b/kelcy-hunter/06/exercise.test.js new file mode 100644 index 0000000..7ef6b30 --- /dev/null +++ b/kelcy-hunter/06/exercise.test.js @@ -0,0 +1,45 @@ +describe('06', () => { + + // Tip: Use setTimeout to delay the execution of marking + + const listOfPapers = [ + { + subject: "Maths", + wasSubmitted: true, + markPaper: () => { + // add a promise here that resolves after 2 seconds + // and print "Maths paper marked" + } + }, + { + subject: "Geology", + wasSubmitted: true, + markPaper: () => { + // add a promise here that resolves after 2 seconds + // and print "Geology paper marked" + } + }, + { + subject: "Social Studies", + wasSubmitted: false, + markPaper: () => { + // add a promise here that resolves after 2 seconds + // and print "Social Studies paper marked" + } + }, + ] + + it('Check if a paper was submitted, and if yes, wait for it to be marked', async () => { + const spyOnLog = vi.spyOn(console, 'log'); + + // Your code here + + + expect(spyOnLog).toHaveBeenCalledWith("Maths paper marked"); + expect(spyOnLog).toHaveBeenCalledWith("Geology paper marked"); + expect(spyOnLog).not.toHaveBeenCalledWith("Social Studies paper marked"); + expect(listOfPapers[0].markPaper()).toBeInstanceOf(Promise); + expect(listOfPapers[1].markPaper()).toBeInstanceOf(Promise); + expect(listOfPapers[2].markPaper()).toBeInstanceOf(Promise); + }); +}); \ No newline at end of file diff --git a/kelcy-hunter/07/exercise.test.js b/kelcy-hunter/07/exercise.test.js new file mode 100644 index 0000000..3053caa --- /dev/null +++ b/kelcy-hunter/07/exercise.test.js @@ -0,0 +1,32 @@ +describe('07', () => { + test('Using the "forEach" array method, print the current element', () => { + const spyOnLog = vi.spyOn(console, 'log'); + /** + * You are expected to use the forEach array method + * + * You are expected to use the console.log() method + * + * You are expected to use the template literal syntax + * Example: `The current element is ....` + */ + const array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; + + // Your code here + function printElement(element){ + console.log(`The current element is ${element}`) + } + array.forEach(printElement); + + // Test cases + expect(spyOnLog).toHaveBeenCalledWith('The current element is 1'); + expect(spyOnLog).toHaveBeenCalledWith('The current element is 2'); + expect(spyOnLog).toHaveBeenCalledWith('The current element is 3'); + expect(spyOnLog).toHaveBeenCalledWith('The current element is 4'); + expect(spyOnLog).toHaveBeenCalledWith('The current element is 5'); + expect(spyOnLog).toHaveBeenCalledWith('The current element is 6'); + expect(spyOnLog).toHaveBeenCalledWith('The current element is 7'); + expect(spyOnLog).toHaveBeenCalledWith('The current element is 8'); + expect(spyOnLog).toHaveBeenCalledWith('The current element is 9'); + expect(spyOnLog).toHaveBeenCalledWith('The current element is 10'); + }); +}); \ No newline at end of file diff --git a/kelcy-hunter/08/exercise.test.js b/kelcy-hunter/08/exercise.test.js new file mode 100644 index 0000000..e357e59 --- /dev/null +++ b/kelcy-hunter/08/exercise.test.js @@ -0,0 +1,79 @@ +describe('08', () => { + /** + * You are the manager of a new Vehicle company + * + * And you need to create different kind of cars to list in your inventory. + * + * 1. Create a Vehicle parent + * - wheels as a property + * - bodyType as a property + * + * 2. Create different kind of vehicles from the parent + * - each vehicle should have a color + * - each vehicle should have a method called "whatIsMyName" that return the bodyType of the vehicle + * - Example: "I am a sedan" + */ + + test('Create a Vehicle class, and create different type of vehicles that extends', () => { + // Your code here + + class Vehicle{ + constructor(wheels, bodyType){ + this.wheels = wheels; + this.bodyType = bodyType; + } + } + + class Car extends Vehicle{ + constructor(color, wheels, bodyType){ + super(wheels,bodyType); + this.color = color; + } + whatIsMyName(){ + return `I am a ${this.bodyType}` + } + } + + class Truck extends Vehicle{ + constructor(color, wheels, bodyType){ + super(wheels,bodyType); + this.color = color; + } + whatIsMyName(){ + return `I am a ${this.bodyType}` + } + } + + class Motorcycle extends Vehicle{ + constructor(color, wheels, bodyType){ + super(wheels,bodyType); + this.color = color; + } + whatIsMyName(){ + return `I am a ${this.bodyType}` + } + } + + // Expected outcome for the child classes + const car = new Car('red', 4, 'sedan'); + const truck = new Truck('blue', 6, 'pickup'); + const motorcycle = new Motorcycle('green', 2, 'motorcycle'); + + + // Test cases + expect(car.wheels).toBe(4); + expect(car.bodyType).toBe('sedan'); + expect(car.color).toBe('red'); + expect(car.whatIsMyName()).toBe('I am a sedan'); + + expect(truck.wheels).toBe(6); + expect(truck.bodyType).toBe('pickup'); + expect(truck.color).toBe('blue'); + expect(truck.whatIsMyName()).toBe('I am a pickup'); + + expect(motorcycle.wheels).toBe(2); + expect(motorcycle.bodyType).toBe('motorcycle'); + expect(motorcycle.color).toBe('green'); + expect(motorcycle.whatIsMyName()).toBe('I am a motorcycle'); + }); +}); \ No newline at end of file