Created
February 11, 2022 11:36
-
-
Save SujalShah3234/b97b9368f9438e860575843b7c43c5f0 to your computer and use it in GitHub Desktop.
Code Academy 10 JavaScript code challenges for beginners
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
// Challenges | |
// - Try to make the solution to this problem as efficiently as possible. | |
// - Try to make optimized output | |
// Print all even numbers from 0 – 10 | |
function printEvenNumbers(start = 0, end) { | |
for (let i = start; i <= end; i++) { | |
if (i % 2 === 0) console.log(i); | |
} | |
} | |
printEvenNumbers(0, 10); | |
// Print a table containing multiplication tables from 1 to 10 | |
function printTable(start = 1, end) { | |
for (let i = start; i <= end; i++) { | |
for (let j = 1; j <= 10; j++) { | |
console.log(`${i} x ${j} = ${i * j}`); | |
} | |
} | |
} | |
printTable(1, 10); | |
// The function should include the input in kilometers and return the answer in miles. | |
function convertKilometersToMiles(kilometers) { | |
return kilometers * 0.621371; | |
} | |
// Calculate the sum of numbers within an array | |
function sumOfNumbers(numbers) { | |
return numbers.reduce((acc, curr) => acc + curr); | |
} | |
sumOfNumbers([1, 2, 3, 4, 5]); | |
// Create a function that reverses an array | |
function reverseArray(array) { | |
return array.reverse(); | |
} | |
reverseArray([1, 2, 3, 4, 5]); | |
// Sort an array from lowest to highest | |
function sortArray(array) { | |
return array.sort((a, b) => a - b); | |
} | |
sortArray([1, 2, 3, 4, 5]); | |
// Create a function that filters out negative numbers | |
function filterNegativeNumbers(numbers) { | |
return numbers.filter((number) => number >= 0); | |
} | |
filterNegativeNumbers([1, 2, 3, 4, 5, -1, -2, -3, -4, -5]); | |
// Remove the spaces found in a string | |
function removeSpaces(string) { | |
return string.replace(/\s/g, ''); | |
} | |
removeSpaces('Hello World'); | |
//Return a Boolean if a number is divisible by 10 | |
function isDivisibleBy10(number) { | |
return number % 10 === 0; | |
} | |
isDivisibleBy10(10); | |
// Return the number of vowels in a string | |
function countVowels(string) { | |
return string.match(/[aeiou]/gi).length; | |
} | |
countVowels('Hello World'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment