Skip to content
Closed
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
28 changes: 25 additions & 3 deletions Sprint-1/fix/median.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,32 @@
// Hint: Please consider scenarios when 'list' doesn't have numbers (the function is expected to return null)
// or 'list' has mixed values (the function is expected to sort only numbers).

//In order to fix this we need to ensure
// (1) only an array is passed
// (2) we filter only valid numbers within the passed array and
// (3) return null if there are no valid numbers.
// filter

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 nums = list.filter(x => typeof x === 'number' && !isNaN(x));
if (nums.length === 0) return null;

const sorted = [...nums].sort((a, b) => a - b);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you expanding an array into an array here? Can you think of a way to simplify this?

const middleIndex = Math.floor(sorted.length / 2);
let median;
if (sorted.length % 2 !== 0) {
median = sorted.slice(middleIndex, middleIndex + 1)[0];

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think about when you want to use slicing to access arrays, and what you are trying to do here. Is there a way to get a value from an array that might be better suited than slicing for this line of code?

return median;
}
else { const mid = sorted.slice(middleIndex - 1, middleIndex + 1); //you are declaring and assigning at the same time
median = (mid[0] + mid [1])/2;
return median;
}
}

module.exports = calculateMedian;

/* const middleIndex = Math.floor(list.length / 2);
const median = list.splice(middleIndex, 1)[0];
return median; */
21 changes: 20 additions & 1 deletion Sprint-1/implement/dedupe.js
Original file line number Diff line number Diff line change
@@ -1 +1,20 @@
function dedupe() {}
function dedupe(elements) {
// returns a new empty array as just returning elements
if (elements.length === 0) return [];

// We create an empty array to repopulate
const result = [];

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

// the loop takes each item and adds it
// as long as it is not in the destination array "results"
if (!result.includes(item)) {
result.push(item);
}
}
return result;
}

module.exports = dedupe;
14 changes: 13 additions & 1 deletion Sprint-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,24 @@ 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", () => {
expect(dedupe([])).toEqual([]);
});

// 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 a copy of the original array", () => {
const input = [1, 2, 3, 7, 9];
const output = dedupe(input);
expect(output).toEqual([1, 2, 3, 7, 9]);
expect(output).not.toBe(input); // ensures it's a new array
});

// 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
test("given array with strings or numbers, it should remove the duplicate values, preserving the first occurrence of each element", () => {
expect(dedupe([1, 2, 2, 3, 4, 5, 5, 5, 7, 9, 9, "a", "a", "b", "c", "d", "d", "e"]))
.toEqual([1, 2, 3, 4, 5, 7, 9, "a", "b", "c", "d", "e"]);
});
25 changes: 25 additions & 0 deletions Sprint-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,29 @@
function findMax(elements) {
if (!Array.isArray(elements)) return null;
const nums = elements.filter(x => typeof x === 'number' && !isNaN(x));
if (nums.length === 0) return -Infinity;

const positives = nums.some(n => n > 0);
const negatives = nums.some(n => n < 0);

if (positives && negatives) {
// this helps us check and considers all values both sides of zer0
let maxAbs = nums[0];
for (let i = 1; i < nums.length; i++) {
if (Math.abs(nums[i]) > Math.abs(maxAbs)) {
maxAbs = nums[i];
}
}
return maxAbs;
} else {
return Math.max(...nums);
}

/* if (nums.length === 1) return elements[0];

const arranged = [...nums].sort((a, b) => a - b);
const highest = arranged[arranged.length - 1];
return highest; */
}

module.exports = findMax;
11 changes: 10 additions & 1 deletion Sprint-1/implement/max.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,41 @@ We have set things up already so that this file can see your function from the o

const findMax = require("./max.js");

//Given an array with regular integers, when passed to the max function it should return the highest
test("an array with regular integers, returns the highest number",() => {expect(findMax([30, 50, 10, 40])).toEqual(50)});

// Given an empty array
// 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",() => {expect(findMax([])).toEqual(-Infinity)});

// Given an array with one number
// When passed to the max function
// Then it should return that number
test("single element array, returns the only item",() => {expect(findMax([23])).toEqual(23)});

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you may have misunderstood what the program specification is asking for here. Try to think about what your understanding is generally - does your code behave consistently when given mixed numbers and only negative numbers?

test("an array with positive and negative numbers, returns the largest",() => {expect(findMax([-5, 15, -20, 3])).toEqual(-20)});

// Given an array with just negative numbers
// When passed to the max function
// Then it should return the closest one to zero
test("an array with just negative numbers, closest to zero",() => {expect(findMax([-9, -3, -20, -1])).toEqual(-1)});

// Given an array with decimal numbers
// When passed to the max function
// Then it should return the largest decimal number
test("an array with decimal numbers, returns the largest decimal number",() => {expect(findMax([1.1, 3.5, -4.9, 2.2])).toEqual(-4.9)});

// 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("an array mixed with non-numbers, returns max ignoring the non-numbers",() => {expect(findMax(["hello", null, 42, "99", undefined, -100])).toEqual(-100)});

// 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("an array with only non-numbers, returns least surprising value",() => {expect(findMax(["fan", null, undefined, {}, ""])).toEqual(-Infinity)});
12 changes: 12 additions & 0 deletions Sprint-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
function sum(elements) {
if (elements.length === 0) return 0;
let total = 0
let num = false;

for (let i = 0; i < elements.length; i++) {
const item = elements[i];
if (typeof item === "number" && !isNaN(item)) { //this excludes non numbers before totalling
total += item;
num = true;
}
}
return num? total: null; //if num true i.e actual numbers exist, return total otherwise return null
}

module.exports = sum;
8 changes: 7 additions & 1 deletion Sprint-1/implement/sum.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,34 @@ E.g. sum(['hey', 10, 'hi', 60, 10]), target output: 80 (ignore any non-numerical
const sum = require("./sum.js");

// Acceptance Criteria:
test("given an array, returns the total of numbers",() => {expect(sum([10, 20, 30])).toEqual(60)});

// 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",() => {expect(sum([])).toEqual(0)});

// Given an array with just one number
// When passed to the sum function
// Then it should return that number
test("given an array with single number, returns the number",() => {expect(sum([72])).toEqual(72)});

// Given an array containing negative numbers
// When passed to the sum function
// Then it should still return the correct total sum
test("an array with negative numbers, returns the total",() => {expect(sum([-1,-27,-13,-59])).toEqual(-100)});

// Given an array with decimal/float numbers
// When passed to the sum function
// Then it should return the correct total sum
test("an array with decimal/float numbers, returns the correct sum",() => {expect(sum([1.1, 3.5, -4.9, 2.2])).toBeCloseTo(1.9,5)});

// 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("an array mixed with non-numbers, returns sum ignoring the non-numbers",() => {expect(sum(["hello", null, 17, "99", 46, -53])).toEqual(10)});

// 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("an array with only non-numbers, returns least surprising value",() => {expect(sum(["fan", null, undefined, {}, ""])).toEqual(null)});
14 changes: 14 additions & 0 deletions Sprint-1/prep/arrays-workshop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Can you fix this code?
function doubleAllNumbers(numbers) {
let doubledNumbers = [];

for (let n of numbers) {
doubledNumbers.push(n * 2);
}

return doubledNumbers;
}

const myNums = [10, 20, 30];
doubleAllNumbers(myNums);
console.log(myNums);
12 changes: 12 additions & 0 deletions Sprint-1/prep/checkingport.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const express = require('express');
const app = express(); // ✅ This creates the app

const PORT = process.env.PORT || 3000;

app.get('/', (req, res) => {
res.send('Hello, world!');
});

app.listen(PORT, () => {
console.log(`🚀 Server running at http://localhost:${PORT}`);
});
29 changes: 29 additions & 0 deletions Sprint-1/prep/iteration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
function calculateMean(list) {
let total = 0;

for (const item of list) {
total += item;
}
const mean = total/list.length
return mean;
}

//console.log(calculateMean([10,20,30,40,50]))


function calculateMedian(list) {
const middleIndex = Math.floor(list.length / 2);
const median = list.splice(middleIndex, 1)[0];

return median;
}


const salaries = [10, 20, 30, 40, 60, 80, 80];

const sal_mean = calculateMean(salaries)
const sal_med = calculateMedian(salaries)

console.log(sal_mean)
console.log(sal_med)
console.log(salaries)
Empty file added Sprint-1/prep/mean.js
Empty file.
7 changes: 7 additions & 0 deletions Sprint-1/prep/mean.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
test("calculates the mean of a list of numbers", () => {
const list = [3, 50, 7];
const currentOutput = calculateMean(list);
const targetOutput = 20;

expect(currentOutput).toEqual(targetOutput); // 20 is (3 + 50 + 7) / 3
});
Empty file added Sprint-1/prep/median.js
Empty file.
7 changes: 7 additions & 0 deletions Sprint-1/prep/median.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
test("calculates the median of a list of odd length", () => {
const list = [10, 20, 30, 50, 60];
const currentOutput = calculateMedian(list);
const targetOutput = 30;

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