I want to compare user input to an array.
Example: if a user would write 4 in the input field, it should show us 31 as thats the value of the 4th element in my array.
What am i doing wrong in my code?
<!DOCTYPE html>
<html>
<head>
<title>Days in Months</title>
<meta charset="utf-8">
<script type="text/javascript">
window.onload = showDays;
var text = "";
// Days in each month, Jan - Dec
var days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
// Months
var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November",
"December"];
function showDays() {
document.getElementById("btn").onclick = selectMonth;
}
function selectMonth() {
var input = document.getElementById("input").value;
for (var i = 0; i < days.length; i++) {
if (input === days[i]) {
text += days[i];
}
}
document.getElementById("print").innerHTML = text;
}
</script>
</head>
<body>
<h1>Show days in Month</h1>
<input id="input" type="number" placeholder="Month (1-12)">
<button id="btn" type="button">Show Days</button>
<p id="print"></p>
</body>
</html>