@@ -7059,6 +7059,218 @@ JavaScript offers several shorthand techniques to write code more concisely and
7059
7059
7060
7060
## Interview Questions
7061
7061
7062
+ Write a function that sorts an array of numbers Without Inbuilt Method.
7063
+
7064
+ ` ` ` javascript
7065
+ function sortArray (arr ) {
7066
+ for (let i = 0 ; i < arr .length ; i++ ) {
7067
+ for (let j = i + 1 ; j < arr .length ; j++ ) {
7068
+ if (arr[i] > arr[j]) {
7069
+ let temp = arr[i];
7070
+ arr[i] = arr[j];
7071
+ arr[j] = temp;
7072
+ }
7073
+ }
7074
+ }
7075
+ return arr;
7076
+ }
7077
+
7078
+ const numbers = [3 , 1 , 4 , 1 , 5 , 9 , 2 , 6 , 5 , 3 , 5 ];
7079
+ console .log (sortArray (numbers)); // Output: [1, 1, 2, 3, 3, 4, 5, 5, 5, 6, 9]
7080
+ ` ` `
7081
+
7082
+ Write a function that finds the maximum number in an array of numbers Without Inbuilt Method.
7083
+
7084
+ ` ` ` javascript
7085
+ function findMax (arr ) {
7086
+ let max = arr[0 ];
7087
+ for (let i = 1 ; i < arr .length ; i++ ) {
7088
+ if (arr[i] > max) {
7089
+ max = arr[i];
7090
+ }
7091
+ }
7092
+ return max;
7093
+ }
7094
+
7095
+ const numbers = [3 , 1 , 4 , 1 , 5 , 9 , 2 , 6 , 5 , 3 , 5 ];
7096
+ console .log (findMax (numbers)); // Output: 9
7097
+ ` ` `
7098
+
7099
+ Write a function that finds the minimum number in an array of numbers Without Inbuilt Method.
7100
+
7101
+ ` ` ` javascript
7102
+ function findMin (arr ) {
7103
+ let min = arr[0 ];
7104
+ for (let i = 1 ; i < arr .length ; i++ ) {
7105
+ if (arr[i] < min) {
7106
+ min = arr[i];
7107
+ }
7108
+ }
7109
+ return min;
7110
+ }
7111
+
7112
+ const numbers = [3 , 1 , 4 , 1 , 5 , 9 , 2 , 6 , 5 , 3 , 5 ];
7113
+ console .log (findMin (numbers)); // Output: 1
7114
+ ` ` `
7115
+
7116
+ Write a function that calculates the sum of an array of numbers Without Inbuilt Method.
7117
+
7118
+ ` ` ` javascript
7119
+ function sumArray (arr ) {
7120
+ let sum = 0 ;
7121
+ for (let i = 0 ; i < arr .length ; i++ ) {
7122
+ sum += arr[i];
7123
+ }
7124
+ return sum;
7125
+ }
7126
+
7127
+ const numbers = [3 , 1 , 4 , 1 , 5 , 9 , 2 , 6 , 5 , 3 , 5 ];
7128
+ console .log (sumArray (numbers)); // Output: 44
7129
+ ` ` `
7130
+
7131
+ Write a function that calculates the average of an array of numbers Without Inbuilt Method.
7132
+
7133
+ ` ` ` javascript
7134
+ function averageArray (arr ) {
7135
+ let sum = 0 ;
7136
+ for (let i = 0 ; i < arr .length ; i++ ) {
7137
+ sum += arr[i];
7138
+ }
7139
+ return sum / arr .length ;
7140
+ }
7141
+
7142
+ const numbers = [3 , 1 , 4 , 1 , 5 , 9 , 2 , 6 , 5 , 3 , 5 ];
7143
+ console .log (averageArray (numbers)); // Output: 4
7144
+ ` ` `
7145
+
7146
+ [Back to Top⤴️](#table-of-contents)
7147
+
7148
+ Write a function that checks if a number is prime Without Inbuilt Method.
7149
+
7150
+ ` ` ` javascript
7151
+ function isPrime (num ) {
7152
+ if (num <= 1 ) {
7153
+ return false ;
7154
+ }
7155
+ for (let i = 2 ; i <= Math .sqrt (num); i++ ) {
7156
+ if (num % i === 0 ) {
7157
+ return false ;
7158
+ }
7159
+ }
7160
+ return true ;
7161
+ }
7162
+
7163
+ console .log (isPrime (7 )); // Output: true
7164
+ console .log (isPrime (10 )); // Output: false
7165
+ ` ` `
7166
+
7167
+ Write a function that checks if a number is a palindrome Without Inbuilt Method.
7168
+
7169
+ ` ` ` javascript
7170
+ function isPalindrome (num ) {
7171
+ const str = num .toString ();
7172
+ for (let i = 0 ; i < str .length / 2 ; i++ ) {
7173
+ if (str[i] !== str[str .length - 1 - i]) {
7174
+ return false ;
7175
+ }
7176
+ }
7177
+ return true ;
7178
+ }
7179
+
7180
+ console .log (isPalindrome (121 )); // Output: true
7181
+ console .log (isPalindrome (123 )); // Output: false
7182
+ ` ` `
7183
+
7184
+ Write a function that checks if a string is a palindrome Without Inbuilt Method.
7185
+
7186
+ ` ` ` javascript
7187
+ function isPalindrome (str ) {
7188
+ for (let i = 0 ; i < str .length / 2 ; i++ ) {
7189
+ if (str[i] !== str[str .length - 1 - i]) {
7190
+ return false ;
7191
+ }
7192
+ }
7193
+ return true ;
7194
+ }
7195
+
7196
+ console .log (isPalindrome (' madam' )); // Output: true
7197
+ console .log (isPalindrome (' hello' )); // Output: false
7198
+ ` ` `
7199
+
7200
+ Write a function to find duplicate numbers in an Array Without Inbuilt Method.
7201
+
7202
+ ` ` ` javascript
7203
+ function findDuplicates (arr ) {
7204
+ const duplicates = [];
7205
+ for (let i = 0 ; i < arr .length ; i++ ) {
7206
+ for (let j = i + 1 ; j < arr .length ; j++ ) {
7207
+ if (arr[i] === arr[j] && ! duplicates .includes (arr[i])) {
7208
+ duplicates .push (arr[i]);
7209
+ }
7210
+ }
7211
+ }
7212
+ return duplicates;
7213
+ }
7214
+
7215
+ const numbers = [3 , 1 , 4 , 1 , 5 , 9 , 2 , 6 , 5 , 3 , 5 ];
7216
+ console .log (findDuplicates (numbers)); // Output: [1, 3, 5]
7217
+ ` ` `
7218
+
7219
+ [Back to Top⤴️](#table-of-contents)
7220
+
7221
+ Write a function to remove duplicate numbers in an Array Without Inbuilt Method.
7222
+
7223
+ ` ` ` javascript
7224
+ function removeDuplicates (arr ) {
7225
+ const unique = [];
7226
+ for (let i = 0 ; i < arr .length ; i++ ) {
7227
+ let isDuplicate = false ;
7228
+ for (let j = 0 ; j < unique .length ; j++ ) {
7229
+ if (arr[i] === unique[j]) {
7230
+ isDuplicate = true ;
7231
+ break ;
7232
+ }
7233
+ }
7234
+ if (! isDuplicate) {
7235
+ unique .push (arr[i]);
7236
+ }
7237
+ }
7238
+ return unique;
7239
+ }
7240
+
7241
+ const numbers = [3 , 1 , 4 , 1 , 5 , 9 , 2 , 6 , 5 , 3 , 5 ];
7242
+ console .log (removeDuplicates (numbers)); // Output: [3, 1, 4, 5, 9, 2, 6]
7243
+ ` ` `
7244
+
7245
+ Write a function to reverse a string Without Inbuilt Method.
7246
+
7247
+ ` ` ` javascript
7248
+ function reverseString (str ) {
7249
+ let reversed = ' ' ;
7250
+ for (let i = str .length - 1 ; i >= 0 ; i-- ) {
7251
+ reversed += str[i];
7252
+ }
7253
+ return reversed;
7254
+ }
7255
+
7256
+ console .log (reverseString (' hello' )); // Output: 'olleh'
7257
+ ` ` `
7258
+
7259
+ Write a function to reverse a number Without Inbuilt Method.
7260
+
7261
+ ` ` ` javascript
7262
+ function reverseNumber (num ) {
7263
+ let reversed = 0 ;
7264
+ while (num > 0 ) {
7265
+ reversed = reversed * 10 + (num % 10 );
7266
+ num = Math .floor (num / 10 );
7267
+ }
7268
+ return reversed;
7269
+ }
7270
+
7271
+ console .log (reverseNumber (12345 )); // Output: 54321
7272
+ ` ` `
7273
+
7062
7274
Print only id's from the below array
7063
7275
7064
7276
` ` ` jsx
0 commit comments