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
52 changes: 30 additions & 22 deletions numberStatistics.html
Original file line number Diff line number Diff line change
@@ -1,28 +1,36 @@
<!DOCTYPE html>
<html>
<head>

<head>
<title>Number Statistics</title>
</head>
<body>
<h1>Number Statistics</h1>
</head>
<script src="numberStatistics.js" defer></script>

<body>
<h1>Number Statistics</h1>
<ul>
<li>
The user should be able to enter a number into a number input with
id='number-statistics-number-input'
</li>
<li>They should then be able to click a "submit" button with id='number-statistics-button'</li>
<li>
Clicking the submit button will add that number to a list with
id='number-statistics-numbers'. There should be three paragraphs on the
page: id='number-statistics-average', id='number-statistics-maximum',
and id='number-statistics-minimum'.
</li>
<li>
The first paragraph should show the average of all the numbers,
the second paragraph should show the maximum value,
and the third paragraph should show the minimum value.
</li>
<li>
The user should be able to enter a number into a number input with id='number-statistics-number-input'
</li>
<li>
They should then be able to click a "submit" button with id='number-statistics-button'
</li>
<li>
Clicking the submit button will add that number to a list with id='number-statistics-numbers'. There should be three paragraphs on the page: id='number-statistics-average', id='number-statistics-maximum', and id='number-statistics-minimum'.
</li>
<li>
The first paragraph should show the average of all the numbers, the second paragraph should show the maximum value, and the third paragraph should show the minimum value.
</li>
</ul>
</body>
</html>
<input id="number-statistics-number-input" type="number" placeholder="Enter your number please">
<button id="number-statistics-button" onclick="statistics()" type="submit">Submit</button> <br>
<ul id="number-statistics-numbers">
<p id="number-statistics-average">Average</p>
<br>
<p id="number-statistics-maximum">Maximum</p>
<br>
<p id="number-statistics-minimum">Minimum</p>
</ul>
</body>

</html>
29 changes: 29 additions & 0 deletions numberStatistics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
let array = []

function statistics() {
const number = Number(document.querySelector("#number-statistics-number-input").value)
const average = document.querySelector("#number-statistics-average")
const max = document.querySelector("#number-statistics-maximum")
const min = document.querySelector("#number-statistics-minimum")
const list = document.querySelector("#number-statistics-numbers")
let maxValue = -Infinity
let minValue = Infinity
let averageValue = 0
let sum = 0
array.push(number)
for (let i = 0; i < array.length; i++) {
sum += array[i]
}
let sortedArray = array.sort((a, b) => {
return a - b
})
averageValue = sum / array.length
maxValue = sortedArray[sortedArray.length - 1]
minValue = sortedArray[0]
const newLi = document.createElement("li")
newLi.textContent = number
list.appendChild(newLi)
average.textContent = `Average is ${averageValue}`
max.textContent = `Max is ${maxValue}`
min.textContent = `Min is ${minValue}`
}
Loading