Turn on thread page Beta
    • 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));
 
 
 
Reply
Submit reply
Turn on thread page Beta
Updated: November 14, 2017

University open days

  1. Loughborough University
    General Open Day Undergraduate
    Fri, 21 Sep '18
  2. University of Cambridge
    Churchill College Undergraduate
    Fri, 21 Sep '18
  3. Richmond, The American International University in London
    Undergraduate Open Day Undergraduate
    Fri, 21 Sep '18
Poll
Which accompaniment is best?

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

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