Hey there! Sign in to join this conversationNew here? Join for free
    • Thread Starter
    Offline

    1
    ReputationRep:
    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));
 
 
 
  • 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.

  • Poll
    What's your favourite Christmas sweets?
  • 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.

  • The Student Room, Get Revising and Marked by Teachers are trading names of The Student Room Group Ltd.

    Register Number: 04666380 (England and Wales), VAT No. 806 8067 22 Registered Office: International House, Queens Road, Brighton, BN1 3XE

    Quick reply
    Reputation gems: You get these gems as you gain rep from other members for making good contributions and giving helpful advice.