1

I'm trying to search through my array for whatever number I put in, but it doesn't work as intended.

In external javascript:

window.onload = startup;

function startup() {
    runArraySequence () {
        writeArray.onclick = findArray;
    }
}
var array = [5,3,9,12,19,15,13,6,9,2,4,7,8,17];

function findArray () {
//The following script is a part of a 12 "else if" radio button form.
    if (arrayRad11.checked) {
        var searchNumber = document.getElementById("arrayValue").value;
        var arrayResult = -1;
        for (var i=0; i < array.length; i++) {
            if (array[i] === searchNumber) {
                i = arrayResult;
            }
            if (arrayResult < 0)  {
                msg6.innerHTML = "Found number " + searchNumber + ", " + arrayResult + " times.";
            }
        }
    }
}

HTML code:

<div>
    <form>
        <input type="radio" id="arrayRad11" name="array" value="11">Find Number:</input>
        <input type="number" id="arrayValue" placeholder="Find Array Number"></input><br />
        <input type="button" id="writeArray" value="Skriv tall"></input>
        <input type="button" value="Reset" onClick="window.location.reload()"></input>
    </form>
    <p id="msg6"></p>
</div>

Also uploaded to jsfiddle

2
  • And when I say "as intended" I guess I should add; It gives me the result: Found Number 6, -1 times. Commented Sep 22, 2015 at 17:13
  • var searchNumber = +document.getElementById("arrayValue").value; Commented Sep 22, 2015 at 17:16

2 Answers 2

2

There seems to be a couple of logic issues in the code, but fairly easy fixes:

writeArray.onclick = findArray;

var array = [5, 3, 9, 12, 19, 15, 13, 6, 9, 2, 4, 7, 8, 17, 9];

function findArray() {
  //The following script is a part of a 12 "else if" radio button form.
  if (arrayRad11.checked) {
    var searchNumber = document.getElementById("arrayValue").value;
    var arrayResult = 0;
    for (var i = 0; i < array.length; i++) {
      if (array[i] == searchNumber) {
        arrayResult++;
      }
    }
    if (arrayResult > 0) {
      msg6.innerHTML = "Found number " + searchNumber + ", " + arrayResult + " times.";
    }
  }
}
<div>
  <form>
    <input type="radio" id="arrayRad11" name="array" value="11">Find Number:</input>
    <input type="number" id="arrayValue" placeholder="Find Array Number"></input>
    <br />
    <input type="button" id="writeArray" value="Skriv tall"></input>
    <input type="button" value="Reset" onClick="window.location.reload()"></input>
  </form>
  <p id="msg6"></p>
</div>

  1. I removed your runArraySequence() function since that didn't exist in your JS. It was blocking the onclick from being reachable.

  2. Based on your message, msg6, it seemed that you were looking for the frequency of the number in the array. I set the arrayResult value to start at 0 and increment count if the == statement was true. Make sure you know the type association difference between == and ===.

  3. I moved your third if statement outside of the for loop so that the message result would only appear once.

Hope this helps!

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

2 Comments

Yeah this is basically how I imagined it would be. Is there a solution for the "Found Number 2, 1 times" when number 2 is not a part of the array?
I'm not sure I completely understand what you're asking, but the message displayed is controlled by the 3rd "if" statement. If you want the message to be displayed under different circumstances, you need to change the (arrayResult > 0) conditions.
1

You can find values in arrays using Array.prototype.indexOf() method

[1,2,3].indexOf(2)
// will give you 1

To get multiple values you need to use indexOf in loop

var find=2;
var i,arr=[1,2,3,2,7],entries=[];

while(-1<(i=arr.indexOf(find,i+1)))
  entries.push(i);

console.log('I found '+ents.length+' entries of '+find+' in array:',ents)
//I found 2 entries of 2 in array: Array [ 1, 3 ]

Comments

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.