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
TSR Support Team

We have a brilliant team of more than 60 Support Team members looking after discussions on The Student Room, helping to make it a fun, safe and useful place to hang out.

Updated: November 14, 2017

985

students online now

800,000+

Exam discussions

Find your exam discussion here

Poll
Should predicted grades be removed from the uni application process

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.