Skip to content

Commit e080744

Browse files
committed
js problems
0 parents  commit e080744

12 files changed

+222
-0
lines changed

FlattenArray.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function flattenArray(arr) {
2+
let result = [];
3+
for (let i = 0; i < arr.length; i++) {
4+
if (Array.isArray(arr[i])) {
5+
result = result.concat(flattenArray(arr[i]))
6+
}
7+
else {
8+
result.push(arr[i])
9+
}
10+
}
11+
return result
12+
}
13+
14+
15+
const nestedArray = [1, [2, [3, 4], 5], 6, [7, 8]];
16+
const flattenedArray = flattenArray(nestedArray);
17+
console.log(flattenedArray);

decodestring.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function twoSum(nums, target) {
2+
let numObj = {};
3+
for (let i = 0; i < nums.length; i++) {
4+
let complement = target - nums[i];
5+
if (numObj[complement] !== undefined) {
6+
return [numObj[complement], i];
7+
}
8+
numObj[nums[i]] = i;
9+
}
10+
}
11+
12+
13+
14+
let target=12;
15+
let nums=[1,4,7,9,2,4,5,6,7,5,6]
16+
17+
console.log(twoSum(nums,target));

encryptString.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
function encrypt(input) {
2+
if (!input) return ""
3+
let result = '';
4+
let count = 1;
5+
6+
for (let i = 0; i < input.length; i++) {
7+
if (input[i] === input[i + 1]) {
8+
count++
9+
}
10+
else {
11+
result += count + input[i]
12+
count = 1
13+
}
14+
15+
}
16+
return result;
17+
}
18+
19+
const input = "aabbbcdda";
20+
const encrypted = encrypt(input);
21+
console.log(encrypted); // Output: "2a3b1c2d1a"

maxProfit.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function maxProfit(prices) {
2+
let globalValue = 0;
3+
4+
for (let i = 0; i < prices.length - 1; i++) {
5+
for (let j = i + 1; j < prices.length; j++) {
6+
const profit = prices[j] - prices[i];
7+
if (profit > globalValue) globalValue = profit
8+
9+
10+
}
11+
12+
}
13+
return globalValue
14+
}
15+
const prices = [7, 1, 5, 3, 6, 4]
16+
console.log(maxProfit(prices))

maxmin.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
function miniMaxSum(arr) {
2+
// Write your code here
3+
let min = arr[0];
4+
let max = arr[0];
5+
let sum = arr[0];
6+
7+
for (let i = 1; i < arr.length; i++) {
8+
const num = arr[i]
9+
if (num < min) {
10+
min = num;
11+
} else if (num > max) {
12+
max = num;
13+
}
14+
sum += num;
15+
}
16+
const minSum = sum - max;
17+
const maxSum = sum - min;
18+
console.log(minSum, maxSum)
19+
20+
21+
}
22+
23+
24+
miniMaxSum([1, 2, 4, 5, 6, 9]);

nonrepeatstring.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function nonRepeat(str) {
2+
for (let i = 0; i < str.length; i++) {
3+
let char = str[i]
4+
if (str.indexOf(char) == i && str.indexOf(char, i + 1) == -1) {
5+
return char;
6+
}
7+
}
8+
return "Nothing found"
9+
}
10+
11+
let str = "abcdabcefef";
12+
13+
console.log(nonRepeat(str))
14+

palingdramString.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function stringPalingdram(str) {
2+
let len = str.length;
3+
for (let i = 0; i < str.length / 2; i++) {
4+
console.log(str)
5+
if (str[i] !== str[len - 1 - i]) {
6+
return "Your String is not Palingdram"
7+
}
8+
9+
return "Strings are Palingdram"
10+
}
11+
12+
}
13+
14+
15+
const string = "racecarq"
16+
console.log(stringPalingdram(string))

plusminus.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
function plusMinus(arr) {
2+
let positiveCount = 0;
3+
let negativeCount = 0;
4+
let zeroCount = 0;
5+
const n = arr.length;
6+
7+
for (let i = 0; i < n; i++) {
8+
if (arr[i] > 0) {
9+
positiveCount++;
10+
} else if (arr[i] < 0) {
11+
negativeCount++;
12+
} else {
13+
zeroCount++;
14+
}
15+
}
16+
17+
const positiveRatio = positiveCount / n;
18+
const negativeRatio = negativeCount / n;
19+
const zeroRatio = zeroCount / n;
20+
21+
console.log(positiveRatio.toFixed(6));
22+
console.log(negativeRatio.toFixed(6));
23+
console.log(zeroRatio.toFixed(6));
24+
}
25+
26+
plusMinus([1, 1, 0, -1 - 1]);

smallestMissingNum.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
function findSmallestMissingPositive(A) {
2+
3+
const positiveSet = new Set(A.filter(x => x > 0))
4+
5+
for (let i = 1; i < A.length + 1; i++) {
6+
if (!positiveSet.has(i)) {
7+
return i;
8+
}
9+
}
10+
11+
return A.length + 1
12+
}
13+
14+
15+
// Test cases
16+
const array1 = [1, 3, 6, 5, 4, 1, 2];
17+
const array2 = [1, 2, 3];
18+
const array3 = [-1, -3];
19+
20+
console.log("Smallest missing positive for array1:", findSmallestMissingPositive(array1)); // Output: 5

twobit.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function twoBit(num) {
2+
let nu = num.split("0").toString(2)
3+
return nu
4+
}
5+
6+
const num = "100010010001";
7+
console.log(twoBit(num))

0 commit comments

Comments
 (0)