Skip to content

Commit 895f6a2

Browse files
adding more exercises
nested loops
1 parent 8d75cbe commit 895f6a2

14 files changed

+353
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
function uniquePINCodes(input) {
2+
let upperLimit1 = parseInt(input[0]);
3+
let upperLimit2 = parseInt(input[1]);
4+
let upperLimit3 = parseInt(input[2]);
5+
6+
function isPrime(number) {
7+
if (number <= 1) return false;
8+
for (let i = 2; i * i <= number; i++) {
9+
if (number % i === 0) return false;
10+
}
11+
return true;
12+
}
13+
14+
for (let i = 2; i <= upperLimit1; i += 2) {
15+
for (let j = 2; j <= upperLimit2; j++) {
16+
if (isPrime(j) && j <= 7) {
17+
for (let k = 2; k <= upperLimit3; k += 2) {
18+
console.log(i + " " + j + " " + k);
19+
}
20+
}
21+
}
22+
}
23+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
function lettersCombinations(input) {
2+
let start = input[0].charCodeAt(0);
3+
let end = input[1].charCodeAt(0);
4+
let exclude = input[2].charCodeAt(0);
5+
let count = 0;
6+
let output = "";
7+
8+
for (let i = start; i <= end; i++) {
9+
if (i === exclude) continue;
10+
for (let j = start; j <= end; j++) {
11+
if (j === exclude) continue;
12+
for (let k = start; k <= end; k++) {
13+
if (k === exclude) continue;
14+
output += String.fromCharCode(i, j, k) + " ";
15+
count++;
16+
}
17+
}
18+
}
19+
20+
console.log(output + count);
21+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
function luckyNumbers(input) {
2+
let N = parseInt(input[0]);
3+
let output = "";
4+
5+
for (let a = 1; a <= 9; a++) {
6+
for (let b = 1; b <= 9; b++) {
7+
let firstTwoSum = a + b;
8+
if (N % firstTwoSum === 0) {
9+
for (let c = 1; c <= 9; c++) {
10+
for (let d = 1; d <= 9; d++) {
11+
if (firstTwoSum === (c + d)) {
12+
let number = "" + a + b + c + d;
13+
output += number + " ";
14+
}
15+
}
16+
}
17+
}
18+
}
19+
}
20+
21+
console.log(output.trim());
22+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
function carNumber(input) {
2+
let start = parseInt(input[0]);
3+
let end = parseInt(input[1]);
4+
let output = "";
5+
6+
function isValidNumber(a, b, c, d) {
7+
let startEndCondition = (a % 2 === 0 && d % 2 !== 0) || (a % 2 !== 0 && d % 2 === 0);
8+
let firstLastDigitCondition = a > d;
9+
let secondThirdSumCondition = (b + c) % 2 === 0;
10+
11+
return startEndCondition && firstLastDigitCondition && secondThirdSumCondition;
12+
}
13+
14+
for (let a = start; a <= end; a++) {
15+
for (let b = start; b <= end; b++) {
16+
for (let c = start; c <= end; c++) {
17+
for (let d = start; d <= end; d++) {
18+
if (isValidNumber(a, b, c, d)) {
19+
let number = "" + a + b + c + d;
20+
output += number + " ";
21+
}
22+
}
23+
}
24+
}
25+
}
26+
27+
console.log(output.trim());
28+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
function challengeTheWedding(input) {
2+
let maleCount = parseInt(input[0]);
3+
let femaleCount = parseInt(input[1]);
4+
let maxTables = parseInt(input[2]);
5+
6+
let output = "";
7+
let tables = 0;
8+
9+
for (let male = 1; male <= maleCount; male++) {
10+
for (let female = 1; female <= femaleCount; female++) {
11+
output += "(" + male + " <-> " + female + ") ";
12+
tables++;
13+
14+
if (tables === maxTables || (male === maleCount && female === femaleCount)) {
15+
console.log(output.trim());
16+
return;
17+
}
18+
}
19+
}
20+
21+
console.log(output.trim());
22+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
function weddingSeats(input) {
2+
let lastSector = input[0].charCodeAt(0);
3+
let firstSectorRows = parseInt(input[1]);
4+
let seatsOnOddRow = parseInt(input[2]);
5+
6+
let totalSeats = 0;
7+
8+
for (let sector = 'A'.charCodeAt(0); sector <= lastSector; sector++) {
9+
for (let row = 1; row <= firstSectorRows; row++) {
10+
let seatsOnRow = (row % 2 === 1) ? seatsOnOddRow : seatsOnOddRow + 2;
11+
for (let seat = 'a'.charCodeAt(0); seat < 'a'.charCodeAt(0) + seatsOnRow; seat++) {
12+
console.log(`${String.fromCharCode(sector)}${row}${String.fromCharCode(seat)}`);
13+
totalSeats++;
14+
}
15+
}
16+
firstSectorRows++;
17+
}
18+
19+
console.log(totalSeats);
20+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
function safePasswordsGenerator(input) {
2+
const A_MIN_ASCII = 35;
3+
const A_MAX_ASCII = 55;
4+
const B_MIN_ASCII = 64;
5+
const B_MAX_ASCII = 96;
6+
const PASS_SEP = "|";
7+
8+
let xMaxValue = parseInt(input[0]);
9+
let yMaxValue = parseInt(input[1]);
10+
let numPasswords = parseInt(input[2]);
11+
12+
let numPassGenerated = 0;
13+
let passTemplate_A_ASCII = A_MIN_ASCII;
14+
let passTemplate_B_ASCII = B_MIN_ASCII;
15+
let passTemplate_x_Value = 1;
16+
let output = '';
17+
18+
for (let x = 1; x <= xMaxValue; x++) {
19+
if (numPassGenerated === numPasswords) break;
20+
for (let passTemplate_y_Value = 1; passTemplate_y_Value <= yMaxValue; passTemplate_y_Value++) {
21+
22+
if (passTemplate_A_ASCII > A_MAX_ASCII) passTemplate_A_ASCII = A_MIN_ASCII;
23+
let passTemplate_A = String.fromCharCode(passTemplate_A_ASCII);
24+
if (passTemplate_B_ASCII > B_MAX_ASCII) passTemplate_B_ASCII = B_MIN_ASCII;
25+
let passTemplate_B = String.fromCharCode(passTemplate_B_ASCII);
26+
passTemplate_x_Value = x;
27+
28+
let curPassword = passTemplate_A + passTemplate_B + passTemplate_x_Value + passTemplate_y_Value + passTemplate_B + passTemplate_A + PASS_SEP;
29+
output += curPassword;
30+
31+
passTemplate_A_ASCII++;
32+
passTemplate_B_ASCII++;
33+
numPassGenerated++;
34+
if (numPassGenerated === numPasswords) break;
35+
}
36+
}
37+
38+
console.log(output)
39+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function secretDoorsLock(input) {
2+
let a = parseInt(input[0]);
3+
let b = parseInt(input[1]);
4+
let c = parseInt(input[2]);
5+
6+
for (let i = 1; i <= a; i++) {
7+
if (i % 2 === 0) {
8+
for (let j = 1; j <= b; j++) {
9+
if (j === 2 || j === 3 || j === 5 || j === 7) {
10+
for (let k = 1; k <= c; k++) {
11+
if (k % 2 === 0) {
12+
console.log(i + " " + j + " " + k);
13+
}
14+
}
15+
}
16+
}
17+
}
18+
}
19+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
function sumOfTwoNumbers(input) {
2+
let startNum = parseInt(input[0]);
3+
let endNum = parseInt(input[1]);
4+
let magicNum = parseInt(input[2]);
5+
6+
let counter = 0;
7+
let isFound = false;
8+
9+
for (let i = startNum; i <= endNum; i++) {
10+
for (let j = startNum; j <= endNum; j++) {
11+
counter++;
12+
if (i + j === magicNum) {
13+
console.log(`Combination N:${counter} (${i} + ${j} = ${i + j})`);
14+
isFound = true;
15+
break;
16+
}
17+
}
18+
if (isFound) {
19+
break;
20+
}
21+
}
22+
23+
if (!isFound) {
24+
console.log(`${counter} combinations - neither equals ${magicNum}`);
25+
}
26+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function profit(input) {
2+
let oneLev = parseInt(input[0]);
3+
let twoLev = parseInt(input[1]);
4+
let fiveLev = parseInt(input[2]);
5+
let sum = parseInt(input[3]);
6+
7+
for (let i = 0; i <= oneLev; i++) {
8+
for (let j = 0; j <= twoLev; j++) {
9+
for (let k = 0; k <= fiveLev; k++) {
10+
if (i * 1 + j * 2 + k * 5 === sum) {
11+
console.log(i + " * 1 lv. + " + j +" * 2 lv. + " + k +" * 5 lv. = " + sum + " lv.");
12+
}
13+
}
14+
}
15+
}
16+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
function happyCatParking(input) {
2+
let days = parseInt(input[0]);
3+
let hours = parseInt(input[1]);
4+
5+
let sumToPayAfterParking = 0;
6+
7+
for (let daysCount = 1; daysCount <= days; daysCount++) {
8+
9+
let currentSum = 0;
10+
11+
for (let hoursCount = 1; hoursCount <= hours; hoursCount++) {
12+
if (daysCount % 2 === 0 && hoursCount % 2 !== 0) {
13+
currentSum += 2.5;
14+
} else if (daysCount % 2 !== 0 && hoursCount % 2 === 0) {
15+
currentSum += 1.25;
16+
} else {
17+
currentSum += 1;
18+
}
19+
}
20+
console.log(`Day: ${daysCount} - ${currentSum.toFixed(2)} leva`);
21+
sumToPayAfterParking += currentSum;
22+
23+
}
24+
console.log(`Total: ${sumToPayAfterParking.toFixed(2)} leva`);
25+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
function theSongOfTheWheels(input) {
2+
let num = parseInt(input[0]);
3+
4+
let counter = 0;
5+
let g = 1;
6+
let h = 1;
7+
let j = 1;
8+
let k = 1;
9+
10+
for (let a = 1; a <= 9; a++) {
11+
for (let b = 1; b <= 9; b++) {
12+
for (let c = 1; c <= 9; c++) {
13+
for (let d = 1; d <= 9; d++) {
14+
if (num === a * b + c * d) {
15+
if (a < b && c > d) {
16+
process.stdout.write(`${a}${b}${c}${d} `);
17+
counter++;
18+
19+
if (counter === 4) {
20+
g = a;
21+
h = b;
22+
j = c;
23+
k = d;
24+
}
25+
26+
}
27+
}
28+
29+
}
30+
}
31+
}
32+
}
33+
34+
if (counter >= 4) {
35+
console.log(`\nPassword: ${g}${h}${j}${k}`);
36+
} else {
37+
console.log(`\nNo!`);
38+
}
39+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
function primePairs(input) {
2+
let firstPair = parseInt(input[0]);
3+
let secondPair = parseInt(input[1]);
4+
let firstPairLimit = parseInt(input[2]);
5+
let secondPairLimit = parseInt(input[3]);
6+
7+
for (let first = firstPair; first <= firstPair + firstPairLimit; first++) {
8+
for (let second = secondPair; second <= secondPair + secondPairLimit; second++) {
9+
let sqrt1 = Math.floor(Math.sqrt(first));
10+
let sqrt2 = Math.floor(Math.sqrt(second));
11+
12+
let isPrime1 = checkPrime(first, sqrt1);
13+
let isPrime2 = checkPrime(second, sqrt2);
14+
15+
if (isPrime1 && isPrime2) {
16+
console.log(`${first}${second}`);
17+
}
18+
}
19+
}
20+
21+
function checkPrime(number, sqrtNumber) {
22+
if (number < 2) {
23+
return false;
24+
}
25+
26+
for (let i = 2; i <= sqrtNumber; i++) {
27+
if (number % i == 0) {
28+
return false;
29+
}
30+
}
31+
32+
return true;
33+
}
34+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function passwordGenerator(input) {
2+
let n = parseInt(input[0]);
3+
let l = parseInt(input[1]);
4+
5+
let output = "";
6+
7+
for (let d1 = 1; d1 <= n; d1++) {
8+
for (let d2 = 1; d2 <= n; d2++) {
9+
for (let l1 = 'a'.charCodeAt(0); l1 < 'a'.charCodeAt(0) + l; l1++) {
10+
for (let l2 = 'a'.charCodeAt(0); l2 < 'a'.charCodeAt(0) + l; l2++) {
11+
for (let d3 = Math.max(d1, d2) + 1; d3 <= n; d3++) {
12+
output += `${d1}${d2}${String.fromCharCode(l1)}${String.fromCharCode(l2)}${d3} `;
13+
}
14+
}
15+
}
16+
}
17+
}
18+
console.log(output.trim());
19+
}

0 commit comments

Comments
 (0)