我想 return 数组中的对象中的 属性 但它会导致代码中出现其他问题
I want to return a property in an object in an array but its causing other problems in the code
所以我正在使用这个应该让汽车变道的功能。只要我不return号牌return vehicleObject_list[i]
,它就会变道。但是当我return按指示要求的车牌时,汽车停止变道return vehicleObject_list[i].Number_Plate
。详情如下:
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: []
}
你可以这样做:
if(checkCarInfront(target_car) && target_car.x == Lane_Position_a ){
target_car.x = Lane_Position_b;
} else {
target_car.x = Lane_Position_a;
}
因为 checkCarInfront(target_car)
将 return 一个车牌字符串,如果有匹配项(它将评估为 true 作为非空字符串)。您可以 link 具有 .x
属性.
的 target_car
对象的条件
一定要手动测试数据。 (例如,使用当前的 vehicleObject_list
和 Detective_CarObject
值,如果您调用 checkCarInfront(Detective_CarObject)
这将是错误的(由于 Distance_Driven
)。但是您可以临时设置 Detective_CarObject
的 x 到 500
并在列表中添加额外的测试车以检查您的逻辑:{ x: 500, y: 0, Distance_Driven: 199, Car_Type: 'rainbowCar', Number_Plate: 'R41NB0W', Gas_Amt: 2, exhaust: [ ]}
)
作为旁注,我建议遵守 JS 命名约定。这里有几个例子:W3Schools, FreeCodeCamp, Google JavaScript Style Guide。
在过去,我会说选择一个(例如 underscores/snake_case(例如 target_cat
)或驼峰式(例如 checkCarInFront
)),但不能同时选择两者并保持一致,但现在看来大多数人坚持驼峰命名法。这样做将使以后在团队中工作和 share/contribute 开源库和项目变得更加容易。
所以我正在使用这个应该让汽车变道的功能。只要我不return号牌return vehicleObject_list[i]
,它就会变道。但是当我return按指示要求的车牌时,汽车停止变道return vehicleObject_list[i].Number_Plate
。详情如下:
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: []
}
你可以这样做:
if(checkCarInfront(target_car) && target_car.x == Lane_Position_a ){
target_car.x = Lane_Position_b;
} else {
target_car.x = Lane_Position_a;
}
因为 checkCarInfront(target_car)
将 return 一个车牌字符串,如果有匹配项(它将评估为 true 作为非空字符串)。您可以 link 具有 .x
属性.
target_car
对象的条件
一定要手动测试数据。 (例如,使用当前的 vehicleObject_list
和 Detective_CarObject
值,如果您调用 checkCarInfront(Detective_CarObject)
这将是错误的(由于 Distance_Driven
)。但是您可以临时设置 Detective_CarObject
的 x 到 500
并在列表中添加额外的测试车以检查您的逻辑:{ x: 500, y: 0, Distance_Driven: 199, Car_Type: 'rainbowCar', Number_Plate: 'R41NB0W', Gas_Amt: 2, exhaust: [ ]}
)
作为旁注,我建议遵守 JS 命名约定。这里有几个例子:W3Schools, FreeCodeCamp, Google JavaScript Style Guide。
在过去,我会说选择一个(例如 underscores/snake_case(例如 target_cat
)或驼峰式(例如 checkCarInFront
)),但不能同时选择两者并保持一致,但现在看来大多数人坚持驼峰命名法。这样做将使以后在团队中工作和 share/contribute 开源库和项目变得更加容易。