Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Having an issue finding the first occurance of an element in an array and then c

ID: 3744435 • Letter: H

Question

Having an issue finding the first occurance of an element in an array and then counting each time it compares elements and displays said number. This is what I have so far but it does not seem to work for me. Disregard the d as it is found previously in the code as a global output.

function displaySearch(){

var k = parseInt(document.getElementById("search").value);
var count=0;

for (i = 0; i<array.length; i++){
if(array[i]==k){
break;
}
else {
count++

}
}
d = "<br/>You found " + k + ". It took you " + count + " comparisons.";
}

Explanation / Answer

function displaySearch(){ var k = parseInt(document.getElementById("search").value); var count=0; var found = false; for (i = 0; i
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote