educative.io

Test failure on the exercise-library-management exercise

 id = id; // id String
available = available; // available String saying 'true' / 'false'
console.log(available)
count = count; // count String
name=name; // name String 
author=author; // author String
ans={
    'id': id*1,
    'available': !!(available),
    'count': count*1,
    'name': name,
    'author': author
}; // assign final answer to ans variable

for the above code I am getting 2/3 tests passed. I would like to understand why there is a failure on Test Case # 2.

Thanks.

Kind regards,

Sam

Hi @Sam_Salani !!
The issue with the code is in the way you are converting the “available” variable from a string to a boolean. You are using double negation (!!) to convert the string to a boolean, which works for most cases, but it doesn’t handle the case where the input string is “false” correctly. In JavaScript, any non-empty string evaluates to true when converted to a boolean.

To handle this correctly, you should specifically check if the string is equal to “true” and assign true or false accordingly. Here’s the corrected code:

ans = {
    'id': Number(id),
    'available': available === 'true',
    'count': Number(count),
    'name': name,
    'author': author
};

With this code, it will correctly convert the “available” string to a boolean based on whether it’s equal to the string “true” or not, and all your test cases should pass as expected.
I hope it helps. Happy Learning :blush: