dwiyanto dwiyanto - 2 months ago 5
Javascript Question

Insert new array and summing the results of the new array

I have a problem in my script. I tried to add value in the array if the same input with the list that I have, it will generate value array[score] 100 and if they included the same value then the [score] will increase, I still started to learn jquery.



var animal = ["duck", "cat", "dog", "bird", "frog", "horse"];
var list = "";
var score = [];

for (i = 0; i < animal.length; i++) {
list += animal[i] + ", ";
}
document.getElementById("data").innerHTML = list;

var $data = $("#target");
$data.val('');
$('#add').click(function() {
if (this.value == animal){
score.push(100);
this.value('');
};

});

list : <label id="data"></label>
<br/>

<input id="target" class="form-control" type="text" value="">
<br/>
<input id="submit" class="form-control" type="button" value="add">
<br/>

score : <label id="count"></label>




Answer

Have created this fiddle based on your code, with these modifications:

  1. If the text entered in the input box matches an animal in the animal array, the score is increased by 100.
  2. The score is displayed in <label id="count"></label>
  3. The input box is cleared if the score is updated.

Is this what you wanted to achieve?

Comments