Skip to content
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
18 changes: 15 additions & 3 deletions Sprint-1/fix/median.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,21 @@
// 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;
// Filter out non-numeric values and sort the remaining numbers
const numbers = list
.filter((x) => typeof x === "number" && !isNaN(x))
.sort((a, b) => a - b);

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

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

module.exports = calculateMedian;
6 changes: 3 additions & 3 deletions Sprint-3/quote-generator/index.html
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<!DOCTYPE html>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Title here</title>
<title>Quote Generator</title>
<script defer src="quotes.js"></script>
</head>
<body>
<h1>hello there</h1>
<h1>Quote Generator</h1>
<p id="quote"></p>
<p id="author"></p>
<button type="button" id="new-quote">New quote</button>
Expand Down
13 changes: 13 additions & 0 deletions Sprint-3/quote-generator/quotes.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
const quoteP = document.querySelector("#quote");
const authorP = document.querySelector("#author");
const button = document.querySelector("#new-quote");

function displayQuote() {
const randomQuote = pickFromArray(quotes);
quoteP.innerText = randomQuote.quote;
authorP.innerText = randomQuote.author;
}

button.addEventListener("click", displayQuote);

// DO NOT EDIT BELOW HERE

// pickFromArray is a function which will return one item, at
Expand Down Expand Up @@ -491,3 +503,4 @@ const quotes = [
];

// call pickFromArray with the quotes array to check you get a random quote
displayQuote();