Scalable Capital challenge

This commit is contained in:
2025-05-10 16:00:33 +02:00
parent ee1866ec9f
commit aaa4af7ea8
3 changed files with 86 additions and 0 deletions

View File

@@ -0,0 +1,54 @@
// Write a function solution that gets String as the input and calculates
// how many times word 'BANANA' could exactly be written with the given letters.
function solution(S) {
let str = (' ' + S).slice(1);
let flag = true;
let b = 0;
let a = 0;
let n = 0;
let iterationCount = 0;
for (let i = 0; i < str.length; i++) {
let c = str[i];
switch (c) {
case 'B':
b++;
break;
case 'A':
a++;
break;
case 'N':
n++;
break;
}
}
while (flag) {
b = b - 1;
a = a - 3;
n = n - 2;
if (b < 0 || a < 0 || n < 0) {
flag = false;
} else {
iterationCount++;
}
}
return iterationCount;
}
// Function to check if a number is prime
function isPrime(num) {
if (num <= 1) return false;
if (num === 2) return true;
if (num % 2 === 0) return false;
for (let i = 3; i <= Math.sqrt(num); i += 2) {
if (num % i === 0) return false;
}
return true;
}
// Export for testing
module.exports = {isPrime, solution};

View File

@@ -0,0 +1,31 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { solution } from './banana.js';
test('checks NAABXXAN', () => {
assert.strictEqual(solution('NAABXXAN'), 1);
});
test('checks NAANAAXNABABYNNBZ', () => {
assert.strictEqual(solution('NAANAAXNABABYNNBZ'), 2);
});
test('checks QABAAAWOBL', () => {
assert.strictEqual(solution('QABAAAWOBL'), 0);
});
// test('checks if 4 is not prime', () => {
// assert.strictEqual(isPrime(4), false);
// });
//
// test('checks if 17 is prime', () => {
// assert.strictEqual(isPrime(17), true);
// });
//
// test('checks if 1 is not prime', () => {
// assert.strictEqual(isPrime(1), false);
// });
//
// test('checks if 0 is not prime', () => {
// assert.strictEqual(isPrime(0), false);
// });