<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<div id="myDiv">
<h2>Рост учеников</h2>
</div>
<script src="index.js"></script>
</body>
</html>
h2 {
font-family: sans-serif;
}
button {
border: none;
height: 40px;
border-radius: 5px;
margin-right: 20px;
color: white;
}
ul {
list-style-type: none;
}
#myDiv {
margin-left: 50px;
}
#myDiv ul {
padding-inline-start: 0px;
}
#myDiv li {
margin-bottom: 5px;
}
let button = document.createElement('button');
button.textContent = 'Добавить рост';
button.style.background = '#228B22';
button.style.width = '130px';
let buttonFilter = document.createElement('button');
buttonFilter.textContent = 'Фильтровать';
buttonFilter.style.background = '#FF7F50';
buttonFilter.style.width = '130px';
let container = document.getElementById('myDiv');
container.append(button);
container.append(buttonFilter);
let arr = [164, 157, 160, 143, 170];
let listEL;
function renderList(arr, bookSearch) {
if (listEL) listEL.remove();
listEL = document.createElement('ul');
for (i = 0; i < arr.length; i++) {
const liEl = document.createElement('li');
liEl.textContent = `${i + 1}) ${arr[i]}`;
if (arr[i] === bookSearch) {
liEl.style.width = '17%';
liEl.style.background = 'yellow';
liEl.style.margin = '0 0 0 12px ';
liEl.style.padding = '7px';
}
listEL.append(liEl);
}
container.append(listEL);
}
renderList(arr);
button.onclick = function book() {
let height = prompt('Введите рост ученика');
if (height === '') {
alert('Рост не введён!');
}
if (height != '') {
arr.push(height);
}
renderList(arr);
};
function minHgt(arr, minHeight) {
let result = [];
for (item of arr) {
if (item >= minHeight) {
result.push(item);
}
}
return result;
}
buttonFilter.onclick = function filter() {
let minHeight = prompt('Введите минимальный рост');
console.log(minHgt(arr, minHeight));
};
Почему функция minHgt в массив result добавляет ВСЕ item, а не только те, которые выше или равны minHeight?