Skip to content
Closed
Show file tree
Hide file tree
Changes from 12 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
39 changes: 36 additions & 3 deletions Sprint-1/fix/median.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,42 @@
// or 'list' has mixed values (the function is expected to sort only numbers).

function calculateMedian(list) {
const middleIndex = Math.floor(list.length / 2);
const median = list.splice(middleIndex, 1)[0];
return median;
if (!Array.isArray(list)) {
return null;
}

const numbers = [];
for (let i = 0; i < list.length; i++) {
if (typeof list[i] === 'number') {
numbers.push(list[i]);
}
}

if (numbers.length === 0) {
return null;
}

for (let i = 0; i < numbers.length; i++) {
for (let j = i + 1; j < numbers.length; j++) {
if (numbers[i] > numbers[j]) {
const temp = numbers[i];
numbers[i] = numbers[j];
numbers[j] = temp;
}
}
}

const middleIndex = Math.floor(numbers.length / 2);


if (numbers.length % 2 === 0) {

return (numbers[middleIndex - 1] + numbers[middleIndex]) / 2;
} else {

return numbers[middleIndex];
}
}


module.exports = calculateMedian;
16 changes: 15 additions & 1 deletion Sprint-1/implement/dedupe.js
Original file line number Diff line number Diff line change
@@ -1 +1,15 @@
function dedupe() {}
function dedupe(elements) {
const unique = [];

for (let i = 0; i < elements.length; i++) {
const current = elements[i];

if (!unique.includes(current)) {
unique.push(current);
}
}

return unique;
}

module.exports = dedupe;
22 changes: 20 additions & 2 deletions Sprint-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,30 @@ E.g. dedupe([1, 2, 1]) target output: [1, 2]
// Given an empty array
// When passed to the dedupe function
// Then it should return an empty array
test.todo("given an empty array, it returns an empty array");

test("given an empty array, it returns an empty array", () => {
const currentOutput = dedupe([]);
const targetOutput = [];

expect(currentOutput).toEqual(targetOutput);
});
// Given an array with no duplicates
// When passed to the dedupe function
// Then it should return a copy of the original array

test("Given an array with no duplicates, it returns an the same array", () => {
const currentOutput = dedupe([1,4,6,"d","a","x","e",0,7,8]);
const targetOutput = [1,4,6,"d","a","x","e",0,7,8];

expect(currentOutput).toEqual(targetOutput);
});
// Given an array with strings or numbers
// When passed to the dedupe function
// Then it should remove the duplicate values, preserving the first occurence of each element
// Then it should remove the duplicate values, preserving the first occurrence of each element

test("Given an array with strings or numbers, it returns an the array whit no duplicated elements", () => {
const currentOutput = dedupe(['a','a','a','b','b','c']);
const targetOutput = ['a','b','c'];

expect(currentOutput).toEqual(targetOutput);
});
17 changes: 17 additions & 0 deletions Sprint-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
function findMax(elements) {
if (elements.length === 0) {
return -Infinity;
}

let max = -Infinity;

for (let i = 0; i < elements.length; i++) {
let current = elements[i];

if (typeof current === "number") {
if (current > max) {
max = current;
}
}
}

return max;
}

module.exports = findMax;
49 changes: 48 additions & 1 deletion Sprint-1/implement/max.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,75 @@ const findMax = require("./max.js");
// When passed to the max function
// Then it should return -Infinity
// Delete this test.todo and replace it with a test.
test.todo("given an empty array, returns -Infinity");

test("given an empty array, returns -Infinity", () => {
const currentOutput = findMax([]);
const targetOutput = -Infinity;

expect(currentOutput).toEqual(targetOutput);
});

// Given an array with one number
// When passed to the max function
// Then it should return that number

test("given an array with one number, returns that number", () => {
const currentOutput = findMax([42]);
const targetOutput = 42;

expect(currentOutput).toEqual(targetOutput);
});


// Given an array with both positive and negative numbers
// When passed to the max function
// Then it should return the largest number overall

test("given an array with positive and negative numbers, returns the largest", () => {
const currentOutput = findMax([-25, 5, 20, -3, 15]);
const targetOutput = 20;

expect(currentOutput).toEqual(targetOutput);
});
// Given an array with just negative numbers
// When passed to the max function
// Then it should return the closest one to zero

test("given an array of only negative numbers, returns the closest to zero", () => {
const currentOutput = findMax([-50, -3, -20, -10]);
const targetOutput = -3;

expect(currentOutput).toEqual(targetOutput);
});

// Given an array with decimal numbers
// When passed to the max function
// Then it should return the largest decimal number

test("given an array of decimal numbers, returns the largest decimal", () => {
const currentOutput = findMax([1.1, 3.5, 2.9, 3.4]);
const targetOutput = 3.5;

expect(currentOutput).toEqual(targetOutput);
});

// Given an array with non-number values
// When passed to the max function
// Then it should return the max and ignore non-numeric values

test("given an array with non-number values, returns correct max (ignoring non-numbers)", () => {
const currentOutput = findMax([10, "hi", 50, true, 3]);
const targetOutput = 50;

expect(currentOutput).toEqual(targetOutput);
});

// Given an array with only non-number values
// When passed to the max function
// Then it should return the least surprising value given how it behaves for all other inputs
test("given an array with only non-number values, returns -Infinity", () => {
const currentOutput = findMax(["a", null, undefined, {}, [], true]);
const targetOutput = -Infinity;

expect(currentOutput).toEqual(targetOutput);
});
15 changes: 15 additions & 0 deletions Sprint-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
function sum(elements) {
// This variable will store the total sum
let total = 0;

// Go through each element in the array
for (let i = 0; i < elements.length; i++) {
let current = elements[i]; // get the current element

// Only add it if it's a number
if (typeof current === "number" && !Number.isNaN(current)) {
total = total + current; // add to total
}
}

// Return the sum of all numbers
return total;
}

module.exports = sum;
42 changes: 41 additions & 1 deletion Sprint-1/implement/sum.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,64 @@ const sum = require("./sum.js");
// Given an empty array
// When passed to the sum function
// Then it should return 0
test.todo("given an empty array, returns 0")
test("given an empty array, returns 0",()=>{
const currentOutput = sum([]);
const targetPitPut= 0;

expect(currentOutput).toEqual(targetPitPut);
})


// Given an array with just one number
// When passed to the sum function
// Then it should return that number
test("given an array whit only 1 number, returns same input",()=>{
const currentOutput = sum([4]);
const targetPitPut= 4;

expect(currentOutput).toEqual(targetPitPut);
})


// Given an array containing negative numbers
// When passed to the sum function
// Then it should still return the correct total sum
test("given an array whit a negative number, returns correct total (subtract the negative number)",()=>{
const currentOutput = sum([1,2,3,4,-5,]);
const targetPitPut= 5;

expect(currentOutput).toEqual(targetPitPut);
})


// Given an array with decimal/float numbers
// When passed to the sum function
// Then it should return the correct total sum

test("given an array whit a decimal/float number, returns correct total (decimal/float number)",()=>{
const currentOutput = sum([1,2,3,4,3.5,]);
const targetPitPut= 13.5;

expect(currentOutput).toEqual(targetPitPut);
})


// Given an array containing non-number values
// When passed to the sum function
// Then it should ignore the non-numerical values and return the sum of the numerical elements
test("given an array whit a non-number values, returns correct total (ignore the NaN and sum the others)",()=>{
const currentOutput = sum([1,2,3,4,"hi",5]);
const targetPitPut= 15;

expect(currentOutput).toEqual(targetPitPut);
})

// Given an array with only non-number values
// When passed to the sum function
// Then it should return the least surprising value given how it behaves for all other inputs
test("given an array with only non-number values, returns the least surprising value (0)", () => {
const currentOutput = sum(["hello", null, undefined, {}, [], true, NaN]);
const targetOutput = 0;

expect(currentOutput).toEqual(targetOutput);
});
7 changes: 3 additions & 4 deletions Sprint-1/refactor/includes.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
// Refactor the implementation of includes to use a for...of loop

function includes(list, target) {
for (let index = 0; index < list.length; index++) {
const element = list[index];
for (const element of list) {
if (element === target) {
return true;
return true;
}
}
return false;
return false;
}

module.exports = includes;
8 changes: 6 additions & 2 deletions Sprint-2/debug/address.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
// Predict and explain first...
// Prediction:
// The code will print "My house number is undefined"
// because address[0] does not exist.
// Objects use keys like address.houseNumber, not numbers.

// This code should log out the houseNumber from the address object
// but it isn't working...
Expand All @@ -12,4 +15,5 @@ const address = {
postcode: "XYZ 123",
};

console.log(`My house number is ${address[0]}`);
// Use the correct key of the object
console.log(`My house number is ${address.houseNumber}`);
7 changes: 4 additions & 3 deletions Sprint-2/debug/author.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// Predict and explain first...

//The code will give an error because for...of does not work on objects.
// This program attempts to log out all the property values in the object.
// But it isn't working. Explain why first and then fix the problem

/*This is because for...of only works with arrays or strings.
An object like author is not an array, so the code will give an error. Nothing will be printed.*/
const author = {
firstName: "Zadie",
lastName: "Smith",
Expand All @@ -11,6 +12,6 @@ const author = {
alive: true,
};

for (const value of author) {
for (const value of Object.values(author)) {
console.log(value);
}
6 changes: 4 additions & 2 deletions Sprint-2/debug/recipe.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Predict and explain first...
/*The code will show [object Object] instead of the ingredients because it tries to print the whole object as a string.*/

// This program should log out the title, how many it serves and the ingredients.
// Each ingredient should be logged on a new line
Expand All @@ -11,5 +12,6 @@ const recipe = {
};

console.log(`${recipe.title} serves ${recipe.serves}
ingredients:
${recipe}`);
ingredients:
${recipe.ingredients.join("\n")}`);

13 changes: 12 additions & 1 deletion Sprint-2/implement/contains.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
function contains() {}
function contains() {function contains(obj, key) {
// First, check if obj is a real object
if (typeof obj !== "object" || obj === null || Array.isArray(obj)) {
return false;
}

// Use hasOwnProperty to check if the key exists
return obj.hasOwnProperty(key);
}

module.exports = contains;
}

module.exports = contains;
Loading
Loading