Skip to content

Commit 2f0d9ac

Browse files
committed
added practice questiongit add .
1 parent ca24cb2 commit 2f0d9ac

26 files changed

+410
-0
lines changed
File renamed without changes.
File renamed without changes.
File renamed without changes.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Given an integer n, return all the numbers in the range [1, n] sorted in lexicographical order.
2+
3+
// You must write an algorithm that runs in O(n) time and uses O(1) extra space.
4+
5+
/**
6+
* @param {number} n
7+
* @return {number[]}
8+
*/
9+
var lexicalOrder = function(n) {
10+
const result=[];
11+
let current =1;
12+
for(let i=0;i<n;i++){
13+
result.push(current);
14+
if(current*10<=n){
15+
current *=10;
16+
}else{
17+
while(current%10 ===9 || current+1>n){
18+
current=Math.floor(current/10)
19+
}
20+
current ++
21+
}
22+
}
23+
return result;
24+
};

self/Fibonicc_series.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
function generateFibonacciSeries(terms) {
2+
// Check if the number of terms is less than 1
3+
if (terms <= 0) {
4+
return [];
5+
}
6+
7+
// Initialize the first two terms of the Fibonacci series
8+
let series = [0];
9+
if (terms > 1) {
10+
series.push(1);
11+
}
12+
13+
// Generate the remaining terms of the series
14+
for (let i = 2; i < terms; i++) {
15+
let nextTerm = series[i - 1] + series[i - 2];
16+
series.push(nextTerm);
17+
}
18+
19+
return series;
20+
}
21+
22+
// Example usage
23+
let numberOfTerms = 10; // Change this value to generate more or fewer terms
24+
console.log(generateFibonacciSeries(numberOfTerms));

self/Patten Js.js

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/**---------
2+
****
3+
****
4+
****
5+
****
6+
------------*/
7+
8+
let n = 4
9+
for (let row = 1; row <= n; row += 1) {
10+
let str = "";
11+
for (let row = 1; row <= n; row++) {
12+
str += "*"
13+
}
14+
console.log(str)
15+
}
16+
17+
/** --------------
18+
*
19+
**
20+
***
21+
****
22+
*****
23+
----------------*/
24+
let a = 5;
25+
for (let row = 1; row <= a; row++) {
26+
let str = "*"
27+
console.log(str.repeat(row))
28+
}
29+
30+
let b = 4;
31+
for (let col = 1; col >= b; col++) {
32+
let str = "*";
33+
let space = ' ';
34+
console.log(space.repeat((b - 1)) + str.repeat(col))
35+
}
36+
37+
/** --------------
38+
39+
*
40+
**
41+
***
42+
****
43+
*****
44+
******
45+
*******
46+
********
47+
*********
48+
49+
50+
----------------*/
51+
52+
let str5 = "";
53+
let y = 10;
54+
let x = 10;
55+
for (let i = 1; i < y; i++) {
56+
for (let j = 1; j < x; j++) {
57+
if (i <= y / 2 && j >= (y / 2) - (i - 1) && j <= (y / 2) + (i - 1)) {
58+
str5 = str5.concat("*");
59+
} else if (i >= y / 2
60+
&& j > ((y / 2) - i) * (-1)
61+
&& j < (y - ((y / 2) - i) * (-1))) {
62+
str5 = str5.concat("*");
63+
}
64+
else {
65+
str5 = str5.concat(" ");
66+
}
67+
}
68+
str5 = str5.concat("\n");
69+
}
70+
71+
console.log(str5)
72+
73+
/**------------------------
74+
75+
76+
*
77+
* *
78+
* * *
79+
* * * *
80+
* * * * *
81+
82+
---------------------------*/
83+
84+
var h = '';
85+
var f = 5;
86+
var m = (f - 1);
87+
for (i = 1; i <= n; i++) {
88+
h = h.trim();
89+
h = ' '.repeat(m) + h + (i > 1 ? ' ' : '') + '*';
90+
console.log(h);
91+
m--;
92+
}

self/Pattern2.js

Whitespace-only changes.

self/SearchInAray.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Javascript program to implement linear
2+
// search in unsorted array
3+
4+
5+
// Function to implement search operation
6+
function findElement( arr, n, key)
7+
{
8+
let i;
9+
for (i = 0; i < n; i++)
10+
if (arr[i] == key)
11+
return i;
12+
13+
return -1;
14+
}
15+
16+
17+
let arr = [12, 34, 10, 6, 40];
18+
let n = arr.length;
19+
20+
// Using a last element as search element
21+
let key = 40;
22+
let position = findElement(arr, n, key);
23+
24+
if (position == - 1)
25+
console.log("Element not found");
26+
else
27+
console.log("Element Found at Position: "
28+
+ (position + 1));

self/Sleep.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**Given a positive integer millis, write an asynchronous function that sleeps for millis milliseconds. It can resolve any value.*/
2+
async function sleep(millis) {
3+
return new Promise(resolve => {
4+
setTimeout(resolve(200), millis)
5+
});
6+
}
7+
8+
sleep(200).then(result => console.log(result));

self/addTwoSorted.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
function mergedArry(arr1, arr2) {
3+
4+
const mergedArry = arr2.concat(arr1)
5+
mergedArry.sort((a, b) => a - b)
6+
return mergedArry
7+
}
8+
const arr1 = [1, 2, 4]
9+
const arr2 = [1, 3, 4]
10+
const output = (mergedArry(arr1, arr2))
11+
console.log(output)

0 commit comments

Comments
 (0)