Skip to content

London | ITP-May-25 | Houssam Lahlah | Sprint 3 | Reading List #750

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 10 commits 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
2 changes: 1 addition & 1 deletion Sprint-2/debug/address.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ const address = {
postcode: "XYZ 123",
};

console.log(`My house number is ${address[0]}`);
console.log(`My house number is ${address[0]}`);
2 changes: 1 addition & 1 deletion Sprint-2/debug/recipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ const recipe = {

console.log(`${recipe.title} serves ${recipe.serves}
ingredients:
${recipe}`);
${recipe}`);
24 changes: 24 additions & 0 deletions Sprint-3/reading-list/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,27 @@ const books = [
},
];

function readingList(books) {
const list = document.getElementById("reading-list");

books.forEach((book) => {
const li = document.createElement("li");
li.style.backgroundColor = book.alreadyRead ? "green" : "red";

const title = document.createElement("p");
title.textContent = book.title;

const author = document.createElement("p");
author.textContent = book.author;

const image = document.createElement("img");
image.src = book.bookCoverImage;

li.appendChild(title);
li.appendChild(author);
li.appendChild(image);
list.appendChild(li);
});
}

readingList(books);
9 changes: 9 additions & 0 deletions prep/mean.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function calculateMean(list) {
const sum = list.reduce((acc, num) => acc + num, 0);
return sum / list.length;
}

module.exports = calculateMean;


// module.exports = { calculateMean };
9 changes: 9 additions & 0 deletions prep/mean.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const calculateMean = require("./mean");

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
});
29 changes: 29 additions & 0 deletions prep/median.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
// prep/median.js
function calculateMedian(list) {
const middleIndex = Math.floor(list.length / 2);
const median = list.splice(middleIndex, 1)[0];

return median;
}
module.exports = calculateMedian;
*/

// We clone the array with [...numbers]
// before sorting to avoid mutating the original input.


function calculateMedian(numbers) {
const sorted = [...numbers].sort((a, b) => a - b);
const mid = Math.floor(sorted.length / 2);

if (sorted.length % 2 === 0) {
return (sorted[mid - 1] + sorted[mid]) / 2;
} else {
return sorted[mid];
}
}

module.exports = calculateMedian;


46 changes: 46 additions & 0 deletions prep/median.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// const { calculateMedian } = require("./median");
const calculateMedian = require("./median");


const calculateMean = require("./mean");
// const { calculateMean } = require("./mean");

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

test("calculates the median of an odd-length array", () => {
const list = [3, 50, 7];
const currentOutput = calculateMedian(list); // sorted = [3, 7, 50], median = 7
const expectedOutput = 7;

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

test("calculates the median of a list of odd length", () => {
const list = [10, 20, 30, 40];
const currentOutput = calculateMedian(list);
const targetOutput = 25;

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

// Both functions access the same array
// because JavaScript passes objects and
// arrays by reference — they all point to the same memory location.

const salaries = [10, 20, 30, 40, 60, 80, 80];
const median = calculateMedian(salaries);
const mean = calculateMean(salaries);
console.log(salaries, "<--- salaries input before we call calculateMean");
console.log(`The median salary is ${median}`);
console.log(`The mean salary is ${mean}`);





Loading