Hi everyone,
In this exercise https://academy.ivanontech.com/products/javascript-programming-for-blockchain-developers/categories/1708400/posts/5734443, Ivan added a redrawList() function. I’m wondering if it is necessary. The way I have come to solve the exercise was somewhat different.
Here is my solution:
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Ordered list assignment</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
</head>
<body>
<h1>My favourite fruits</h1>
<ol id="myList">
</ol>
<input type="text" id="textBox" onfocus="this.value=''" placeholder="Enter a fruit..."> //here, I was looking for a solution to be able avoid clearing the text input box manually. I found onfocus will do the job when we click on the box.
<button type="button" id="btn1" name="button">Add to list</button>
<script>
let fruits = [];
let list = $("#myList");
$("#btn1").click(function(){
let item = $("#textBox").val();
if(fruits.includes(item)){ //this is to avoid adding the same fruit to the list
alert("Item is already on list, please enter a different fruit!")
}else{
fruits.push(item);
console.log(fruits);
$("<li/>").text(item).appendTo(list);
}
});
</script>
</body>
</html>
I didn’t really understand why we need to display a list in the first place, and not just letting the user to add items to the list. Can anyone explain why my code is not practical, and why Ivan’s solution would be a better one?
I’m trying to understand…
Thanks a lot
Zoltan