Added S62
parent
d52fa7f45e
commit
151a35c28f
@ -0,0 +1 @@
|
||||
/node_modules
|
@ -0,0 +1,10 @@
|
||||
image: node:latest
|
||||
|
||||
stages:
|
||||
- test
|
||||
|
||||
test:
|
||||
stage: test
|
||||
script:
|
||||
- npm install
|
||||
- npm test
|
@ -0,0 +1,5 @@
|
||||
To start using the built-in tests, run <code>npm install</code> first then issue the <code>npm test</code> at the root directory of this project.
|
||||
|
||||
WARNING: Do not change any code inside <code>test.js</code>.
|
||||
|
||||
[Link to Solution](https://gitlab.com/zuitt-coding-bootcamp-curricula/courses/wdc028v1.5a/-/tree/main/fullstack/s52-solution)
|
@ -0,0 +1,86 @@
|
||||
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
|
||||
};
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,14 @@
|
||||
{
|
||||
"name": "s52",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "jest"
|
||||
},
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"jest": "^26.6.3"
|
||||
}
|
||||
}
|
@ -0,0 +1,87 @@
|
||||
// This is the file to check whether your answer is correct or wrong.
|
||||
// Do not change anything in this file.
|
||||
|
||||
const assert = require('assert');
|
||||
const source = require('./index');
|
||||
|
||||
describe('[1] Count letters in a sentence.', function() {
|
||||
const validLetter = 'o';
|
||||
const invalidLetter = 'abc';
|
||||
const sentence = 'The quick brown fox jumps over the lazy dog';
|
||||
|
||||
it('Invalid letter returns undefined.', function() {
|
||||
const result = source.countLetter(invalidLetter, sentence);
|
||||
expect(result).toBeUndefined();
|
||||
});
|
||||
|
||||
it('Valid letter returns number of occurrence.', function() {
|
||||
const result = source.countLetter(validLetter, sentence);
|
||||
expect(result).toEqual(4);
|
||||
});
|
||||
});
|
||||
|
||||
describe('[2] Check isogram.', function() {
|
||||
const isogram = 'Machine';
|
||||
const notIsogram = 'Hello';
|
||||
|
||||
it('Returns true if a word is an isogram.', function() {
|
||||
const result = source.isIsogram(isogram);
|
||||
expect(result).toEqual(true);
|
||||
});
|
||||
|
||||
it('Returns false if a word is not an isogram.', function() {
|
||||
const result = source.isIsogram(notIsogram);
|
||||
expect(result).toEqual(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('[3] Purchase goods.', function() {
|
||||
const price = 109.4356;
|
||||
const discountedPrice = price * 0.8;
|
||||
const roundedPrice = discountedPrice.toFixed(2);
|
||||
|
||||
it('Returns undefined for students aged below 13.', function() {
|
||||
const result = source.purchase(12, price);
|
||||
expect(result).toBeUndefined();
|
||||
});
|
||||
|
||||
it('Returns discounted price (rounded off) for students aged 13 to 21.', function() {
|
||||
const result = source.purchase(15, price);
|
||||
expect(result).toEqual(roundedPrice);
|
||||
});
|
||||
|
||||
it('Returns discounted price (rounded off) for senior citizens.', function() {
|
||||
const result = source.purchase(72, price);
|
||||
expect(result).toEqual(roundedPrice);
|
||||
});
|
||||
|
||||
it('Returns price (rounded off) for people aged 22 to 64.', function() {
|
||||
const result = source.purchase(34, price);
|
||||
expect(result).toEqual(price.toFixed(2));
|
||||
});
|
||||
});
|
||||
|
||||
describe('[4] Find hot categories.', function() {
|
||||
const items = [
|
||||
{ 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' }
|
||||
];
|
||||
|
||||
it('Returns item categories without stocks.', function() {
|
||||
const result = source.findHotCategories(items);
|
||||
expect(result).toEqual(['toiletries', 'gadgets']);
|
||||
});
|
||||
});
|
||||
|
||||
describe('[5] Find flying voters.', function() {
|
||||
const candidateA = ['LIWf1l', 'V2hjZH', 'rDmZns', 'PvaRBI', 'i7Xw6C', 'NPhm2m'];
|
||||
const candidateB = ['kcUtuu', 'LLeUTl', 'r04Zsl', '84EqYo', 'V2hjZH', 'LIWf1l'];
|
||||
|
||||
it('Returns the array of flying voters.', function() {
|
||||
const result = source.findFlyingVoters(candidateA, candidateB);
|
||||
expect(result).toEqual(['LIWf1l', 'V2hjZH']);
|
||||
});
|
||||
});
|
@ -1 +0,0 @@
|
||||
Subproject commit a9253e2e4f52925478c882822da78456d1526540
|
Loading…
Reference in New Issue