Skip to content
Closed
Show file tree
Hide file tree
Changes from 5 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
15 changes: 10 additions & 5 deletions Sprint-3/quote-generator/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Title here</title>
<title>Quote generator app</title>
<link rel="stylesheet" href="style.css" />
<script defer src="quotes.js"></script>
</head>
<body>
<h1>hello there</h1>
<p id="quote"></p>
<p id="author"></p>
<button type="button" id="new-quote">New quote</button>
<div class="main-container">
<div class="quote-box">
<p class="quote-mark">“</p>
<p id="quote"></p>
<p id="author"></p>
<button type="button" id="new-quote">New quote</button>
</div>
</div>
</body>
</html>
22 changes: 20 additions & 2 deletions Sprint-3/quote-generator/quotes.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
//
// Examples of use
// ---------------
// pickFromArray(['a','b','c','d']) // maybe returns 'c'
// pickFromArray(['a','b','c','d']) // maybe returns 'c'

// You don't need to change this function
function pickFromArray(choices) {
Expand Down Expand Up @@ -490,4 +490,22 @@ const quotes = [
},
];

// call pickFromArray with the quotes array to check you get a random quote
function displayRandomQuote() {
const randomQuote = pickFromArray(quotes);
const quoteElement = document.getElementById("quote");
const authorElement = document.getElementById("author");

if (!quoteElement || !authorElement) return;

quoteElement.innerText = randomQuote.quote;
authorElement.innerText = randomQuote.author;
}

window.addEventListener("load", () => {
displayRandomQuote();

const newQuoteButton = document.getElementById("new-quote");
if (newQuoteButton) {
newQuoteButton.addEventListener("click", displayRandomQuote);
}
});
70 changes: 69 additions & 1 deletion Sprint-3/quote-generator/style.css
Original file line number Diff line number Diff line change
@@ -1 +1,69 @@
/** Write your CSS in here **/
body {
margin: 0;
font-family: sans-serif;
}

.main-container {
background-color: #ff9933;
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
padding: 20px;
}

.quote-box {
background-color: white;
padding: 40px;
max-width: 700px;
width: 90%;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
position: relative;
}

.quote-mark {
position: absolute;
top: 10px;
left: 20px;
font-size: 100px;
font-family: serif;
color: #ff9933;
margin: 0;
line-height: 1;
opacity: 0.6;
}

#quote {
font-size: 24px;
line-height: 1.5;
margin-top: 50px;
margin-bottom: 20px;
text-align: left;
padding-left: 15px;
}

#author {
font-size: 18px;
text-align: right;
margin-bottom: 30px;
margin-top: 0;
}

#author::before {
content: "- ";
}

#new-quote {
background-color: #ff9933;
color: white;
border: none;
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
float: right;
transition: background-color 0.2s;
}

#new-quote:hover {
background-color: #e0872c;
}
80 changes: 80 additions & 0 deletions prep/mean.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
Let’s consider a list of prices in a bill:
4.6, 5.03, 7.99, 8.01
instead of writing the like below
const price0 = 4.6;
const price1 = 5.03;
const price2 = 7.99;
const price3 = 8.01;
We can write it as an array literal
const items = [4.6, 5.03, 7.99, 8.01];

The Array object, as with arrays in other programming languages, enables storing a collection
of multiple items under a single variable name, and has members for performing common array operations.
Arrays can store items of any type & multiple pieces of information.

In JavaScript, we can use [] notation to access specific elements in the array using index numbers.
The index numbers start from 0.


const items = [4.6, 5.03, 7.99, 8.01];
console.log(items[0]); // 4.6
console.log(items[1]); // 5.03
console.log(items[2]); // 7.99
console.log(items[3]); // 8.01
// Accessing elements using index numbers

const items = [4.6, 5.03, 7.99, 8.01];
function calculateMean(list) {
// Calculate the sum of all elements in the array
const sum = list.reduce(
(accumulator, currentValue) => accumulator + currentValue,
0
);
// Calculate the mean by dividing the sum by the number of elements
const mean = sum / list.length;
return mean;
}
*/
/*
//const list = [4.6, 5.03, 7.99, 8.01];
function calculateMean(list) {
//1. sum the elements of the array
let sum = 0;
for (let i = 0; i < list.length; i++) {
const arrayInValue = Number(list);
if (typeof list[i] === "number" && !isNaN(list[i])) {
sum += list[i];
}
}
//2. determine the length of the array
let count = list.length;
//3. divide #1 by #2
const mean = sum / count;
//4. return #3
return mean;
}
console.log(calculateMean([4.6, 5.03, 7.99, 8.01]));
console.log(calculateMean([3, "50", 7]));

//module.exports = calculateMean;
*/
function calculateMean(list) {
//1. sum the elements of the array
let sum = 0;
for (const item of list) {
const value = Number(item);
if (!isNaN(value)) {
sum += value;
}
}

//2. determine the length of the array
let count = list.length;
//3. divide #1 by #2
const mean = sum / count;
//4. return #3
return mean;
}
console.log(calculateMean([4.6, 5.03, 7.99, 8.01]));
console.log(calculateMean([3, "50", 7]));
26 changes: 26 additions & 0 deletions prep/mean.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
test("does something as described below", () => {
// test implementation goes here
});


*/
//mean.test.js
const mean = require("./mean"); // Import the mean function from mean.js
test("calculates the mean of a list of numbers", () => {
expect(mean([3, 50, 7])).toBe(20); // 20 is (3 + 50 + 7) / 3
expect(mean([4.6, 5.03, 7.99, 8.01])).toBeCloseTo(6.4075); // 6.4075 is (4.6 + 5.03 + 7.99 + 8.01) / 4
expect(mean([10, 20, 30, 40, 50])).toBe(30); // 30 is (10 + 20 + 30 + 40 + 50) / 5
expect(mean([1, 2, 3, 4, 5, 6])).toBe(3.5); // 3.5 is (1 + 2 + 3 + 4 + 5 + 6) / 6
});
/*
The expect statement is used to create an assertion that checks if the output of the mean function matches the expected value.
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
});

*/
16 changes: 16 additions & 0 deletions prep/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "prep",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "jest"
},
"keywords": [],
"author": "",
"license": "ISC",
"type": "commonjs",
"devDependencies": {
"jest": "^30.2.0"
}
}