I think that you need to coerce the string to a numerical value, in order to perform arithmetical functions.
You could perhaps do this most simply at the top of your add function, using a unary plus operator (+)
With the list.sort() shouldn’t the array be sorted from the lowest number to the highest number?
Since I try to find the highest number, the last one in the string, and the lowest, the first one, this is important.
let number = document.querySelector("input#txtnum");
let sel = document.querySelector("select#sel");
let res = document.querySelector("p#res");
let list = [];
function add() {
let n = +number.value;
list.sort();
if (n < 1 || n > 100 || list.includes(n)) {
alert("Invalid Value or already added to the list");
} else {
list.push(n);
let item = document.createElement("option");
item.text = `Number ${n} added`;
sel.appendChild(item);
res.innerHTML = ""
}
number.value = "";
number.focus();
}
function analyze() {
let total = list.length;
let max = list[list.length - 1];
let min = list[0];
let sum = 0;
for (let i = 0; i < list.length; i++) {
sum += list[i];
}
let avg = sum / total;
res.innerHTML = "";
res.innerHTML += `In total, we have ${total} numbers added <br><br>`;
res.innerHTML += `The highest number is ${min} <br><br>`;
res.innerHTML += `The lowest number is ${max} <br><br>`;
res.innerHTML += `The sum of all values ${sum} <br><br>`;
res.innerHTML += `The average is ${avg} <br><br>`;
}
I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.