You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 lines
1.4 KiB
JavaScript

function countLetter(letter, sentence) {
let result = 0;
if (typeof letter !== 'string' || letter.length !== 1) {
return undefined;
}
for (let i = 0; i < sentence.length; i++) {
if (sentence[i] === letter) {
result++;
}
}
return result;
}
function isIsogram(text) {
// Convert the text to lowercase to disregard casing.
text = text.toLowerCase();
let seenLetters = {};
for (let char of text) {
if (seenLetters[char]) {
return false;
}
seenLetters[char] = true;
}
return true;
}
function purchase(age, price) {
if (age < 13) {
return undefined;
}
if (age >= 13 && age <= 21 || age >= 65) {
let discountedPrice = price * 0.8;
return discountedPrice.toFixed(2);
}
return price.toFixed(2);
}
function findHotCategories(items) {
let hotCategories = {};
for (let item of items) {
if (item.stocks === 0) {
hotCategories[item.category] = true;
}
}
let result = Object.keys(hotCategories);
return result;
}
function findFlyingVoters(candidateA, candidateB) {
let setA = new Set(candidateA);
let setB = new Set(candidateB);
let commonVoters = [...setA].filter(voter => setB.has(voter));
return commonVoters;
}
module.exports = {
countLetter,
isIsogram,
purchase,
findHotCategories,
findFlyingVoters
};