-
Notifications
You must be signed in to change notification settings - Fork 0
/
array&string.js
192 lines (164 loc) · 4.33 KB
/
array&string.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// prime number
function isPrime(n) {
if (n < 2) {
return false
}
for (let i = 2; i < n; i++) {
if (n % i === 0) {
return false
}
}
return true
}
// console.log(isPrime(1))
// finding vowel
function findVowel(str) {
const vowels = "aeiouAEIOU";
let a = [];
for (let char of str) {
if (vowels.includes(char)) {
a.push(char);
}
}
return a;
}
// console.log(findVowel("hello"));
// finding maximum counted value
function arrayOccurence(array) {
let maxEl = 0
let maxCount = 0
for (let i = 0; i < array.length; i++) {
let count = 0
for (let j = i + 1; j < array.length; j++) {
if (array[i] === array[j]) {
count++
}
}
if (maxCount < count) {
maxCount = count
maxEl = array[i]
}
}
return [maxCount, maxEl]
}
// console.log(arrayOccurence([3, 2, 3, 2, 3, 5, 6, 6, 7]));
// changing alphabets to another alphabet
function getAlphebts(str, n) {
let a = ''
for (let i = 0; i < str.length; i++) {
let char = str[i]
console.log(char, 'char');
let ascii = char.charCodeAt(0)
console.log(ascii);
let newAscii = ascii + n
if ((ascii >= 65 && ascii <= 90 && newAscii > 90) || (ascii >= 97 && ascii <= 122 && newAscii > 122)) {
newAscii -= 26
}
if (ascii < 65 || ascii > 90 && ascii < 97 || ascii > 122)
a += String.fromCharCode(newAscii)
}
return a
}
// console.log(getAlphebts('Hello, World!', 3));
function alphepets(str, n) {
const alphepets = 'qwertyuioplkjhgfdsazxcvbnm'
let a = ''
for (let i = 0; i < str.length; i++) {
let char = str[i].toLowerCase()
const index = alphepets.indexOf(char)
console.log(index, 'indez');
if (index !== -1) {
const newIndex = (index + n) % 26
const newChar = alphepets[newIndex]
a += char === str[i] ? newChar : newChar.toUpperCase()
} else {
a += str[i]
}
}
return a
}
// console.log(alphepets('Hello, World!', 3));
// return two values and equal target
function twoSum(arr,target){
let a= []
for(i=0;i<arr.length;i++){
for(j=i+1;j<arr.length;j++){
if(arr[i]+arr[j]===target){
console.log(arr[i],arr[j],target)
a.push(i,j)
return a
}
}
}
return a
}
// console.log(twoSum([2,3,1, 7,9,3],10));
// another method
function twoSum(arr, target) {
let a = []
const set = new Set()
for (i = 0; i < arr.length; i++) {
const num = arr[i]
const match = target - num
if (set.has(match)) {
return a.push(num, match)
} else {
set.add(match)
}
}
return a
}
console.log(twoSum([2, 3, 1, 7, 9, 3], 10));
// assigning same value in the end of the array
const array = [6, 3, 5, 6, 8, 1, 6]
let match = 6
function myFunction(array, match) {
for (let i = 0; i < array.length; i++) {
for (let j = array.length - 1; j > i; j--) {
if (array[i] == match) {
if (array[i] == array[j]) {
continue
} else {
let temp = array[i]
array[i] = array[j]
array[j] = temp
}
}
}
} return array
}
// console.log(myFunction(array, match))
// words swapping method Hello World ----> World Hello
function reverseWords(str) {
let a = ''
let b = ''
let space = false
for (let i = 0; i < str.length; i++) {
if (str[i] === ' ') {
space = true
} else {
if (!space) {
a += str[i]
} else {
b += str[i]
}
}
} return b + ' ' + a
}
// console.log(reverseWords('Hello World'));
// checking string is palindrome or not
function checkPalindrome(str){
let count = 0
for (let i = 0, j = str.length - 1; i < j; i++, j--) {
if (str[i] !== str[j]) {
count = 1
break
}
}
if (count === 0) {
console.log('palindrome');
} else {
console.log('not');
}
}
// console.log(checkPalindrome('malayalam'));