Refactor the code below to remove all ESLint errors, without disabling any rules or modifying the ESLint configuration.
function isAdult(birthdate) {
function calcAge(birthdate) {
let n = new Date();
let b = new Date(birthdate);
let a = n.getFullYear() - b.getFullYear();
let md = n.getMonth() - b.getMonth();
// Adjust age if the birth date hasn't occurred yet this year
if (md < 0 || md === 0) {
if (n.getDate() < b.getDate()) {
a--;
}
}
return a;
}
var a = calcAge(birthdate);
function isAdult(a) {
// Check if the age is 18 or older
// Return true if the person is an adult, false otherwise
if (a >= 18) {
return true;
} else {
return false;
}
}
return { message: 'isAdult(' + birthdate + ') = ' + isAdult(a), result: isAdult(a) };
}
var data = ['2000-01-01', '2005-05-05', '2010-10-10', '1995-12-31', '2021-01-01'];
function output(data) {
var c = 0;
for (var i = 0; i < data.length; i++) {
var u = isAdult(data[i]);
console.log(u.message);
if (u.result) {
c++;
}
}
return c + ' of the ' + data.length + ' group are adults';
}
console.log(output(data));
Use the following eslint.config.js file:
import js from '@eslint/js';
import globals from 'globals';
import { defineConfig } from 'eslint/config';
export default defineConfig([
{
files: ['**/*.{js,mjs,cjs}'],
languageOptions: {
globals: globals.browser,
ecmaVersion: 'latest',
},
plugins: { js },
rules: {
...js.configs.all.rules,
'sort-keys': 'off',
'sort-imports': 'off',
'capitalized-comments': 'warn',
'no-inline-comments': 'warn',
'one-var': ['error', 'never'],
'no-warning-comments': 'off',
'no-console': 'warn',
},
},
]);
Given the following code construct:
const MAX_CHILD_AGE = 12;
const MIN_ELDERLY_AGE = 65;
const KM_DISTANCE_FOR_DISCOUNT = 75;
const AGE_DISCOUNT = 0.3;
const RANGE_DISCOUNT = 0.25;
/**
* @description This function checks if a person is a child based on their age.
*
* @param {number} age - The age of the person (must be a positive integer)
* @returns {boolean} - true if the person is a child, false otherwise
* @throws {Error} - If the age is not a positive integer
*/
function isChild(age) {
return null;
}
/**
* @description This function checks if a person is an elderly based on their age.
*
* @param {number} age - The age of the person (must be a positive integer)
* @returns {boolean} - true if the person is elderly, false otherwise
* @throws {Error} - If the age is not a positive integer
*/
function isElderly(age) {
return null;
}
/**
* @description This function checks if a person is a child or an adult based on their age.
*
* @param {number} age - The age of the person (must be a positive integer)
* @returns {boolean} - true if the person has the right for a discount, false otherwise
* @throws {Error} - If the age is not a positive integer
*/
function hasRightForAgeDiscount(age) {
return null;
}
/**
* @description This function checks if the travel distance is greater than the defined distance for a discount.
*
* @param {number} travelDistance - The distance in km of the travel (must be a positive integer)
* @returns {boolean} - true if the travel distance is greater than the defined distance for a discount, false otherwise
* @throws {Error} - If the travel distance is not a positive integer
*/
function hasRightForRangeDiscount(travelDistance) {
return null;
}
/**
* @description This function calculates the price of a ticket based on the age of the person and the travel distance.
*
* @param {number} age - The age of the person (must be a positive integer)
* @param {number} travelDistance - The distance in km of the travel (must be a positive integer)
* @returns {number} - The price of the ticket after applying any applicable discounts (AGE_DISCOUNT and RANGE_DISCOUNT)
* @throws {Error} - If the age or travel distance is not a positive integer
*/
function ticketsPrice(age, travelDistance) {
return null;
}
export {
isChild,
isElderly,
hasRightForAgeDiscount,
hasRightForRangeDiscount,
ticketsPrice,
};
Write unit tests for the functions isChild, isElderly, hasRightForAgeDiscount, hasRightForRangeDiscount, and ticketsPrice using vitest. Ensure that you not only cover the happy flow scenarios, but also all possible edge cases and scenarios.
Implement the functions isChild, isElderly, hasRightForAgeDiscount, hasRightForRangeDiscount, and ticketsPrice and validate that your implementation complies with your tests (also known as Test Driven Development [TDD]).
Implement a Lit Web Components based UI for your code of challenge 3 and write an E2E test for it using Playwright. The E2E test should cover the following scenarios:
Check you site of challenge 3 for accessibility issues by adding a playwright test that checks for the following scenarios:
Solve any accessibility issues that you find and then check you site of challenge 3 for accessibility issues using the AXE and/or ArcTool browser extensions.
Home |
E2E Testing |
Testing