while working around in Js, I need to check if a value in array is present or not & if it exists show error to user, & if not push it in array. here is my code snippet.
<html>
<body>
<label>Enter an New item to add in Stock</label>
<br> </br>
<input type="text" name=" itemName" id="addItemInStock">
<br></br>
<p id="errorMsg"></p>
<button onclick="addToStock()">Add</button>
<p id="showList"></p>
<select id="showInDropDown">
<option disabled selected style="display: block;">Stock Items</option>
</select>
<script>
var fruitsfromLS = localStorage.getItem("fruits");
var fruits = fruitsfromLS ? JSON.parse(fruitsfromLS) : ["Banana", "Orange", "Apple", "Mango"];
//document.getElementById("showList").innerHTML = fruits;
var newItem = document.getElementById("addItemInStock");
function addToStock() {
if ((newItem.value) === "") {
document.getElementById("errorMsg").innerHTML = "Blank item cannot be added!!";
document.getElementById("errorMsg").style.display = "block";
} else if ((newItem.value) === fruits[i].value)) {
document.getElementById("errorMsg").innerHTML = "aLREADY IN sTOCK!";
document.getElementById("errorMsg").style.display = "block";
} else {
document.getElementById("errorMsg").style.display = "none";
fruits.push(newItem.value);
localStorage.setItem("fruits", JSON.stringify(fruits));
clearAndShow();
}
fillSelect();
}