ภาพรวม
เราต้องเขียนโปรแกรม JavaScript ที่ให้ผู้ใช้ป้อนค่าสตริง โปรแกรมจะตรวจสอบค่าที่ป้อนกับค่าที่กำหนดไว้ในอาร์เรย์ แล้วแสดง true หากค่าอยู่ในอาร์เรย์ มิฉะนั้นแสดง false
ตัวอย่างโค้ด (HTML + JavaScript)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>CHECK EXISTENCE</title>
</head>
<body>
<script>
const arr = ['arsenal', 'chelsea', 'everton', 'fulham', 'swansea'];
const checkExistence = () => {
const userInput = document.getElementById("input").value;
const exists = arr.includes(userInput);
document.getElementById('result').innerText = exists;
};
</script>
<input type="text" id="input">
<button onclick="checkExistence()">Check</button>
<p id='result'></p>
</body>
</html>
ผลลัพธ์บนหน้าจอ
แสดงผล true หรือ false ตามค่าที่ป้อน