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.

124 lines
4.0 KiB
JavaScript

// Check first whether the letter is a single character.
// If letter is a single character, count how many times a letter has occurred in a given sentence then return count.
// If letter is invalid, return undefined.
function countLetter(letter, sentence) {
if (letter.length !== 1) {
return undefined; // Return undefined if letter is not a single character
}
const cleanedLetter = letter.toLowerCase(); // Convert letter to lowercase for case insensitivity
const cleanedSentence = sentence.toLowerCase(); // Convert sentence to lowercase for case insensitivity
let count = 0;
for (const char of cleanedSentence) {
if (char === cleanedLetter) {
count++;
}
}
return count;
}
function isIsogram(text) {
// An isogram is a word where there are no repeating letters.
// The function should disregard text casing before doing anything else.
// If the function finds a repeating letter, return false. Otherwise, return true.
// Convert the text to lowercase for case insensitivity
function isIsogram(text) {
const cleanedText = text.toLowerCase(); // Convert text to lowercase for case insensitivity
const uniqueLetters = new Set();
for (const letter of cleanedText) {
if (letter !== ' ') {
if (uniqueLetters.has(letter)) {
return false; // Found a repeating letter
} else {
uniqueLetters.add(letter);
}
}
}
return true; // No repeating letters found
}
function purchase(age, price) {
// Return undefined for people aged below 13.
// Return the discounted price (rounded off) for students aged 13 to 21 and senior citizens. (20% discount)
// Return the rounded off price for people aged 22 to 64.
// The returned value should be a string.
function purchase(age, price) {
if (age < 13) {
return undefined;
} else if (age >= 13 && age <= 21 || age >= 65) {
// Apply 20% discount for students aged 13 to 21 and senior citizens
const discountedPrice = Math.round(price * 0.8);
return discountedPrice.toString();
} else {
// For people aged 22 to 64, return the rounded off price
return Math.round(price).toString();
}
}
function findHotCategories(items) {
// Find categories that has no more stocks.
// The hot categories must be unique; no repeating categories.
// The passed items array from the test are the following:
{ id: 'tltry001', name: 'soap', stocks: 14, category: 'toiletries' }
{ id: 'tltry002', name: 'shampoo', stocks: 8, category: 'toiletries' }
{ id: 'tltry003', name: 'tissues', stocks: 0, category: 'toiletries' }
{ id: 'gdgt001', name: 'phone', stocks: 0, category: 'gadgets' }
{ id: 'gdgt002', name: 'monitor', stocks: 0, category: 'gadgets' }
// The expected output after processing the items array is ['toiletries', 'gadgets'].
// Only putting return ['toiletries', 'gadgets'] will not be counted as a passing test during manual checking of codes.
// Use a Set to keep track of unique categories
const uniqueCategories = new Set();
// Iterate through the items
for (const item of items) {
if (item.stocks === 0) {
uniqueCategories.add(item.category);
}
}
// Convert the Set to an array and return it
return Array.from(uniqueCategories);
}
}
function findFlyingVoters(candidateA, candidateB) {
// Find voters who voted for both candidate A and candidate B.
// The passed values from the test are the following:
candidateA: ['LIWf1l', 'V2hjZH', 'rDmZns', 'PvaRBI', 'i7Xw6C', 'NPhm2m']
candidateB: ['kcUtuu', 'LLeUTl', 'r04Zsl', '84EqYo', 'V2hjZH', 'LIWf1l']
The expected output after processing the candidates array is ['LIWf1l', 'V2hjZH'].
Only putting return ['LIWf1l', 'V2hjZH'] will not be counted as a passing test during manual checking of codes.
}
module.exports = {
countLetter,
isIsogram,
purchase,
findHotCategories,
findFlyingVoters
};