Skip to content

completed js and git exercises #23

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

Open
wants to merge 1 commit 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
21 changes: 21 additions & 0 deletions kelcy-hunter/01/exercise.test.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
24 changes: 24 additions & 0 deletions kelcy-hunter/02/exercise.test.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
19 changes: 19 additions & 0 deletions kelcy-hunter/03/exercise.test.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
18 changes: 18 additions & 0 deletions kelcy-hunter/04/exercise.test.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
22 changes: 22 additions & 0 deletions kelcy-hunter/05/exercise.test.js
Original file line number Diff line number Diff line change
@@ -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);
});
})
45 changes: 45 additions & 0 deletions kelcy-hunter/06/exercise.test.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
32 changes: 32 additions & 0 deletions kelcy-hunter/07/exercise.test.js
Original file line number Diff line number Diff line change
@@ -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');
});
});
79 changes: 79 additions & 0 deletions kelcy-hunter/08/exercise.test.js
Original file line number Diff line number Diff line change
@@ -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');
});
});