Last active
October 2, 2020 12:12
-
-
Save ShilpiMaurya/2ab507277f2c85eb4284f7d7678ec93d 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
//Mock interview | |
//reverse sentence | |
const reverseSentence = str => { | |
if (typeof str !== "string" || !str.length) { | |
return null; | |
} | |
const smallStr = str | |
.toLowerCase() | |
.split("") | |
.reverse() | |
.join(""); | |
return smallStr; | |
}; | |
//tests | |
console.log(reverseSentence("") === null); | |
console.log(reverseSentence(123) === null); | |
console.log(reverseSentence([1, 2, 3, 4]) === null); | |
console.log(reverseSentence("smart work") === "krow trams"); | |
console.log(reverseSentence("good job") === "boj doog"); | |
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
const operationAnagram = (str1, str2) => { | |
if ( | |
typeof str1 !== "string" || | |
typeof str2 !== "string" || | |
!str1.length || | |
!str2.length | |
) { | |
return null; | |
} | |
const input1 = str1.split(""); | |
const count1 = {}; | |
const input2 = str2.split(""); | |
const count2 = {}; | |
input1.forEach(item => { | |
if (count1[item]) { | |
count1[item] = count1[item] + 1; | |
} else { | |
count1[item] = 1; | |
} | |
}); | |
input2.forEach(item => { | |
if (count2[item]) { | |
count2[item] = count2[item] + 1; | |
} else { | |
count2[item] = 1; | |
} | |
}); | |
const key1 = Object.keys(count1); | |
for (let key of key1) { | |
if (count1[key] !== count2[key]) { | |
return false; | |
} | |
} | |
return true; | |
}; | |
//tests | |
console.log(operationAnagram("cheerios", "") === null, "test 1"); | |
console.log(operationAnagram("", "cake") === null, "test 2"); | |
console.log(operationAnagram("kite", "apple") === false, "test 3"); | |
console.log(operationAnagram("mary", "army") === true, "test 4"); | |
////////////////////////////////////////////////////////////////////////////// | |
////////////////////////////////////////////////////////////////////////////// | |
////////////////////////////////////////////////////////////////////////////// | |
//Anagram challenge | |
const isAnagram = (str1, str2) => { | |
if ( | |
typeof str1 !== "string" || | |
typeof str2 !== "string" || | |
!str1.length || | |
!str2.length | |
) { | |
return null; | |
} | |
const result1 = str1 | |
.toLowerCase() | |
.split("") | |
.sort() | |
.join(""); | |
const result2 = str2 | |
.toLowerCase() | |
.split("") | |
.sort() | |
.join(""); | |
if (result1.length !== result2.length) { | |
return false; | |
} | |
if (result1 !== result2) { | |
return false; | |
} | |
return true; | |
}; | |
//tests | |
console.log(isAnagram("cheerios", "") === null, "test 1"); | |
console.log(isAnagram("", "cake") === null, "test 2"); | |
console.log(isAnagram("kite", "apple") === false, "test 3"); | |
console.log(isAnagram("mary", "army") === true, "test 4"); | |
////////////////////////////////////////////////////////////////////////////// | |
////////////////////////////////////////////////////////////////////////////// | |
////////////////////////////////////////////////////////////////////////////// | |
//find the largest array from an array of arrays | |
const twoDimArr = arr => { | |
let largest = []; | |
let num; | |
for (let i = 0; i < arr.length; i++) { | |
for (let j = 0; j < arr[i].length; j++) { | |
num = Math.max(...arr[i]); | |
} | |
largest.push(num); | |
} | |
return largest; | |
}; | |
//tests | |
console.log( | |
twoDimArr([ | |
[1, 2, 3], | |
[4, 5, 6], | |
[7, 8, 9], | |
[10, 11, 12], | |
[13, 14, 15] | |
]) | |
); | |
//Confirm ending | |
const confirmEnding = (str, target) => { | |
if ( | |
typeof str !== "string" || | |
typeof target !== "string" || | |
!str.length || | |
!target.length | |
) { | |
return false; | |
} | |
const words = str.split(""); | |
if (target === words[words.length - 1]) { | |
return true; | |
} else { | |
return false; | |
} | |
}; | |
//tests | |
console.log(confirmEnding("pancakes", "") === false, "test 1"); | |
console.log(confirmEnding("", "m") === false, "test 2"); | |
console.log(confirmEnding("", "") === false, "test 3"); | |
console.log(confirmEnding("maple", "e") === true, "test 4 "); | |
console.log(confirmEnding("tuna", "a") === true, "test 5"); | |
//Another method | |
const confirmEnd = (str, target) => { | |
if (str.endsWith(target)) { | |
return true; | |
} else { | |
return false; | |
} | |
}; | |
//tests | |
console.log(confirmEnd("maple", "e") === true, "test extra 1 "); | |
console.log(confirmEnd("tunafish", "h") === true, "test extra 2"); | |
console.log(confirmEnd("misosoup", "h") === false, "test extra 2"); | |
//Repeat string num times | |
const repeatNumTimes = (str, num) => { | |
if ( | |
typeof str !== "string" || | |
!str.length || | |
!Number.isFinite(num) || | |
num < 0 | |
) { | |
return false; | |
} | |
let final = ""; | |
for (let i = 0; i < num; i++) { | |
final = final + str; | |
} | |
return final; | |
}; | |
//tests | |
console.log(repeatNumTimes("", 3) === false, "test 1"); | |
console.log(repeatNumTimes("abc", -1) === false, "test 2"); | |
console.log(repeatNumTimes("xyz", 2) === "xyzxyz", "test 3"); | |
console.log(repeatNumTimes("we won", 2) === "we wonwe won", "test 4"); | |
//alternate method | |
const repeatNtimes = (str, num) => { | |
return str.repeat(num); | |
}; | |
//tests | |
console.log(repeatNtimes("xyz", 3) === "xyzxyzxyz", "test extra 1"); | |
//Truncate string | |
//Truncate a string (first argument) if it is longer than the given | |
//maximum string length (second argument). Return the truncated string with | |
//a ... ending, else return string. | |
const truncateString = (str, num) => { | |
if ( | |
typeof str !== "string" || | |
!str.length || | |
!Number.isFinite(num) || | |
num < 0 | |
) { | |
return false; | |
} | |
if (str.length <= num) { | |
return str; | |
} else { | |
return str.slice(0, num - 3) + "..."; | |
} | |
}; | |
//tests | |
console.log(truncateString("", 6) === false); | |
console.log(truncateString("shilpi", -3) === false); | |
console.log(truncateString("shilpi", 6) === "shilpi"); | |
console.log(truncateString("hey whats up", 5) === "he..."); | |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
//18-09-2020, Thursday | |
//Find out whether given series is a AP or not | |
const mathSequenceAP = arr => { | |
if (!Array.isArray(arr) || !arr.length) { | |
return null; | |
} | |
for (let i = 1; i < arr.length - 1; i++) { | |
if (arr[i] - arr[i - 1] !== arr[i + 1] - arr[i]) { | |
return false; | |
} | |
} | |
return true; | |
}; | |
//tests | |
console.log(mathSequenceAP(1, 2, 3, 4, 5, 6) === null, "test 1"); | |
console.log(mathSequenceAP("It's a trap") === null, "test 2"); | |
console.log(mathSequenceAP([]) === null, "test 3"); | |
console.log(mathSequenceAP([2, 4, 6, 8]) === true, "test 4"); | |
console.log(mathSequenceAP([2, 4, 6, 9]) === false, "test 5"); | |
console.log(mathSequenceAP([2, 3, 6, 8]) === false, "test 6"); | |
//Reverse a string | |
const reverseString = str => { | |
if (typeof str !== "string" || !str.length) { | |
return null; | |
} | |
const results = []; | |
const strSplit = str.split(""); | |
for (let i = strSplit.length - 1; i >= 0; i--) { | |
results.push(strSplit[i]); | |
} | |
return results.join(""); | |
}; | |
//tests | |
console.log(reverseString("") === null, "test 1"); | |
console.log(reverseString(123) === null, "test 2"); | |
console.log(reverseString("hello") === "olleh", "test 3"); | |
console.log(reverseString("shilpi") === "iplihs", "test 4"); | |
console.log(reverseString("sweet") === "teews", "test 5"); | |
//tutorial's method | |
const stringReverse = str => { | |
return str | |
.split("") | |
.reverse() | |
.join(""); | |
}; | |
//Another way | |
const strReverse = str => { | |
let results = ""; | |
for (let i = str.length - 1; i >= 0; i--) { | |
results = results + str[i]; | |
} | |
return results; | |
}; | |
//Factorial | |
const factorial = num => { | |
if (!Number.isInteger(num)) { | |
return null; | |
} | |
let results = 1; | |
for (let i = num; i >= 1; i--) { | |
results = results * i; | |
} | |
return results; | |
}; | |
//tests | |
console.log(factorial("string") === null, "test 1"); | |
console.log(factorial([1, 2, 3, 4]) === null, "test 2"); | |
console.log(factorial(3) === 6, "test 3"); | |
console.log(factorial(6) === 720, "test 4"); | |
//Palindrome | |
const palindrome = str => { | |
if (typeof str !== "string" || !str.length) { | |
return null; | |
} | |
const smallStr = str.toLowerCase().replace(/[\W_]/, ""); | |
const reverseStr = smallStr | |
.split("") | |
.reverse() | |
.join(""); | |
if (smallStr === reverseStr) { | |
return true; | |
} else { | |
return false; | |
} | |
}; | |
//tests | |
console.log(palindrome(123) === null, "test 1"); | |
console.log(palindrome("") === null, "test 2"); | |
console.log(palindrome("eye") === true, "test 3"); | |
console.log(palindrome("shilpi") === false, "test 4"); | |
console.log(palindrome("level") === true, "test 5"); | |
console.log(palindrome("race car") === true, "test 6"); | |
//Longest word length | |
const LongestWordLength = str => { | |
if (typeof str !== "string" || !str.length) { | |
return null; | |
} | |
const longestWord = str.split(" "); | |
let longest = ""; | |
for (let i = 0; i < longestWord.length; i++) { | |
if (longestWord[i].length > longest.length) { | |
longest = longestWord[i]; | |
} | |
} | |
return longest.length; | |
}; | |
//tests | |
console.log(LongestWordLength("") === null, "test 1"); | |
console.log(LongestWordLength(123) === null, "test 2"); | |
console.log(LongestWordLength("my name is shilpi") === 6, "test 3"); | |
console.log(LongestWordLength("cats are awesome") === 7, "test 4"); | |
console.log(LongestWordLength("wishing well") === 7, "test 5"); | |
//tutorial | |
const longestWord = str => { | |
return str.split(" ").sort(function(a, b) { | |
return b.length - a.length; | |
})[0].length; | |
}; | |
//19-09-2020, Friday | |
//Title case a sentence | |
const titleCase = str => { | |
if (typeof str !== "string" || !str.length) { | |
return null; | |
} | |
const smallStr = str.toLowerCase().split(" "); | |
for (let i = 0; i < smallStr.length; i++) { | |
smallStr[i] = smallStr[i][0].toUpperCase() + smallStr[i].slice(1); | |
} | |
return smallStr.join(" "); | |
}; | |
//test cases | |
console.log(titleCase("Shilpi christy")); | |
console.log(titleCase(123) === null, "test 1"); | |
console.log(titleCase("") === null, "test 2"); | |
console.log(titleCase("my name is Shilpi") === "My Name Is Shilpi", "test 3"); | |
console.log(titleCase("flying kites") === "Flying Kites", "test 4"); | |
console.log(titleCase("Organic farming") === "Organic Farming", "test 5"); | |
//Another way | |
const caseTitle = str => { | |
return str | |
.toLowerCase() | |
.split(" ") | |
.map(item => { | |
return item[0].toUpperCase() + item.slice(1); | |
}) | |
.join(" "); | |
}; | |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
//Older Challenges | |
//Programming practice | |
//write a js program to check two numbers and return true is one of them is | |
//100 or their sum is 100 | |
const isHundred = (num1, num2) => { | |
if (typeof num1 !== "number" || typeof num2 !== "number" || !num1 || !num2) { | |
return null; | |
} | |
if (num1 === 100 || num2 === 100 || num1 + num2 === 100) { | |
return true; | |
} else { | |
return false; | |
} | |
}; | |
//0(1) | |
//tests | |
console.log(isHundred(100, 1) === true); | |
console.log(isHundred(1, 100) === true); | |
console.log(isHundred(10, 19) === false); | |
console.log(isHundred(15, 75) === false); | |
console.log(isHundred(50, 50) === true); | |
console.log(isHundred(1, "shilpi") === null); | |
console.log(isHundred(1) === null); | |
//Write a program to get the extension of a filename | |
const getFileExtension = str => { | |
if ( | |
typeof str !== "string" || | |
str.lastIndexOf(".") === str.length - 1 || | |
str.lastIndexOf(".") === -1 | |
) { | |
return null; | |
} | |
const result = str.slice(str.lastIndexOf(".")); | |
return result; | |
}; | |
//tests | |
console.log(getFileExtension("index.html") === ".html"); | |
console.log(getFileExtension("index.css") === ".css"); | |
console.log(getFileExtension("index.js") === ".js"); | |
console.log(getFileExtension("index") === null); | |
console.log(getFileExtension("index.") === null); | |
console.log(getFileExtension(123) === null); | |
// write a program in js that replace every character in a given string with | |
// the character following it in a alphabet | |
const moveCharacterForword = str => { | |
if (typeof str !== "string" || !str.length) { | |
return null; | |
} else { | |
return str | |
.split("") | |
.map(char => String.fromCharCode(char.charCodeAt(0) + 1)) | |
.join(""); | |
} | |
}; | |
//tests | |
console.log(moveCharacterForword("abc") === "bcd", "test 1"); | |
console.log(moveCharacterForword("") === null, "test 2"); | |
console.log(moveCharacterForword(123) === null, "test 3"); | |
//write a js program to return a current date | |
const formatDate = (date = new Date()) => { | |
const day = date.getDate(); | |
const month = date.getMonth(); | |
const year = date.getFullYear(); | |
return `${day}/${month}/${year}`; | |
}; | |
console.log(formatDate()); | |
const now = new Date(); | |
console.log(now); | |
//Write a js program to create a new string adding "New!" in front of | |
//a new given string , if the given string already has "New!" return the | |
//original string | |
const addingNew = str => { | |
if (typeof str !== "string" || !str.length) { | |
return null; | |
} | |
const newStr = str.split(" "); | |
if (newStr[0] === "New!") { | |
return str; | |
} else { | |
return newStr.splice(0, 0, "New!").join(" "); | |
} | |
}; | |
//tests | |
console.log(addingNew("name is shilpi") === "New! name is shilpi"); | |
console.log(addingNew(123) === null); | |
console.log(addingNew("") === null); | |
console.log(addingNew("New! offers") === "New! offers"); | |
//alternate method | |
const addNew = string => | |
string.indexOf("New!" === 0) ? string : `New! ${string}`; | |
//tests | |
console.log(addNew("Discounts")); | |
console.log(addNew("New! offers")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Suggestions:
js your code
. Otherwise, code is really difficult to read.string.indexOf("New!") === 0
, notstring.indexOf("New!" === 0)
Number.isFinite() or Number.isInteger()