NehaMehta
New Coder
MY CODE is working but is not passing test, need help!!
and below is test case by school
JavaScript:
function compare_to_sort(library) {
return library.sort(function (a, b) {
const titleA = a.title.toUpperCase();
const titleB = b.title.toUpperCase();
if (titleA < titleB) {
return -1;
}
if (titleA > titleB) {
return 1;
}
return 0;
});
}
var library = [
{ author: "Bill Gates", title: "The Road Ahead", libraryID: 1254 },
{ author: "Steve Jobs", title: "Walter Isaacson", libraryID: 4264 },
{
author: "Suzanne Collins",
title: "Mockingjay: The Final Book of The Hunger Games",
libraryID: 3245,
}, //This code uses var as instructed in the task, and therefor i do not change it to let or const.
];
const sortedLibrary = compare_to_sort(library);
console.log(sortedLibrary);
and below is test case by school
JavaScript:
const rewire = require('rewire');
describe('[ICODE]compare_to_sort[/ICODE] function', () => {
test("[ICODE]library[/ICODE] array should be defined", () => {
const indexFile = rewire('../index');
const library = indexFile.[B]get[/B]('library');
expect(library).toBeDefined();
expect(library.length).toBeTruthy();
})
test("function should sort elements in an array by [ICODE]title[/ICODE] and return them", () => {
const indexFile = rewire('../index');
const compare_to_sort = indexFile.[B]get[/B]('compare_to_sort');
const library = indexFile.[B]get[/B]('library');
expect(library.sort(compare_to_sort)).toEqual(expect.arrayContaining([
{
author: expect.any(String), title: expect.any(String), libraryID: expect.any(Number)
}
]));
});....
})
Last edited by a moderator: