write frontend developer All javascript logic code
// Function to get user input and perform calculations
function calculate() {
// Get input values from the form
var num1 = parseInt(document.getElementById('num1').value);
var num2 = parseInt(document.getElementById('num2').value);
// Perform calculations
var sum = num1 + num2;
var difference = num1 - num2;
var product = num1 * num2;
var quotient = num1 / num2;
// Display results
document.getElementById('sum').innerHTML = 'Sum: ' + sum;
document.getElementById('difference').innerHTML = 'Difference: ' + difference;
document.getElementById('product').innerHTML = 'Product: ' + product;
document.getElementById('quotient').innerHTML = 'Quotient: ' + quotient;
}
// Add event listener to the calculate button
document.getElementById('calculateBtn').addEventListener('click', calculate);
Calculate the sum of an array of numbers:
Contents
hide
function sumArray(numbers) {
return numbers.reduce((total, num) => total + num, 0);
}
Find the maximum number in an array:
function findMaxNumber(numbers) {
return Math.max(...numbers);
Check if a number is even:
function isEven(number) {
return number % 2 === 0;
}
Check if a string is a palindrome:
function isPalindrome(str) {
const reversedStr = str.split('').reverse().join('');
return str === reversedStr;
}
Generate a random number between a given range:
function generateRandomNumber(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
// 1. Create a function that takes a string and returns the number of words in the string.
function numberOfWords(string) {
return string.split(" ").length;
}
// 2. Create a function that takes a string and returns the longest word in the string.
function longestWord(string) {
var words = string.split(" ");
var longestWord = words[0];
for (var i = 1; i < words.length; i++) {
if (words[i].length > longestWord.length) {
longestWord = words[i];
}
}
return longestWord;
}
// 3. Create a function that takes a string and returns the shortest word in the string.
function shortestWord(string) {
var words = string.split(" ");
var shortestWord = words[0];
for (var i = 1; i < words.length; i++) {
if (words[i].length < shortestWord.length) {
shortestWord = words[i];
}
}
return shortestWord;
}
// 4. Create a function that takes a string and returns an array of all the words in the string.
function wordsInString(string) {
return string.split(" ");
}
// 5. Create a function that takes an array of strings and returns a string with all the strings concatenated together.
function concatenateStrings(strings) {
var concatenatedString = "";
for (var i = 0; i < strings.length; i++) {
concatenatedString += strings[i];
}
return concatenatedString;
}
// 6. Create a function that takes an array of numbers and returns the sum of all the numbers in the array.
function sumOfNumbers(numbers) {
var sum = 0;
for (var i = 0; i < numbers.length; i++) {
sum += numbers[i];
}
return sum;
}
// 7. Create a function that takes an array of numbers and returns the average of all the numbers in the array.
function averageOfNumbers(numbers) {
var sum = 0;
for (var i = 0; i < numbers.length; i++) {
sum += numbers[i];
}
return sum / numbers.length;
}
// 8. Create a function that takes an array of numbers and returns the maximum number in the array.
function maxNumber(numbers) {
var maxNumber = numbers[0];
for (var i = 1; i < numbers.length; i++) {
if (numbers[i] > maxNumber) {
maxNumber = numbers[i];
}
}
return maxNumber;
}
// 9. Create a function that takes an array of numbers and returns the minimum number in the array.
function minNumber(numbers) {
var minNumber = numbers[0];
for (var i = 1; i < numbers.length; i++) {
if (numbers[i] < minNumber) {
minNumber = numbers[i];
}
}
return minNumber;
}
// 10. Create a function that takes an array of numbers and returns an array with all the odd numbers in the array.
function oddNumbers(numbers) {
var oddNumbers = [];
for (var i = 0; i < numbers.length; i++) {
if (numbers[i] % 2 === 1) {
oddNumbers.push(numbers[i]);
}
}
return oddNumbers;
}
// 11. Create a function that takes an array of numbers and returns an array with all the even numbers in the array.
function evenNumbers(numbers) {
var evenNumbers = [];
for (var i = 0; i < numbers.length; i++) {
if (numbers[i] % 2 === 0) {
evenNumbers.push(numbers[i]);
}
}
return evenNumbers;
}
// 1. Create a function that takes two numbers as arguments and returns their sum.
function sum(a, b) {
return a + b;
}
// 2. Create a function that takes an array of numbers and returns the average.
function average(numbers) {
var sum = 0;
for (var i = 0; i < numbers.length; i++) {
sum += numbers[i];
}
return sum / numbers.length;
}
// 3. Create a function that takes a string and returns the number of characters in the string.
function length(string) {
return string.length;
}
// 4. Create a function that takes a string and returns the first character of the string.
function firstCharacter(string) {
return string[0];
}
// 5. Create a function that takes a string and returns the last character of the string.
function lastCharacter(string) {
return string[string.length - 1];
}
// 6. Create a function that takes a string and returns the string with all the spaces removed.
function removeSpaces(string) {
return string.replace(/ /g, "");
}
// 7. Create a function that takes a string and returns the string with all the letters capitalized.
function capitalize(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
// 8. Create a function that takes a string and returns the string with all the letters lowercase.
function lowercase(string) {
return string.toLowerCase();
}
// 9. Create a function that takes a string and returns the string with all the vowels removed.
function removeVowels(string) {
return string.replace(/[aeiou]/g, "");
}
// 10. Create a function that takes a string and returns the string with all the consonants removed.
function removeConsonants(string) {
return string.replace(/[^aeiou]/g, "");
}
// 11. Create a function that takes a string and returns the string with all the numbers removed.
function removeNumbers(string) {
return string.replace(/\d/g, "");
}
// 12. Create a function that takes a string and returns the string with all the special characters removed.
function removeSpecialCharacters(string) {
return string.replace(/[^\w\s]/g, "");
}
// 13. Create a function that takes a string and returns the string with all the HTML tags removed.
function removeHTMLTags(string) {
return string.replace(/<[^>]+>/g, "");
}
// 14. Create a function that takes a string and returns the string with all the JavaScript code removed.
function removeJavaScriptCode(string) {
return string.replace(/<script[^>]*>.*?<\/script>/g, "");
}
// 15. Create a function that takes a string and returns the string with all the CSS code removed.
function removeCSSCode(string) {
return string.replace(/<style[^>]*>.*?<\/style>/g, "");
}
// 16. Create a function that takes a string and returns the string with all the comments removed.
function removeComments(string) {
return string.replace(/<!--.*?-->/g, "");
}
// 17. Create a function that takes a string and returns the string with all the line breaks removed.
function removeLineBreaks(string) {
return string.replace(/\r\n|\r|\n/g, "");
}
// 18. Create a function that takes a string and returns the string with all the tabs removed.
function removeTabs(string) {
return string.replace(/\t/g, "");
}
// 19. Create a function that takes a string and returns the string with all the leading and trailing spaces removed.
function trim(string) {
return string.trim();
}
// 20. Create a function that takes a string and returns the string with all the leading spaces removed.
function ltrim(string) {
return string.replace(/^\s+/
Using the compound assignment operator:
let message = 'Hello, ';
message += 'World!';
console.log(message); // Output: Hello, World!
Using the concatenation operator:
let message = 'Hello, ';
message = message + 'World!';
console.log(message); // Output: Hello, World!
Using an anonymous function as a callback function
You can use an anonymous function as a callback function by passing it directly as an argument to another function. Here’s an example:
function process(callback) {
// Do some processing
callback();
}
process(function() {
console.log('Processing complete!');
});
Using an anonymous function as an event listener
- To use an anonymous function as an event listener, you can assign it directly to the event property using the
addEventListener()
method. Here’s an example:
const button = document.querySelector('button');
button.addEventListener('click', function() {
console.log('Button clicked!');
});
Looping through an array
To loop through an array, you can use a for
loop, a forEach()
method, or other array iteration methods. Here are examples of both approaches:
Using a for
loop:
//Using a for loop:
const myArray = [1, 2, 3, 4, 5];
for (let i = 0; i < myArray.length; i++) {
console.log(myArray[i]);
}
// Using the forEach() method:
const myArray = [1, 2, 3, 4, 5];
myArray.forEach((element) => {
console.log(element);
});