Skip to content
Open

sss #174

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/carbon-dating.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@ const HALF_LIFE_PERIOD = 5730;
* dateSample('WOOT!') => false
*
*/
function dateSample(/* sampleActivity */) {
// Remove line below and write your code here
throw new NotImplementedError('Not implemented');
function dateSample( sampleActivity ) {
if (typeof sampleActivity !== 'string' || sampleActivity === '' || !Number(sampleActivity) || Number(sampleActivity) <= 0 || Number(sampleActivity) > 15){
return false;
}
const k = Math.LN2 / HALF_LIFE_PERIOD;
const age = Math.log(MODERN_ACTIVITY / Number(sampleActivity)) / k;
return Math.ceil(age);
}

module.exports = {
Expand Down
14 changes: 10 additions & 4 deletions src/common-character-count.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,16 @@ const { NotImplementedError } = require('../lib');
* For s1 = "aabcc" and s2 = "adcaa", the output should be 3
* Strings have 3 common characters - 2 "a"s and 1 "c".
*/

function getCommonCharacterCount(/* s1, s2 */) {
// Remove line below and write your code here
throw new NotImplementedError('Not implemented');
function getCommonCharacterCount( s1, s2 ) {
return s1.split('').reduce((prev, value, i) => {
let index = s2.indexOf(value);
if ( index !== -1 ){
prev += 1;
s2 = s2.replace(s2[index], '');
return prev;
}
return prev;
},0)
}

module.exports = {
Expand Down
13 changes: 10 additions & 3 deletions src/count-cats.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,16 @@ const { NotImplementedError } = require('../lib');
* ]) => 3`
*
*/
function countCats(/* matrix */) {
// Remove line below and write your code here
throw new NotImplementedError('Not implemented');
function countCats( matrix ) {
let count = 0;
for (let i = 0; i < matrix.length; i++) {
for (let j = 0; j < matrix[i].length; j++) {
if (matrix[i][j] === "^^"){
count += 1;
}
}
}
return count;
}

module.exports = {
Expand Down
11 changes: 8 additions & 3 deletions src/delete-digit.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,14 @@ const { NotImplementedError } = require('../lib');
* For n = 152, the output should be 52
*
*/
function deleteDigit(/* n */) {
// Remove line below and write your code here
throw new NotImplementedError('Not implemented');
function deleteDigit( n ) {
let str = n.toString();
let arrSumm = [];
for (let i = 0; i < str.length; i++) {
let newSumm = Number(str.replace(str[i], ''));
arrSumm.push(newSumm);
}
return Math.max(...arrSumm)
}

module.exports = {
Expand Down
20 changes: 17 additions & 3 deletions src/dns-stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,23 @@ const { NotImplementedError } = require('../lib');
* }
*
*/
function getDNSStats(/* domains */) {
// Remove line below and write your code here
throw new NotImplementedError('Not implemented');
function getDNSStats( domains ) {
const objDNS = {};
for (let i = 0; i < domains.length; i++) {
let reverseArr = domains[i].split('.').reverse();
let dns = '';
for (let j = 0; j < reverseArr.length; j++) {
dns += `.${reverseArr[j]}`;
if(objDNS[dns]){
objDNS[dns]++;
}
else{
objDNS[dns] = 1;
}
}

}
return objDNS;
}

module.exports = {
Expand Down
15 changes: 12 additions & 3 deletions src/dream-team.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,18 @@ const { NotImplementedError } = require('../lib');
* createDreamTeam(['Olivia', 1111, 'Lily', 'Oscar', true, null]) => 'LOO'
*
*/
function createDreamTeam(/* members */) {
// Remove line below and write your code here
throw new NotImplementedError('Not implemented');
function createDreamTeam( members ) {
if(!Array.isArray(members)){
return false;
}

return members.reduce((prev, value) => {
if(typeof value === 'string'){
prev.push(value.trim().at(0).toUpperCase());
return prev;
}
return prev;
}, []).sort().join('');
}

module.exports = {
Expand Down
22 changes: 18 additions & 4 deletions src/encode-line.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// const { NotImplementedError } = require("../extensions/index.js");
const { NotImplementedError } = require('../lib');

/**
Expand All @@ -10,12 +11,25 @@ const { NotImplementedError } = require('../lib');
* For aabbbc should return 2a3bc
*
*/
function encodeLine(str) {
if(str.length === 0) return '';
let result = "";
let currentChar = str[0];
let count = 1;

function encodeLine(/* str */) {
// Remove line below and write your code here
throw new NotImplementedError('Not implemented');
for (let index = 1; index < str.length; index++) {
if (currentChar === str[index]) {
count++;
} else {
result += (count > 1 ? count : "") + currentChar;
currentChar = str[index];
count = 1;
}
}
result += (count > 1 ? count : "") + currentChar;
return String(result);
}

module.exports = {
encodeLine
encodeLine,
};
5 changes: 3 additions & 2 deletions src/get-email-domain.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ const { NotImplementedError } = require('../lib');
* For the input 'prettyandsimple@example.com', the output should be 'example.com'
*
*/
function getEmailDomain(/* email */) {
throw new NotImplementedError('Not implemented');
function getEmailDomain( email ) {
const index = email.lastIndexOf('@');
return email.slice(index +1 );
}

module.exports = {
Expand Down
7 changes: 4 additions & 3 deletions src/hanoi-tower.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ const { NotImplementedError } = require('../lib');
* calculateHanoi(9, 4308) => { turns: 511, seconds: 427 }
*
*/
function calculateHanoi(/* disksNumber, turnsSpeed */) {
// Remove line below and write your code here
throw new NotImplementedError('Not implemented');
function calculateHanoi( disksNumber, turnsSpeed ) {
const turns = Math.pow(2, disksNumber) - 1;
const seconds = Math.floor(turns / turnsSpeed * 3600);
return { turns, seconds };
}

module.exports = {
Expand Down
15 changes: 12 additions & 3 deletions src/matrix-elements-sum.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,18 @@ const { NotImplementedError } = require('../lib');
*
* The result should be 9
*/
function getMatrixElementsSum(/* matrix */) {
// Remove line below and write your code here
throw new NotImplementedError('Not implemented');
function getMatrixElementsSum( matrix ) {
let sum = 0;

for (let i = 0; i < matrix.length; i++) {
for (let j = 0; j < matrix[0].length; j++) {
if (i === 0 || matrix[i - 1][j] !== 0) {
sum += matrix[i][j];
}
}
}

return sum;
}

module.exports = {
Expand Down
16 changes: 13 additions & 3 deletions src/sum-digits.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,19 @@ const { NotImplementedError } = require('../lib');
* For 91, the result should be 1 (9 + 1 = 10, 1 + 0 = 1)
*
*/
function getSumOfDigits(/* n */) {
// Remove line below and write your code here
throw new NotImplementedError('Not implemented');
function getSumOfDigits( n ) {
let str = n.toString();
let sum = 0;

for (let i = 0; i < str.length; i++) {
sum += parseInt(str[i]);
}

if (sum <= 9) {
return sum;
}

return getSumOfDigits(sum);
}

module.exports = {
Expand Down