เราจำเป็นต้องเขียนโปรแกรม 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> ผลลัพธ์
และผลลัพธ์บนหน้าจอจะเป็น −
