You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
05. Write functions to calculate the bitwise AND, bitwise OR and bitwise XOR of two numbers.
function bitwiseAND(n1, n2) {
//Write Your solution Here
};
function bitwiseOR(n1, n2) {
//Write Your solution Here
};
function bitwiseXOR(n1, n2) {
//Write Your solution Here
};
console.log(bitwiseAND(10, 20)); // 0
console.log(bitwiseOR(10, 20)); // 30
console.log(bitwiseXOR(10, 20)); // 30
Solution
function bitwiseAND(n1, n2) {
let answer = n1 & n2;
return (answer);
};
function bitwiseOR(n1, n2) {
let answer = n1 | n2;
return (answer);
};
function bitwiseXOR(n1, n2) {
let answer = n1 ^ n2;
return (answer);
};
18. Your function will be passed two functions, f and g, that don't take any parameters. Your function has to call them, and return a string which indicates which function returned the larger number.
If f returns the larger number, return the string f.
If g returns the larger number, return the string g.
If the functions return the same number, return the string neither.
function whichIsLarger(f, g){
//Write Your solution Here
};
console.log(whichIsLarger(() => 25, () => 15)); // f
console.log(whichIsLarger(() => 25, () => 25)); // neither
console.log(whichIsLarger(() => 25, () => 50)); // g
Solution
function whichIsLarger(f, g){
if (f() > g()) {
return ('f')
}
else if (g() > f()) {
return ('g')
}
else if (f() === g()) {
return ('neither')
}
};
19. Christmas Eve is almost upon us, so naturally we need to prepare some milk and cookies for Santa! Create a function that accepts a Date object and returns true if it's Christmas Eve (December 24th) and false otherwise. Keep in mind JavaScript's Date month is 0 based, meaning December is the 11th month while January is 0.
function timeForMilkAndCookies(date){
//Write Your solution Here
};
console.log(timeForMilkAndCookies(new Date(3000, 11, 24))); //true
console.log(timeForMilkAndCookies(new Date(2013, 0, 23))); //false
console.log(timeForMilkAndCookies(new Date(3000, 11, 24))); //true
20. function that takes a two-digit number and determines if it's the largest of two possible digit swaps.
function largestSwap(num){
//Write Your solution Here
};
console.log(largestSwap(14)); //false
console.log(largestSwap(53)); //true
console.log(largestSwap(-27)); //false
Solution
function largestSwap(num){
let num1 = num + "";
let num2 = num1.split("").reverse().join("");
if (num1 >= num2) {
return true;
}
if (num1 < num2) {
return false;
}
};
function largestSwap(num) {
let c = num.toString();
let a = [];
let b = 0;
for (let i = 0; i < c.length; i++) {
a.push(c[c.length - 1 - i]);
b += a[i];
}
let d = parseInt(b);
if (d > num) {
return false;
} else return true;
};
21. function that takes two strings as arguments and returns the number of times the first string (the single character) is found in the second string.
function charCount(myChar, str){
//Write Your solution Here
};
console.log(charCount("a", "largest")); //1
console.log(charCount("c", "Chamber of secrets")); // 2
console.log(charCount("b", "big fat bubble")); //4
Solution
function charCount(myChar, str){
let a = 0;
for (let i = 0; i < str.length; i++) {
if (myChar.toLowerCase() === str.toLowerCase()[i]) {
a += 1;
}
}
return a
};
24. Write a function that take a string and write a regular expression inner function that returns the value that matches every red flag and blue flag in this string.
function matchFlag(str){
//Write Your solution Here
};
console.log(matchFlag("yellow flag red flag blue flag green flag")); //[ 'red flag', 'blue flag' ]
console.log(matchFlag("yellow flag green flag orange flag white flag")); //null
console.log(matchFlag("yellow flag blue flag green flag")); //[ 'blue flag' ]
Solution
function matchFlag(str){
let REGEXP = /red flag|blue flag/g;
return str.match(REGEXP);
};
27. Create a function that takes a string and returns a string in which each character is repeated once.
function doubleChar(str){
//Write Your solution Here
};
console.log(doubleChar('jahidul')); //jjaahhiidduull
console.log(doubleChar('islam')); //iissllaamm
console.log(doubleChar('zim')); //zziimm
Solution
function doubleChar(str) {
let doubleString = '';
for(let i=0; i<str.length; i++){
doubleString += str[i] + str[i]
}
return doubleString
};
function doubleChar(str){
let array = str.split("");
let array2 = array.map( x => x.repeat(2));
let doubleString = array2.join("");
return doubleString
};
28. Write a function that takes a positive integer and return its factorial.
function factorial(num){
//Write Your solution Here
};
console.log(factorial(5)); //120
console.log(factorial(10)); //3628800
console.log(factorial(8)); //40320
Solution
function factorial(num) {
let fact = 1;
for (let i = 0; i<num ; i++){
fact *= (num-i);
}
return fact
};
function factorial(num){
if (num < 0)
return -1;
else if (num == 0)
return 1;
else {
return (num * factorial(num - 1));
}
};
function factorial(num){
let result = num;
if (num === 0 || num === 1) return 1;
while (num > 1) {
num--;
result *= num;
}
return result;
};
function factorial(num){
let fact = num;
if (num === 0 || num === 1) return 1;
for (let i = num - 1; i >= 1; i--) {
fact *= i;
}
return fact;
};
31. Create a function that takes a string and returns the number (count) of vowels contained within it.
function countVowels(str){
//Write Your solution Here
};
console.log(countVowels('Jahidul Islam zim')); // 6
console.log(countVowels('returns the number of vowels')); // 8
console.log(countVowels('JavaScript Coding Challenges')); // 8
Solution
function countVowels(str){
let count = 0;
let vowlStr = str.toLowerCase().match(/(a|e|i|o|u)/g);
for (let i = 0; i < vowlStr.length; i++) {
count += 1;
}
return count;
};
32. Create a function that takes two vectors as arrays and checks if the two vectors are orthogonal or not. The return value is boolean. Two vectors a and b are orthogonal if their dot product is equal to zero.
function isOrthogonal(arr1, arr2){
//Write Your solution Here
};
console.log(isOrthogonal([1, -2, 4], [2, 5, 2])); //true
console.log(isOrthogonal([1, -2, 5], [2, 5, 2])); //false
console.log(isOrthogonal([1, 2, 4], [-2, 5, -2])); //true
Solution
function isOrthogonal(arr1, arr2){
let ortho = 0;
for (i = 0; i < arr1.length; i++){
ortho += arr1[i]*arr2[i]
}
if (ortho === 0) {
return true
}
return false
};
33. Given an object of how many more pages each ink color can print, output the maximum number of pages the printer can print before any of the colors run out.
34. Create a function that takes a string and returns a new string with all vowels removed.
function removeVowels(str){
//Write Your solution Here
};
console.log(removeVowels('Jahidul Islam Zim')); //Jhdl slm Zm
console.log(removeVowels('a new string with all vowels')); // nw strng wth ll vwls
console.log(removeVowels('Create a function')); //Crt fnctn
Solution
function removeVowels(str){
let newArr = str.match(/[^aeiouAEIOU]/g);
let newString = newArr.join('');
return newString
};
36. Given an array of scrabble tiles, create a function that outputs the maximum possible score a player can achieve by summing up the total number of points for all the tiles in their hand. Each hand contains 7 scrabble tiles.
37. Assume a program only reads .js or .jsx files. Write a function that accepts a file path and returns true if it can read the file and false if it can't.
function isJS(path){
//Write Your solution Here
};
console.log(isJS('file.jsx')); //true
console.log(isJS('file.jsg')); //false
console.log(isJS('file.js')); //true
Solution
function isJS(path){
let extension = path.match( /[^.]+$/g)[0]
if(extension === 'js' || extension==='jsx'){
return true;
}
return false;
};
38. A farmer is asking you to tell him how many legs can be counted among all his animals. The farmer breeds three species:
chickens = 2 legs
cows = 4 legs
goats = 4 legs
The farmer has counted his animals and he gives you a subtotal for each species. You have to implement a function that returns the total number of legs of all the animals.
function animals(chickens, cows, goats){
//Write Your solution Here
};
console.log(animals(2, 3, 5)) // 36
console.log(animals(1, 2, 3)) // 22
console.log(animals(5, 2, 8)) // 50
41.Create a function that takes two strings as arguments and return either true or false depending on whether the total number of characters in the first string is equal to the total number of characters in the second string.
function comp(str1, str2){
//Write Your solution Here
};
console.log(comp("AB", "CD")); //true
console.log(comp("ABC", "DE")); //false
console.log(comp("WE", "RT")); // true
Solution
function comp(str1, str2) {
if (str1.length === str2.length) {
return true;
}
return false;
};
42.A vehicle needs 10 times the amount of fuel than the distance it travels. However, it must always carry a minimum of 100 fuel before setting off. Create a function which calculates the amount of fuel it needs, given the distance.
function calculateFuel(n){
//Write Your solution Here
};
console.log(calculateFuel(15)); //150
console.log(calculateFuel(23.5)); //235
console.log(calculateFuel(3)); //100
Solution
function calculateFuel(n){
let fuel = n * 10;
if (fuel >= 100) {
return fuel;
};
return 100;
};
43.Given an n-sided regular polygon n, return the total sum of internal angles (in degrees). n will always be greater than 2.
The formula (n - 2) x 180 gives the sum of all the measures of the angles of an n-sided polygon.
function sumPolygon(n){
//Write Your solution Here
};
console.log(sumPolygon(3)); //180
console.log(sumPolygon(4)); //360
console.log(sumPolygon(6)); // 720
44.Create a function that returns true if an integer is evenly divisible by 5, and false otherwise.
function divisibleByFive(n){
//Write Your solution Here
};
console.log(divisibleByFive(5)); //true
console.log(divisibleByFive(-55)); //false
console.log(divisibleByFive(37)); //true
Solution
function divisibleByFive(n){
if (n % 5 === 0) {
return true;
};
return false;
};
46.A group of friends have decided to start a secret society. The name will be the first letter of each of their names, sorted in alphabetical order. Create a function that takes in an array of names and returns the name of the secret society.
function societyName(friends){
//Write Your solution Here
};
console.log(societyName(['zim', 'zoy', 'shithil', 'akib'])); //zzsa
console.log(societyName(['Rakib', 'Taskin', 'shomrat', 'Prionty'])); //RTsP
console.log(societyName(['Ratul', 'Rakib', 'Ritu', 'Taj'])); //RRRT
Solution
function societyName(friends){
let nameArr = friends.map((x) => x.split(""));
let letterArr = "";
for (let i = 0; i < nameArr.length; i++) {
letterArr += nameArr[i][0];
};
return letterArr;
};
47. Create two functions: isPrefix(word, prefix-) and isSuffix(word, -suffix).
isPrefix should return true if it begins with the prefix argument.
isSuffix should return true if it ends with the suffix argument.
Otherwise return false.
function isPrefix(word, prefix) {
//Write Your solution Here
};
function isSuffix(word, suffix){
//Write Your solution Here
};
console.log(isPrefix("automation", "auto-")); //true
console.log(isPrefix("retrospect", "sub-")); //false
console.log(isSuffix("arachnophobia", "-phobia")); //true
console.log(isSuffix("vocation", "-logy")); //false
Solution
function isPrefix(word, prefix) {
return word.startsWith(prefix.slice(0, -1));
}
function isSuffix(word, suffix) {
return word.endsWith(suffix.slice(1));
}
function isPrefix(word, prefix) {
let prefixWord = prefix.slice(0, -1);
let mainWord = '';
for(let i = 0; i < prefixWord.length; i++){
mainWord += word[i];
}
if(prefixWord === mainWord){
return true;
}
return false;
}
function isSuffix(word, suffix) {
let suffixWord = suffix.slice(1);
let mainWord = '';
let newString = '';
for(let i = word.length - 1; i > suffixWord.length; i--){
mainWord += word[i];
}
for (let i = mainWord.length - 1; i >= 0; i--) {
newString += mainWord[i];
}
if(suffixWord === newString){
return true;
}
return false;
}
49. Create a function that takes an array of 10 numbers (between 0 and 9) and returns a string of those numbers formatted as a phone number (e.g. (555) 555-5555).
50. Write a regular expression that matches only an even number. Numbers will be presented as strings.
function matchEven(str){
//Write Your solution Here
};
console.log(matchEven("3458")); //8
console.log(matchEven("3517")); //Undefined
console.log(matchEven("4902")); //2
Solution
function matchEven(num){
let REGEXP = /[24680]$/;
return num.match(REGEXP)?.[0];
};
51. Create a function that returns the index of the first vowel in a string.
function firstVowel(str){
//Write Your solution Here
};
console.log(firstVowel("zimislam")); //1
console.log(firstVowel("akib")); //0
console.log(firstVowel("shomrat")); //2
Solution
function firstVowel(str){
let regex = /[aeiou]/gi;
return str.search(regex);
};
54. Given a higher bound num, implement a function that returns an array with the sequence of numbers, after that every multiple of 4 has been amplified by 10.
function maxTotal(nums){
let nums1 = nums.sort(function (a, b){return a-b})
let max = 0
for (i=5; i<nums1.length; i++){
max += nums1[i];
}
return max;
}
56. Write a regular expression that matches a string if and only if it is a valid zip code.
Must only contain numbers (no non-digits allowed).
Must not contain any spaces.
Must not be greater than 5 digits in length.
function isValidZip(zip){
//Write Your solution Here
};
console.log(isValidZip("393939")); //false
console.log(isValidZip("59001")); //true
console.log(isValidZip("853a7")); //false
Solution
function isValidZip(zip) {
const REXEX = /^\d{5}$/;
return REXEX.test(zip);
}
57. Create a function that takes a number as an argument and returns the highest digit in that number.
function highestDigit(number){
//Write Your solution Here
};
console.log(highestDigit(3456)); //6
console.log(highestDigit(21098)); //9
console.log(highestDigit(56123)); //6
58. Create a function that takes a number as an argument and returns the highest digit in that number.
function highestDigit(number){
//Write Your solution Here
};
console.log(highestDigit(3456)); //6
console.log(highestDigit(21098)); //9
console.log(highestDigit(56123)); //6
59. Given an array of scrabble tiles, create a function that outputs the maximum possible score a player can achieve by summing up the total number of points for all the tiles in their hand. Each hand contains 7 scrabble tiles.
60.Create a function that takes an array as an argument and returns true or false depending on whether the average of all elements in the array is a whole number or not.
62. A value is omnipresent if it exists in every subarray inside the main array. Create a function that determines whether an input value is omnipresent for a given array.Create a function that takes an array of strings and return an array, sorted from shortest to longest.
Hello JavaScript code newbie! In this repository I'm proposing you a series of coding challenges that will help you practice the basic language constructs and algorithms.