I want to return a property in an object in an array but its causing other problems in the code

Viewed 98

So I'm using this function that's supposed to make a car change lanes. It will change lanes as long as I don't return the number plate return vehicleObject_list[i]. But when I return the number plate as the instructions ask, the car stops changing lanes return vehicleObject_list[i].Number_Plate. The details are below:

function moveLanes(target_car)
{
    /*
    This function should do the following: 
     - move target_car from one lane to the other.
     - do the move in a single step without any extra animation.
     - use Lane_Position_a and Lane_Position_b to effect the change.
     - finally you should return target_car at the end of the function.
     hint: You will need to modify the x property of target_car.
    */
    if(checkCarInfront(target_car).x == Lane_Position_a ){
        target_car.x = Lane_Position_b;
    }   
    else {
        target_car.x = Lane_Position_a;
    }
    
}


function checkCarInfront( carObj )
{
    /*
    This function should do the following: 
     - determine if carObj is in the same lane and less than 200px behind any of the cars in vehicleObject_list.
     - do this by traversing vehicleObject_list and comparing each car's Distance_Driven property to that of carObj.
     - if you find a car that matches these requirements then return the Number_Plate property for the car. Otherwise return false.
    */
    
    for (var i = 0; i < vehicleObject_list.length; i++)
    {
        if (carObj.x == vehicleObject_list[i].x && ((vehicleObject_list[i].Distance_Driven - carObj.Distance_Driven) < 200) && ((vehicleObject_list[i].Distance_Driven - carObj.Distance_Driven) > 0))
        {
            return vehicleObject_list[i].Number_Plate;
        }
    }
    return false;
    
    
}

var vehicleObject_list = [
{ x: 500, y: 0, Distance_Driven: -200, Car_Type: 'greenCar', Number_Plate: 'MBH0WW', Gas_Amt: 2, exhaust: [  ]} , { x: 500, y: 0, Distance_Driven: 200, Car_Type: 'whiteCar', Number_Plate: 'RLDGCM', Gas_Amt: 2, exhaust: [  ]} , { x: 300, y: 0, Distance_Driven: 600, Car_Type: 'whiteCar', Number_Plate: '9WGXXI', Gas_Amt: 2, exhaust: [  ]} ]

Detective_CarObject = 
    {
        x: roadLeftEdge + roadWidth/4,
        y: 550,
        Distance_Driven: 0,
        Gas_Amt: 3,
        EngineShudder_Value: 0,
        Car_Type: 'detective',
        Number_Plate: '5L3UTH',
        exhaust: []
    }

1 Answers

You could do something like:

if(checkCarInfront(target_car) && target_car.x == Lane_Position_a ){
    target_car.x = Lane_Position_b;
} else {
    target_car.x = Lane_Position_a;
}

because checkCarInfront(target_car) will return a number plate string if there is a match (which will evaluate to true as a non empty string). You can link the condition to the target_car object which has an .x property.

Be sure to manually test the data. (for example, with the current vehicleObject_list and Detective_CarObject values if you call checkCarInfront(Detective_CarObject) this will be false (due to Distance_Driven). You can however temporarily set Detective_CarObject's x to 500 and add an extra test car in the list to check your logic: { x: 500, y: 0, Distance_Driven: 199, Car_Type: 'rainbowCar', Number_Plate: 'R41NB0W', Gas_Amt: 2, exhaust: [ ]})

As a side note I recommend adhering to JS naming conventions. Here are a couple of examples: W3Schools, FreeCodeCamp, Google JavaScript Style Guide. In the past I would've said choose one (e.g. underscores/snake_case (e.g. target_cat) or camelCase (e.g. checkCarInFront)) but not both and keep it consistent, but these days it seems the majority adhere to camelCase. Doing so will make it much easier to work in a team later and share/contribute to open source libraries and projects.

Related