Skip to content

Commit 710a8a2

Browse files
code formatting
1 parent 4cd0efa commit 710a8a2

20 files changed

+163
-146
lines changed

.DS_Store

6 KB
Binary file not shown.

Arrays-Questions/diagonal-array-sum.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,20 @@
33
// 4 5 6
44
// 9 8 9
55

6-
let arr = [[1,2,3],[4,5,6],[9,8,9]];
6+
let arr = [[1, 2, 3], [4, 5, 6], [9, 8, 9]];
77

88
const getDiagonalSums = (matrixArr) => {
99
let sum1 = 0;
1010
let sum2 = 0;
11-
for(let i=0; i<matrixArr.length; i++)
12-
{
11+
for (let i = 0; i < matrixArr.length; i++) {
1312
//let firstIndex = 0;
14-
let lastIndex = matrixArr[i].length-1;
15-
for(let j=0; j<matrixArr[i].length; j++)
16-
{
13+
let lastIndex = matrixArr[i].length - 1;
14+
for (let j = 0; j < matrixArr[i].length; j++) {
1715
sum1 += matrixArr[j][j];
18-
sum2 += matrixArr[j][lastIndex-j];
16+
sum2 += matrixArr[j][lastIndex - j];
1917
}
2018
break;
2119
}
22-
return [sum1,sum2];
20+
return [sum1, sum2];
2321
}
2422
console.log(getDiagonalSums(arr))

Arrays-Questions/duplicate-from-array.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
let arr = [1,2,1,3,2,1,2,3,4,5];
1+
let arr = [1, 2, 1, 3, 2, 1, 2, 3, 4, 5];
22

33
const getDuplicateFromArr = (inputArr) => {
44
let resultArr = [];
55
let obj = {};
6-
for(let i=0; i<inputArr.length; i++)
7-
{
8-
if(obj[inputArr[i]]){
9-
if(!resultArr.includes(inputArr[i])){
6+
for (let i = 0; i < inputArr.length; i++) {
7+
if (obj[inputArr[i]]) {
8+
if (!resultArr.includes(inputArr[i])) {
109
resultArr.push(inputArr[i]);
1110
}
12-
}else{
11+
} else {
1312
obj[inputArr[i]] = 1;
1413
}
1514
}

Arrays-Questions/factorial-of-num.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
// Write a program to prints factorial of any number ?
22
const getFactorial = (inputNum) => {
33
let result = 1;
4-
for(let i=1; i<=inputNum; i++)
5-
{
4+
for (let i = 1; i <= inputNum; i++) {
65
result *= i;
76
}
87
return result;

Arrays-Questions/flatten-array.js

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,33 @@
11
//Write a program to flat Matrix array [1,[2,3],[4,5,6,[7,8]]] = [1,2,3,4,5,6,7,8]
22

3-
const flatArray = (matrixArray,innerArray=[],startFrom=0,resultArr=[]) => {
3+
const flatArray = (matrixArray, innerArray = [], startFrom = 0, resultArr = []) => {
44
let count = 0;
55
let loopArray = (innerArray.length === 0) ? matrixArray : innerArray;
66
let tempStartFrom = startFrom;
77
startFrom = (innerArray.length === 0) ? tempStartFrom : 0;
8-
for(let i=startFrom; i<loopArray.length; i++)
9-
{
10-
if(Array.isArray(loopArray[i])){
11-
return flatArray(matrixArray,matrixArray[i],i,resultArr);
8+
for (let i = startFrom; i < loopArray.length; i++) {
9+
if (Array.isArray(loopArray[i])) {
10+
return flatArray(matrixArray, matrixArray[i], i, resultArr);
1211
}
13-
else{
12+
else {
1413
resultArr.push(loopArray[i]);
1514
}
1615
}
1716

1817

1918
count++;
2019

21-
if(count == 3) return false;
20+
if (count == 3) return false;
2221

23-
if(tempStartFrom != 0 && tempStartFrom < matrixArray.length-1)
24-
{
25-
console.log(matrixArray,[],tempStartFrom,resultArr)
26-
return flatArray(matrixArray,[],tempStartFrom,resultArr);
22+
if (tempStartFrom != 0 && tempStartFrom < matrixArray.length - 1) {
23+
console.log(matrixArray, [], tempStartFrom, resultArr)
24+
return flatArray(matrixArray, [], tempStartFrom, resultArr);
2725
}
2826

2927

3028
return resultArr;
3129
}
3230

33-
let inputArr = [1,[2,3],[4,5,6,[7,8]]];
31+
let inputArr = [1, [2, 3], [4, 5, 6, [7, 8]]];
3432

3533
console.log(flatArray(inputArr));

Arrays-Questions/is-prime-number.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
// Write a program for check number is prime or not ?
22
const isPrime = (inputNum) => {
33
let result = true;
4-
for(let i=2; i<inputNum; i++)
5-
{
6-
if(inputNum%i === 0)
4+
for (let i = 2; i < inputNum; i++) {
5+
if (inputNum % i === 0)
76
result = false;
8-
break;
7+
break;
98
}
109
return result;
1110
}
Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,24 @@
11
//Write a program to Multiply two numbers without using * sign.
2-
const multiplyTwoNums = (x,y) => {
2+
const multiplyTwoNums = (x, y) => {
33
//Theory: Multiply is nothing but equal to SUM
44
x = parseFloat(x);
55
y = parseFloat(y);
66
let result = 0;
7-
for(let i=0; i<y; i++)
8-
{
7+
for (let i = 0; i < y; i++) {
98
result += x;
109
}
1110
return result;
1211
}
1312

1413
let x = process.argv[2];
1514
let y = process.argv[3];
16-
if(x == '' || x == null)
17-
{
15+
if (x == '' || x == null) {
1816
console.error('x value is required');
1917
return false;
2018
}
21-
if(y == '' || y == null)
22-
{
19+
if (y == '' || y == null) {
2320
console.error('y value is required');
2421
return false;
2522
}
26-
console.log('Result:',multiplyTwoNums(x,y));
23+
console.log('Result:', multiplyTwoNums(x, y));
2724

Frequency-Counter/anagram-frequency-check.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
// Given 2 strings, write a function to determine if the second string is an anagram of the first. An anagram is a word, phrase, or name formed by rearranging the letters of another, such as cinema, formed from iceman.
1+
// Given 2 strings, write a function to determine if the second string is an anagram of the first. An anagram is a word,
2+
// phrase, or name formed by rearranging the letters of another, such as cinema, formed from iceman.
23

34
const validAnagram = (str1, str2) => {
45
let result = true

Frequency-Counter/compare-two-arrays-optimized.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
// Write a function called the same. Which accepts 2 arrays. The function should return true if every value in the array has its corresponding value squared in the second array. The frequency of values must be the same.
1+
// Write a function called the same. Which accepts 2 arrays. The function should return true if every value in the array
2+
// has its corresponding value squared in the second array. The frequency of values must be the same.
23

34
const arr1 = [1, 2, 3] // Frequency {"1":1, "2":1, "3":1}
45
const arr2 = [1, 9, 4] // Frequency {"1":1, "9":1, "4":1}

Frequency-Counter/compare-two-arrays.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
// Write a function called the same. Which accepts 2 arrays. The function should return true if every value in the array has its corresponding value squared in the second array. The frequency of values must be the same.
1+
// Write a function called the same. Which accepts 2 arrays. The function should return true if every value in the array
2+
// has its corresponding value squared in the second array. The frequency of values must be the same.
23

34
const arr1 = [1, 2, 3]
45
const arr2 = [1, 9, 4]

0 commit comments

Comments
 (0)