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; iRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.