Skip to content
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

Finished Lab (Jordan) #109

Open
wants to merge 4 commits into
base: master
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
8 changes: 8 additions & 0 deletions numberStatistics.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<html>
<head>
<title>Number Statistics</title>
<script defer src="numberStatistics.js"></script>
</head>
<body>
<h1>Number Statistics</h1>
Expand All @@ -24,5 +25,12 @@ <h1>Number Statistics</h1>
and the third paragraph should show the minimum value.
</li>
</ul>

<input type="number" id="number-statistics-number-input" placeholder="waiting for a number">
<button type="submit" id="number-statistics-button" onclick="onSubmit()">submit</button><br>
<p id="number-statistics-average">average: </p>
<p id="number-statistics-maximum">maximum: </p>
<p id="number-statistics-minimum">minimum: </p>
<ul id="number-statistics-numbers"></ul>
</body>
</html>
48 changes: 48 additions & 0 deletions numberStatistics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
let sum = 0
let max = -Infinity
let min = Infinity

function onSubmit(){
//First, retrieve the inputted number
let input = document.getElementById('number-statistics-number-input')
let value = Number(input.value)

//Make a list to display all inputted numbers
let list = document.createElement('li')
list.textContent = value

//Display the list in an unordered fashion
const ul = document.querySelector('#number-statistics-numbers')
ul.appendChild(list)

//Grab all the values from the current run-through of the list, put them into an array to track
//how many values have been inputted
let array = document.querySelectorAll('#number-statistics-numbers li')
let i = 0
sum = sum + value
while(i < array.length){
i++
}

//(i) tracks all the numbers, so divide the sum of all numbers by how many numbers exist
let average = document.getElementById('number-statistics-average')
average.textContent = "average: " + (sum/i)

//max is sent to -Infinity, which changes if the inputted value is larger than it
let maximum = document.getElementById('number-statistics-maximum')
if(value > max){
max = value
}
maximum.textContent = "maximum: " + max

//min is sent to Infinity, which changes if the inputted value is smaller than it
let minimum = document.getElementById('number-statistics-minimum')
if(value < min){
min = value
}
minimum.textContent = "minimum: " + min
console.log(array)

//average, maximum, and minimum will all display the same number on the first input.
//That will change with multiple numbers being inputted.
}
Loading