#Vehicle Purchase - JS

15 messages · Page 1 of 1 (latest)

lusty cloud
#

Hello guys, I hope you are well, I hope you can help me, I am trying to do the Vehicle Purchase exercise, in the second problem which is "Choose between two potential vehicles to buy" it does not give me the expected result, I already used the method " length ", but I still receive the expected result, then I leave you my code


function chooseVehicle(option1, option2) {

    if(option1.length > option2.length){
        return option1 + " is clearly the better choice.";
    }else if(option1.length < option2.length){
        return option2 + " is clearly the better choice.";
    }else{
        return 'none';
    }
}

console.log(chooseVehicle('Wuling Hongguang', 'Toyota Corolla'));

// =>  'Toyota Corolla is clearly the better choice.'
// =>  'Volkswagen Beetle is clearly the better choice.'

wooden saddle
#

Could you share the test code run and test failure details? Please use a codeblock and not a screenshot

lusty cloud
#

Of course, with the length method, it passes test number 7, but the error is thrown in test number 6.

expect(chooseVehicle('Bugatti Veyron', 'Ford Pinto')).toBe(
      'Bugatti Veyron' + rest,
    );
expect(chooseVehicle('Chery EQ', 'Kia Niro Elektro')).toBe(
      'Chery EQ' + rest,
    );
Error: expect(received).toBe(expected) // Object.is equality

Expected: "Chery EQ is clearly the better choice."
Received: "Kia Niro Elektro is clearly the better choice."
wooden saddle
#

And that's when you use the above code?

#

I don't think you're supposed to be checking lengths at any point

#

What's the first if do?

#

(in English)

lusty cloud
#

Yes, I am currently using the length method, but the code still does not work, I have already tried to invert options 1 and 2 and it still does not work

wooden saddle
#

Better yet, tell me in English what your logic/algorithm is

lusty cloud
#

Compare the text string lengths of the two vehicle options (option1 and option2).
If the length of option1 is greater than the length of option2, a message is returned indicating that option1 is the best choice.
If the length of option1 is less than the length of option2, a message is returned indicating that option2 is the best choice.
If the lengths of both options are equal, 'none' is returned, indicating that there is no clear best choice between the two options in terms of length.

wooden saddle
#

Compare the text string lengths of the two vehicle options (option1 and option2).
I don't think the exercise mentions length anywhere?

lusty cloud
#

Thanks for the help, you can detect the error and I was able to solve it, I appreciate the help. 🤝

wooden saddle
#

For that implement the function chooseVehicle(option1, option2) that takes two vehicles as arguments and returns a decision that includes the option that comes first in dictionary order.

#

Congrats! 🎉
You're very welcome

bronze helmBOT
#

If everything is resolved, we ask that people react to the top/original post with a :white_check_mark: (:white_check_mark:). This indicates to others that this issue has been resolved and locks the thread.
If all the tests pass and you want to further improve your solution, we encourage you to use the "Request a Code Review" feature on the website!