Linear search algorithm Javascript Watch

Caitlin_R97
Badges: 1
Rep:
?
#1
Report Thread starter 1 year ago
#1
I have written most of the code, but it keeps returning null instead of finding the 483373 in the students array, can someone help?

Implement a function find(needle, haystack) that
-uses an iterative linear sort algorithm
-returns the object in haystack which has the snr property matching the needle
-returns null if there is no object in haystack that has a snr property matching the needle


function find(needle, haystack){

if(haystack.length > 0){
//console.log(needle);
//console.log(haystack[0]);
if(needle == haystack[0]){
return 0;
}else{
var idx = find(needle, haystack.slice(1));
if(idx !== null) {

idx = idx +1 ;
}
return idx;

}
}else{
return null;


}

}

var students =[{name: 'Mark', snr: 9947832}, {name: 'Dave', snr: 483373}, {name: 'Chris', snr: 6482724}];
console.log(find(483373, students));
0
quote
reply
X

Quick Reply

Attached files
Write a reply...
Reply
new posts
Latest
My Feed

See more of what you like on
The Student Room

You can personalise what you see on TSR. Tell us a little about yourself to get started.

Personalise

University open days

  • University of Lincoln
    Mini Open Day at the Brayford Campus Undergraduate
    Wed, 19 Dec '18
  • University of East Anglia
    UEA Mini Open Day Undergraduate
    Fri, 4 Jan '19
  • Bournemouth University
    Undergraduate Mini Open Day Undergraduate
    Wed, 9 Jan '19

Were you ever put in isolation at school?

Yes (246)
27.24%
No (657)
72.76%

Watched Threads

View All