Last active
October 23, 2020 11:23
-
-
Save ShilpiMaurya/f7463578ec0f1309ef99d108bd806958 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//20-10-2020 | |
//Where do i belong | |
const getIndex = (arr, num) => { | |
if (!Array.isArray(arr) || !arr.length || !Number.isFinite(num)) { | |
return null; | |
} | |
arr.push(num); | |
const newArr = arr.sort(); | |
const position = newArr.indexOf(num); | |
return position; | |
}; | |
console.log(getIndex([5, 2, 3, 1, 0], 4)); | |
//Tutorial Method | |
const getIndexOfNum = (arr, num) => { | |
arr.sort((a, b) => { | |
return a - b; | |
}); | |
for (let i = 0; i < arr.length; i++) { | |
if (num <= arr[i]) { | |
return i; | |
} | |
} | |
return arr.length; | |
}; | |
console.log(getIndexOfNum([22, 48, 62, 90, 12, 30], 50)); | |
//21/10/2020 | |
//Caesars Cipher | |
const caesarCipher = str => { | |
if (typeof str !== "string" || !str.length) { | |
return null; | |
} | |
let char = ""; | |
for (let i = 0; i < str.length; i++) { | |
let asciiCode = str[i].charCodeAt(); | |
if (asciiCode >= 65 && asciiCode <= 77) { | |
char = char + String.fromCharCode(asciiCode + 13); | |
} else if (asciiCode >= 78 && asciiCode <= 90) { | |
char = char + String.fromCharCode(asciiCode - 13); | |
} else { | |
char = char + str[i]; | |
} | |
} | |
return char; | |
}; | |
//tests | |
console.log(caesarCipher("ABCD EFGH IJKL MNOP") === "NOPQ RSTU VWXY ZABC"); | |
//22/20/2020 | |
//sum all numbers in a range | |
//we ll pass you an array of two numbers and all numbers between them | |
//the lowest no ll always not come first | |
const sumAll = arr => { | |
if (!Array.isArray(arr) || !arr.length) { | |
return null; | |
} | |
let sum = 0; | |
if (arr[0] < arr[arr.length - 1]) { | |
for (let i = arr[arr.length - 1]; i >= arr[0]; i--) { | |
sum = sum + i; | |
} | |
} else { | |
for (let i = arr[arr.length - 1]; i <= arr[0]; i++) { | |
sum = sum + i; | |
} | |
} | |
return sum; | |
}; | |
console.log(sumAll([4, 1])); | |
//tutorial | |
const sumItUp = arr => { | |
let sum = 0; | |
let start = Math.min(arr[0], arr[1]); | |
let end = Math.max(arr[0], arr[1]); | |
for (let i = start; i <= end; i++) { | |
sum = sum + i; | |
} | |
return sum; | |
}; | |
console.log(sumItUp([2, 4])); | |
//23/10/2020 | |
//Diff two arrays | |
//You are given two arrays, you have to return all the unique numbers | |
const uniqueNum = (arr1, arr2) => { | |
if ( | |
!Array.isArray(arr1) || | |
!Array.isArray(arr2) || | |
!arr1.length || | |
!arr2.length | |
) { | |
return null; | |
} | |
const uniques = []; | |
for (let i = 0; i < arr1.length; i++) { | |
if (arr2.indexOf(arr1[i]) === -1) { | |
uniques.push(arr1[i]); | |
} | |
} | |
for (let j = 0; j < arr2.length; j++) { | |
if (arr1.indexOf(arr2[j]) === -1) { | |
uniques.push(arr2[j]); | |
} | |
} | |
return uniques; | |
}; | |
console.log(uniqueNum([1, 2, 3, 4], [1, 4, 5, 6, 7, 8, 2])); | |
//Using filter method | |
const uniNum = (arr1, arr2) => { | |
const arr = [...arr1, ...arr2]; | |
return arr.filter(num => { | |
console.log(num, "num"); | |
if (arr1.indexOf(num) === -1 || arr2.indexOf(num) === -1) { | |
return num; | |
} | |
}); | |
}; | |
console.log(uniNum([1, 2, 3], [1, 2, 3, 4])); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment