Skip to content
1 change: 1 addition & 0 deletions requirement-sets/1-reading-writing-files/answer.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
count: 3
24 changes: 23 additions & 1 deletion requirement-sets/1-reading-writing-files/src/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,23 @@
// run the program
const fs = require('fs');

const data = fs.readFileSync('../input.txt');
const dataArray = data.toString().toUpperCase().trim().split('\r\n');
console.log(dataArray);

function checkPosition(data) {
let count = 0;
dataArray.forEach((el) => {
const counts = { W: 0, E: 0, N: 0, S: 0 };
el.split('').forEach((letter) => (counts[letter] += 1));
if (counts.W === counts.E && counts.S === counts.N) count += 1;
});
const output = `count: ${count}`;
fs.writeFile('../answer.txt', output, (err) => {
if (err) {
console.error(err);
}
});
return count;
}

console.log(checkPosition(data));
14 changes: 6 additions & 8 deletions requirement-sets/2-coding-tests/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ module.exports = {
env: {
commonjs: true,
es2021: true,
node: true
node: true,
jasmine: true,
},
extends: [
'standard'
],
extends: ['standard'],
parserOptions: {
ecmaVersion: 'latest'
ecmaVersion: 'latest',
},
rules: {
}
}
rules: {},
};
121 changes: 120 additions & 1 deletion requirement-sets/2-coding-tests/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions requirement-sets/2-coding-tests/spec/longest-sequence.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const longest_sequence = require('../src/longest-sequence.js');

describe('Longest sequence', () => {
it('scenario 1', () => {
expect(longest_sequence('dghhhmhmx')).toEqual({ h: 3 });
});
it('scenario 2', () => {
expect(longest_sequence('dhkkhhKKKt')).toEqual({ k: 3 });
});
it('scenario 3', () => {
expect(longest_sequence('aBbBadddadd')).toEqual({ b: 3 });
});
it('scenario 4', () => {
expect(longest_sequence('gggttrfdesdcxzzzzzzzzzz')).toEqual({ z: 10 });
});
it('scenario 5', () => {
expect(longest_sequence('jshetdrefdddesawwesdcxzwwwwwwwwww')).toEqual({
w: 10,
});
});
});
45 changes: 45 additions & 0 deletions requirement-sets/2-coding-tests/src/longest-sequence.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const longest_sequence = (str) => {
// the longest sequence
let longestSequence = { a: 0 };
// the current sequence
let currentSequence = {};

// loop
for (let i = 0; i < str.length - 1; i++) {
// 1. find the current char
const char = str[i].toLowerCase();
// 2. find the next char
const nextChar = str[i + 1].toLowerCase();

// reset the current sequence
if (!currentSequence[char]) {
currentSequence = {};
currentSequence[char] = 1;
}

// check the next char
if (char === nextChar) {
currentSequence[char] += 1;
} else {
// update the longest sequence if 1. is longer, 2. is the same and alphabetically earlier
// look at the values, compare them?
if (
Object.values(currentSequence)[0] > Object.values(longestSequence)[0]
) {
// if the current sequence is longer, we update longest sequence
longestSequence = { ...currentSequence };
// 2. is the same and alphabetically earlier
} else if (
Object.values(currentSequence)[0] === Object.values(longestSequence)[0]
) {
if (Object.keys(currentSequence)[0] < Object.keys(currentSequence)[0]) {
longestSequence = { ...currentSequence };
}
}
}
}

return longestSequence;
};

module.exports = longest_sequence;
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,19 @@ GET https://api.npms.io/v2/search/suggestions?q=react

*/

const axios = require('axios');

module.exports = async function countMajorVersionsAbove10() {
// TODO
try {
const { data } = await axios.get(
'https://api.npms.io/v2/search/suggestions?q=react'
);
const answer = data
.map((el) => el.package.version.split('.')[0])
.filter((el) => Number(el) >= 10).length;

return number
return answer;
} catch (err) {
console.log(err);
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,22 @@ GET https://api.npms.io/v2/search/suggestions?q=react
* the "name" of the package that has the oldest "date" value

*/
const axios = require('axios');

module.exports = async function oldestPackageName() {
// TODO
try {
const { data } = await axios.get(
'https://api.npms.io/v2/search/suggestions?q=react'
);

return name
const name = data
.map((el) => [el.package.date, el.package.name])
.reduce((previous, current) => {
return previous[0] < current[0] ? previous : current;
})[1];

return name;
} catch (err) {
console.log(err);
}
};
Loading