Skip to content

Commit

Permalink
feat: Add functions for sorting, finding max/min, sum/average, prime/…
Browse files Browse the repository at this point in the history
…palindrome checks, and duplicate removal

Added functions to the code that perform various operations on arrays and strings without using inbuilt methods. The functions include sorting an array, finding the maximum and minimum numbers, calculating the sum and average of an array, checking if a number or string is prime or palindrome, finding duplicate numbers in an array, removing duplicate numbers from an array, reversing a string, and reversing a number.
  • Loading branch information
manthanank committed Oct 6, 2024
1 parent 8dc3c26 commit 76cba6f
Showing 1 changed file with 212 additions and 0 deletions.
212 changes: 212 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7059,6 +7059,218 @@ JavaScript offers several shorthand techniques to write code more concisely and
## Interview Questions
Write a function that sorts an array of numbers Without Inbuilt Method.
```javascript
function sortArray(arr) {
for (let i = 0; i < arr.length; i++) {
for (let j = i + 1; j < arr.length; j++) {
if (arr[i] > arr[j]) {
let temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
return arr;
}

const numbers = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5];
console.log(sortArray(numbers)); // Output: [1, 1, 2, 3, 3, 4, 5, 5, 5, 6, 9]
```
Write a function that finds the maximum number in an array of numbers Without Inbuilt Method.
```javascript
function findMax(arr) {
let max = arr[0];
for (let i = 1; i < arr.length; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
return max;
}

const numbers = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5];
console.log(findMax(numbers)); // Output: 9
```
Write a function that finds the minimum number in an array of numbers Without Inbuilt Method.
```javascript
function findMin(arr) {
let min = arr[0];
for (let i = 1; i < arr.length; i++) {
if (arr[i] < min) {
min = arr[i];
}
}
return min;
}

const numbers = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5];
console.log(findMin(numbers)); // Output: 1
```
Write a function that calculates the sum of an array of numbers Without Inbuilt Method.
```javascript
function sumArray(arr) {
let sum = 0;
for (let i = 0; i < arr.length; i++) {
sum += arr[i];
}
return sum;
}

const numbers = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5];
console.log(sumArray(numbers)); // Output: 44
```
Write a function that calculates the average of an array of numbers Without Inbuilt Method.
```javascript
function averageArray(arr) {
let sum = 0;
for (let i = 0; i < arr.length; i++) {
sum += arr[i];
}
return sum / arr.length;
}

const numbers = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5];
console.log(averageArray(numbers)); // Output: 4
```
[Back to Top⤴️](#table-of-contents)
Write a function that checks if a number is prime Without Inbuilt Method.
```javascript
function isPrime(num) {
if (num <= 1) {
return false;
}
for (let i = 2; i <= Math.sqrt(num); i++) {
if (num % i === 0) {
return false;
}
}
return true;
}

console.log(isPrime(7)); // Output: true
console.log(isPrime(10)); // Output: false
```
Write a function that checks if a number is a palindrome Without Inbuilt Method.
```javascript
function isPalindrome(num) {
const str = num.toString();
for (let i = 0; i < str.length / 2; i++) {
if (str[i] !== str[str.length - 1 - i]) {
return false;
}
}
return true;
}

console.log(isPalindrome(121)); // Output: true
console.log(isPalindrome(123)); // Output: false
```
Write a function that checks if a string is a palindrome Without Inbuilt Method.
```javascript
function isPalindrome(str) {
for (let i = 0; i < str.length / 2; i++) {
if (str[i] !== str[str.length - 1 - i]) {
return false;
}
}
return true;
}

console.log(isPalindrome('madam')); // Output: true
console.log(isPalindrome('hello')); // Output: false
```
Write a function to find duplicate numbers in an Array Without Inbuilt Method.
```javascript
function findDuplicates(arr) {
const duplicates = [];
for (let i = 0; i < arr.length; i++) {
for (let j = i + 1; j < arr.length; j++) {
if (arr[i] === arr[j] && !duplicates.includes(arr[i])) {
duplicates.push(arr[i]);
}
}
}
return duplicates;
}

const numbers = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5];
console.log(findDuplicates(numbers)); // Output: [1, 3, 5]
```
[Back to Top⤴️](#table-of-contents)
Write a function to remove duplicate numbers in an Array Without Inbuilt Method.
```javascript
function removeDuplicates(arr) {
const unique = [];
for (let i = 0; i < arr.length; i++) {
let isDuplicate = false;
for (let j = 0; j < unique.length; j++) {
if (arr[i] === unique[j]) {
isDuplicate = true;
break;
}
}
if (!isDuplicate) {
unique.push(arr[i]);
}
}
return unique;
}

const numbers = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5];
console.log(removeDuplicates(numbers)); // Output: [3, 1, 4, 5, 9, 2, 6]
```
Write a function to reverse a string Without Inbuilt Method.
```javascript
function reverseString(str) {
let reversed = '';
for (let i = str.length - 1; i >= 0; i--) {
reversed += str[i];
}
return reversed;
}

console.log(reverseString('hello')); // Output: 'olleh'
```
Write a function to reverse a number Without Inbuilt Method.
```javascript
function reverseNumber(num) {
let reversed = 0;
while (num > 0) {
reversed = reversed * 10 + (num % 10);
num = Math.floor(num / 10);
}
return reversed;
}

console.log(reverseNumber(12345)); // Output: 54321
```
Print only id's from the below array
```jsx
Expand Down

0 comments on commit 76cba6f

Please sign in to comment.