JavaScriptCodingChallenges
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.
Install / Use
/learn @jahidulislamzim/JavaScriptCodingChallengesREADME
<img src='./images/logo.png' alt='JavaScript Coding Challenges jahidul islam zim' id='header'/>
<h1 align="center" >JavaScript Coding Challenges </h1>
<div align="center" >
<a href="mailto:jahidulislamzim845@gmail.com">
<img
src='https://img.shields.io/badge/Gmail-D14836?style=for-the-badge&logo=gmail&logoColor=white'
alt='jahidul islam zim'
/>
</a>
<a href="tel:+8801780115943">
<img
src='https://img.shields.io/badge/WhatsApp-25D366?style=for-the-badge&logo=whatsapp&logoColor=white'
alt='jahidul islam zim'
/>
</a>
<a href="https://jahidulislamzim.netlify.app" target="_blank">
<img
src='https://img.shields.io/badge/website-000000?style=for-the-badge&logo=About.me&logoColor=white'
alt='jahidul islam zim'
/>
</a>
<a href="https://www.facebook.com/jahidulislamzim43" target="_blank">
<img
src='https://img.shields.io/badge/Facebook-1877F2?style=for-the-badge&logo=facebook&logoColor=white'
alt='jahidul islam zim'
/>
</a>
<a href="https://www.linkedin.com/in/jahidulislamzim/" target="_blank">
<img
src='https://img.shields.io/badge/LinkedIn-0077B5?style=for-the-badge&logo=linkedin&logoColor=white'
alt='jahidul islam zim'
/>
</a>
<a href="https://github.com/jahidulislamzim" target="_blank">
<img
src='https://img.shields.io/badge/GitHub-100000?style=for-the-badge&logo=github&logoColor=white'
alt='jahidul islam zim'
/>
</a>
</div>
01. Creates a function that takes two numbers as arguments and return their sum.
function addition(a, b) {
//Write Your solution Here
};
console.log(addition(10, 20)); // 30
console.log(addition(30, 20)); // 50
console.log(addition(10, 90)); // 100
<details><summary style="cursor:pointer">Solution</summary>
function addition(a, b) {
let add = a + b;
return (add)
};
</details>
02. Converts hours into seconds.
function howManySeconds(hours) {
//Write Your solution Here
};
console.log(howManySeconds(12)); // 43200
console.log(howManySeconds(8)); // 28800
console.log(howManySeconds(3)); // 10800
<details><summary style="cursor:pointer">Solution</summary>
function howManySeconds(hours) {
let hoursToSeconds = (hours*3600);
return(hoursToSeconds)
};
</details>
03. Converts minutes into seconds.
function convert(minutes){
//Write Your solution Here
};
console.log(convert(30)); // 1800
console.log(convert(10)); // 600
console.log(convert(20)); // 1200
<details><summary style="cursor:pointer">Solution</summary>
function convert(minutes) {
let seconds = minutes*60;
return (seconds)
};
</details>
04. Calculates total points of a team from number of wins(3pts), draws(1pt), and losses(0pt).
function footballPoints(wins, draws, losses){
//Write Your solution Here
};
console.log(footballPoints(4, 3, 1)); // 15
console.log(footballPoints(10, 5, 0)); // 35
console.log(footballPoints(11, 0, 9)); // 33
<details><summary style="cursor:pointer">Solution</summary>
function footballPoints(wins, draws, losses) {
let points = (wins*3) + (draws*1) + (losses*0)
return(points)
};
</details>
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
<details><summary style="cursor:pointer">Solution</summary>
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);
};
</details>
06. Write Function to return first value of an array.
function getFirstValue(arr) {
//Write Your solution Here
};
console.log(getFirstValue(["Saab", "Volvo", "BMW"])); // Saab
console.log(getFirstValue([3, 5, 1])); // 3
console.log(getFirstValue(['hello', 'world', 'welcome'])); // hello
<details><summary style="cursor:pointer">Solution</summary>
function getFirstValue(arr) {
return arr[0];
};
</details>
07. Create a function that takes a number as an argument, increments the number by +1 and returns the result.
function addition(num){
//Write Your solution Here
};
console.log(addition(5)); // 6
console.log(addition(100)); // 101
console.log(addition(99)); // 100
<details><summary style="cursor:pointer">Solution</summary>
function addition(num) {
let numPlusOne = num + 1;
return(numPlusOne)
};
</details>
08. Given two numbers, return true if the sum of both numbers is less than 100. Otherwise return false.
function lessThan100(a, b){
//Write Your solution Here
};
console.log(lessThan100(10, 20)); // true
console.log(lessThan100(50, 60)); // false
console.log(lessThan100(20, 50)); // true
<details><summary style="cursor:pointer">Solution</summary>
function lessThan100(a, b) {
if (a + b < 100) {
return true;
}
else {
return false;
}
};
</details>
09. Create a function that returns true when num1 is equal to num2; otherwise return false.
function isSameNum(num1, num2){
//Write Your solution Here
};
console.log(isSameNum(30, 30)); // true
console.log(isSameNum(20, 40)); // false
console.log(isSameNum(50, 50)); // true
<details><summary style="cursor:pointer">Solution</summary>
function isSameNum(num1, num2) {
if (num1 === num2){
return true;
}
else {
return false;
}
};
</details>
10. Create a function that takes a number (step) as an argument and returns the amount of matchsticks in that step.
<div align="center"> <img src='./images/matchstick.png' alt='JavaScript Coding Challenges jahidul islam zim' id='header'/> </div>function matchHouses(step){
//Write Your solution Here
};
console.log(matchHouses(5)); // 26
console.log(matchHouses(0)); // 0
console.log(matchHouses(10)); // 51
<details><summary style="cursor:pointer">Solution</summary>
function matchHouses(step){
if (step > 0) {
let matchSticks = ((step*6) - (step -1));
return(matchSticks)
}
else {
let matchSticks = 0;
return (matchSticks)
}
};
</details>
11. Write function to return the square of a number.
function squared(a){
//Write Your solution Here
};
console.log(squared(6)); // 36
console.log(squared(9)); // 81
console.log(squared(4)); // 16
<details><summary style="cursor:pointer">Solution</summary>
function squared(a) {
return (a*a);
};
</details>
12. Write function to calculate Perimeter of Rectangles
function findPerimeter(height, width){
//Write Your solution Here
};
console.log(findPerimeter(20, 50)); // 140
console.log(findPerimeter(80, 30)); // 220
console.log(findPerimeter(10, 40)); // 100
<details><summary style="cursor:pointer">Solution</summary>
function findPerimeter(height, width){
let perimeter = 2*(height + width);
return (perimeter)
};
</details>
13. Add up all the numbers from 1 to the number you passed to the function.
function addUp(num){
//Write Your solution Here
};
console.log(addUp(10)); // 55
console.log(addUp(40)); // 820
console.log(addUp(15)); // 120
<details><summary style="cursor:pointer">Solution</summary>
function addUp(num) {
let sum = 0;
for (i = 0; i <= num; i++){
sum += i;
}
return(sum)
};
</details>
14. Create a function that takes in three arguments (prob, prize, pay) and returns true if prob * prize > pay; otherwise return false.
function profitableGamble(prob, prize, pay){
//Write Your solution Here
};
console.log(profitableGamble(2, 10, 20)); // false
console.log(profitableGamble(5, 10, 40)); // true
console.log(profitableGamble(6, 3, 30)); // false
<details><summary style="cursor:pointer">Solution</summary>
function profitableGamble(prob, prize, pay){
if (prob*prize > pay) {
return (true)
}
else {
return (false)
}
};
</details>
15. Takes an array of numbers, returns both the minimum and maximum numbers, in that order.
function minMax(arr){
//Write Your solution Here
};
console.log(minMax([2, -1, 5])); // [ -1, 5 ]
console.log(minMax([0, 5, 2])); // [ 0, 5 ]
console.log(minMax([2, -5, -1])); // [ -5, 2 ]
<details><summary style="cursor:pointer">Solution</summary>
function minMax(arr){
arr.sort(function(a, b){return(a - b)})
return [arr[0], arr[arr.length - 1]]
};
</details>
16. Create a function that returns true if the first array can be nested inside the second.
arr1 can be nested inside arr2 if:
arr1's min is greater than arr2's min.
arr1's max is less than arr2's max.
function canNest(arr1, arr2){
//Write Your solution Here
};
console.log(canNest([3, 1], [4, 0])); // true
console.log(canNest([9, 9, 8], [8, 9])); // false
console.log(canNest([1, 2, 3, 4], [0, 6])); // true
<details><summary style="cursor:pointer"