Skip to content

Commit ba4291e

Browse files
problem solving part 1
0 parents  commit ba4291e

14 files changed

+326
-0
lines changed

NumberLineJumps.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Complete the 'kangaroo' function below.
3+
*
4+
* The function is expected to return a STRING.
5+
* The function accepts following parameters:
6+
* 1. INTEGER x1
7+
* 2. INTEGER v1
8+
* 3. INTEGER x2
9+
* 4. INTEGER v2
10+
*/
11+
12+
function kangaroo(x1, v1, x2, v2) {
13+
// Write your code here
14+
let temp;
15+
if((x2 > x1) && v2 > v1)
16+
{
17+
return 'YES';
18+
}
19+
for(let i=v1, j=v2; i<=10000, j<=10000; i+=v1, j+=v2){
20+
if((x1 += i) == (x2 += j)){
21+
temp = 1;
22+
break;
23+
}else continue;
24+
}
25+
if(temp == 1){
26+
return 'YES';
27+
}else return 'NO';
28+
}

aVeryBigSum.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
* Complete the 'aVeryBigSum' function below.
3+
*
4+
* The function is expected to return a LONG_INTEGER.
5+
* The function accepts LONG_INTEGER_ARRAY ar as parameter.
6+
*/
7+
8+
function aVeryBigSum(ar) {
9+
// Write your code here
10+
let res;
11+
12+
res = ar.reduce((a, b) => a + b, 0);
13+
return res;
14+
}

appleAndOrange.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Complete the 'countApplesAndOranges' function below.
3+
*
4+
* The function accepts following parameters:
5+
* 1. INTEGER s
6+
* 2. INTEGER t
7+
* 3. INTEGER a
8+
* 4. INTEGER b
9+
* 5. INTEGER_ARRAY apples
10+
* 6. INTEGER_ARRAY oranges
11+
*/
12+
13+
function countApplesAndOranges(s, t, a, b, apples, oranges) {
14+
// Write your code here
15+
let k = 0;
16+
let j = 0;
17+
let i;
18+
19+
for (i = 0; i < apples.length; i++) {
20+
if (a + apples[i] >= s && a + apples[i] <= t) {
21+
++k;
22+
}
23+
}
24+
for (i = 0; i < oranges.length; i++) {
25+
if (b + oranges[i] >= s && b + oranges[i] <= t) {
26+
++j;
27+
}
28+
}
29+
console.log(k);
30+
console.log(j);
31+
}

betweenTwoSets.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Complete the 'getTotalX' function below.
3+
*
4+
* The function is expected to return an INTEGER.
5+
* The function accepts following parameters:
6+
* 1. INTEGER_ARRAY a
7+
* 2. INTEGER_ARRAY b
8+
*/
9+
10+
function getTotalX(a, b) {
11+
// Write your code here
12+
let res = 0;
13+
let i = 1;
14+
let cloneA = a.splice(1, a.length);
15+
while(a[0] * i <= b[0]) {
16+
if(
17+
cloneA.every(item => (a[0] * i) % item === 0)
18+
&&
19+
b.every(item => item % (a[0] * i) === 0)
20+
) {
21+
res++;
22+
}i++;
23+
}
24+
return res;
25+
}

birthdayCakeCandles.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Complete the 'birthdayCakeCandles' function below.
3+
*
4+
* The function is expected to return an INTEGER.
5+
* The function accepts INTEGER_ARRAY candles as parameter.
6+
*/
7+
8+
function birthdayCakeCandles(candles) {
9+
// Write your code here
10+
let count = 0;
11+
let max = 0;
12+
for(let i = 0; i < candles.length ; i++)
13+
{
14+
let num = candles[i];
15+
if(num > max)
16+
{
17+
max = num;
18+
count = 1;
19+
}else if(max == num)
20+
{
21+
count++;
22+
}
23+
}
24+
return count;
25+
26+
}

breakingTheRecords.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Complete the 'breakingRecords' function below.
3+
*
4+
* The function is expected to return an INTEGER_ARRAY.
5+
* The function accepts INTEGER_ARRAY scores as parameter.
6+
*/
7+
8+
function breakingRecords(scores) {
9+
// Write your code here
10+
let tempMax = scores[0];
11+
let tempMin = scores[0];
12+
let countMax = 0;
13+
let countMin = 0;
14+
let resTab = [];
15+
for(let i=1; i<scores.length; i++){
16+
if(tempMax < scores[i]){
17+
tempMax = scores[i];
18+
countMax++;
19+
}
20+
}
21+
resTab.push(countMax);
22+
for(let i=1; i<scores.length; i++){
23+
if(tempMin > scores[i]){
24+
tempMin = scores[i];
25+
countMin++;
26+
}
27+
}
28+
resTab.push(countMin);
29+
return resTab;
30+
}

compareTheTriplets.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function compareTriplets(a, b) {
2+
let ap = 0;
3+
let bp = 0;
4+
let resTab = [];
5+
for(let i=0; i<=a.length; i++){
6+
if(a[i] < b[i]){
7+
bp++;
8+
} else if(a[i] > b[i]) {
9+
ap++;
10+
}
11+
}
12+
resTab.push(ap);
13+
resTab.push(bp);
14+
return resTab;
15+
}

diagonalDifference.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Complete the 'diagonalDifference' function below.
3+
*
4+
* The function is expected to return an INTEGER.
5+
* The function accepts 2D_INTEGER_ARRAY arr as parameter.
6+
*/
7+
8+
function diagonalDifference(arr) {
9+
// Write your code here
10+
let s1=0;
11+
let s2=0;
12+
let n=arr.length;
13+
for(let i=0; i<n; i++){
14+
for(let j=0; j<n; j++) {
15+
16+
if(i === j){
17+
s1 += arr[i][j];
18+
}
19+
if(i+j == n-1){
20+
s2 += arr[i][j];
21+
}
22+
}
23+
}
24+
25+
26+
return Math.abs(s1 - s2);
27+
}

miniMaxSum.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Complete the 'miniMaxSum' function below.
3+
*
4+
* The function accepts INTEGER_ARRAY arr as parameter.
5+
*/
6+
7+
function miniMaxSum(arr) {
8+
// Write your code here
9+
let arr2 = [...arr];
10+
let min = Math.min(...arr);
11+
let max = Math.max(...arr);
12+
13+
let indexOfMin = arr.indexOf(min);
14+
let indexOfMax = arr.indexOf(max);
15+
16+
let tabMax = arr.splice(indexOfMin, 1);
17+
let resMax = arr.reduce((a, b) => a+b, 0);
18+
19+
let tabMin = arr2.splice(indexOfMax, 1);
20+
let resMin = arr2.reduce((a, b) => a+b, 0);
21+
22+
console.log(resMin, resMax);
23+
}

plusMinus.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Complete the 'plusMinus' function below.
3+
*
4+
* The function accepts INTEGER_ARRAY arr as parameter.
5+
*/
6+
7+
function plusMinus(arr) {
8+
// Write your code here
9+
let n = arr.length;
10+
let p=0;
11+
let ng=0;
12+
let z=0;
13+
for(let i=0; i<=n; i++){
14+
if(arr[i] > 0){
15+
p++;
16+
}else if(arr[i] < 0){
17+
ng++;
18+
}else if(arr[i] == 0){
19+
z++;
20+
}
21+
}
22+
23+
let r1 = (p/n).toFixed(6);
24+
let r2 = (ng/n).toFixed(6);
25+
let r3 = (z/n).toFixed(6);
26+
console.log(r1 + "\n" + r2 +"\n" + r3);
27+
28+
}

solveMeFirst.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
function solveMeFirst(a, b) {
2+
// Hint: Type return a+b below
3+
return a+b;
4+
}

stairCase.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
* Complete the 'staircase' function below.
3+
*
4+
* The function accepts INTEGER n as parameter.
5+
*/
6+
7+
function staircase(n) {
8+
// Write your code here
9+
let star = "#";
10+
let space = " ";
11+
for(let i=(n-1), j=1; i>=0 && j<=n; i--, j++){
12+
console.log(space.repeat(i) + star.repeat(j));
13+
}
14+
15+
}

subarrayDivision.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Complete the 'birthday' function below.
3+
*
4+
* The function is expected to return an INTEGER.
5+
* The function accepts following parameters:
6+
* 1. INTEGER_ARRAY s
7+
* 2. INTEGER d
8+
* 3. INTEGER m
9+
*/
10+
11+
function birthday(s, d, m) {
12+
// Write your code here
13+
let num = s;
14+
let nums = [];
15+
let count = 0;
16+
const add = (arr) => arr.reduce((a, b) => a + b, 0);
17+
for (let i = 0; i < s.length; i++) {
18+
let tempTab = num.slice(0 + i, m + i);
19+
nums.push(tempTab);
20+
}
21+
if(num.length===1 && num[0]===d){
22+
count++;
23+
}else{
24+
nums.forEach((item) => {
25+
if (add(item) === d) {
26+
count++;
27+
}
28+
});
29+
}
30+
31+
32+
return count;
33+
}

timeConversion.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Complete the 'timeConversion' function below.
3+
*
4+
* The function is expected to return a STRING.
5+
* The function accepts STRING s as parameter.
6+
*/
7+
8+
function timeConversion(s) {
9+
// Write your code here
10+
let [time, part] = [s.substring(0, s.length - 2), s.substring(s.length - 2)]
11+
12+
time = time.split(":").map(Number)
13+
14+
if (part === "PM" && time[0] === 12) time[0] = 12
15+
16+
if (part === "PM" && time[0] !== 12) time[0] = (time[0] + 12) % 24
17+
18+
if (part === "AM" && time[0] === 12) time[0] = 0
19+
20+
return time
21+
22+
.map(String)
23+
24+
.map(s => s.padStart(2, "0"))
25+
26+
.join(":")
27+
}

0 commit comments

Comments
 (0)