Skip to content

Instantly share code, notes, and snippets.

@Wadkar07
Last active February 17, 2023 08:29
Show Gist options
  • Save Wadkar07/107e3d1519a02ce104850ddd1649c83b to your computer and use it in GitHub Desktop.
Save Wadkar07/107e3d1519a02ce104850ddd1649c83b to your computer and use it in GitHub Desktop.
Problem_1
// ==== Problem #1 ====
// The dealer can't recall the information for a car with an id of 33 on his lot. Help the dealer find out which car has an id of 33 by calling a function that will return the data for that car. Then log the car's year, make, and model in the console log in the format of:
// "Car 33 is a *car year goes here* *car make goes here* *car model goes here*"
const inventory = require('./cars.cjs')
function getCarWithId(inventoryData,id){
let car =[];
if(inventoryData === undefined || id === undefined || typeof inventoryData !== 'object' ||typeof id !== 'number'||!Array.isArray(inventoryData)||inventoryData.length === 0) {
return car;
}
car=inventoryData[id-inventoryData[0].id];
return car;
}
let id = 33;
const result = getCarWithId(inventory,id);
console.log(result)
if(!Array.isArray(result)){
console.log(`Car ${result.id} is a ${result.car_year} ${result.car_make} ${result.car_model}`);
}
else
console.log(result);
module.exports = getCarWithId;
var expect = require('chai').expect;
const inventory = require('../cars.cjs');
const problem1 = require('../problem1.cjs')
describe('Problem 1',() => {
it('Return the information for a car with an id of 33 on his lot',()=>{
expect(problem1(inventory,33)).to.eql({id: 33, car_make: 'Jeep', car_model: 'Wrangler', car_year: 2011});
})
it('Returns empty array',()=>{
expect(problem1()).to.eql([]);
expect(problem1(inventory)).to.eql([]);
expect(problem1(2)).to.eql([]);
expect(problem1([],3)).to.eql([]);
expect(problem1({id: 33, name: "Test", length: 10}, 33)).to.eql([]);
expect(problem1(inventory,[])).to.eql([]);
})
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment