0

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.

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>

2
  • You're trying to compare a string with an array? Commented Sep 8, 2016 at 3:16
  • yes @ifvictr, so if the string is equal to the array list then it will be true and generate value score. Commented Sep 8, 2016 at 3:20

2 Answers 2

2

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?

Sign up to request clarification or add additional context in comments.

6 Comments

very helpful answers were very good, I thought to add value scores must be stored in the array and then summed?.
No, you can just sum it up in a single variable like it's done in the fiddle code, no need to store it in an array.
but whether it could if the score was created a new array with a variable ?
Just for summing, a single variable is all you need. If you need to do things like track the score history for example, you can store each score hit in an array and then sum it up
whether you can give each instance with as add further later I will develop again?
|
0

For starters, you're event for your button is wrong, the id of the button is submit and not add, also you're comparing the value of you're button to an array, you should compare the text instead with the stuff in your array, and not the array itself.

3 Comments

means I have to compare the value of the array with new input text me the results when clicked ?
Yes, also since you've started learning jquery, I suggest that do not mix pure javascript with jquery. For instance, you are setting a value of an html through javascript. You should try to be consistent as much as possible.
Thank you for suggesting little help for me, I think it's pure javascript and jquery alike turned out differently .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.