Write a function that takes an array of numbers as input and returns the sum of the squares of all the odd numbers in the array.
function sumOfSquaresOfOdds(arr) {
return arr.filter(num => num % 2 !== 0).reduce((acc, num) => acc + num ** 2, 0);
}
// Example usage
const nums = [1, 2, 3, 4, 5];
console.log(sumOfSquaresOfOdds(nums)); // Output: 35
Write a function that takes two arrays of strings as input, and returns an array containing all the strings that appear in both input arrays.
function commonStrings(arr1, arr2) {
const set1 = new Set(arr1);
const set2 = new Set(arr2);
const common = [];
set1.forEach(str => {
if (set2.has(str)) {
common.push(str);
}
});
return common;
}
// Example usage
const arr1 = ['apple', 'banana', 'orange'];
const arr2 = ['banana', 'kiwi', 'orange'];
console.log(commonStrings(arr1, arr2)); // Output: ['banana', 'orange']
Write a function that takes a string as input and returns the most common letter in the string. If there is a tie for the most common letter, the function should return an array of all the tied letters.
function mostCommonLetter(str) {
const letterCount = {};
for (let i = 0; i < str.length; i++) {
const char = str[i].toLowerCase();
if (/\w/.test(char)) {
letterCount[char] = (letterCount[char] || 0) + 1;
}
}
const maxCount = Math.max(...Object.values(letterCount));
const mostCommon = Object.keys(letterCount).filter(key => letterCount[key] === maxCount);
return mostCommon.length === 1 ? mostCommon[0] : mostCommon;
}
// Example usage
const str = 'hello world';
console.log(mostCommonLetter(str)); // Output: 'l'
Write a function that takes an array of numbers as input and returns the median value of the numbers in the array.
function median(arr) {
const sorted = arr.sort((a, b) => a - b);
const mid = Math.floor(sorted.length / 2);
return sorted.length % 2 !== 0 ? sorted[mid] : (sorted[mid - 1] + sorted[mid]) / 2;
}
// Example usage
const nums = [1, 3, 2, 5, 4];
console.log(median(nums)); // Output: 3
Write a function that takes a string as input and returns a new string with the first letter of each word capitalized, and all other letters lowercase.
function capitalizeWords(str) {
return str.split(' ').map(word => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase()).join(' ');
}
// Example usage
const str = 'the quick brown fox';
console.log(capitalizeWords(str)); // Output: 'The Quick Brown Fox'
Write a function that takes an array of objects as input, where each object represents a person with a name and an age, and returns the name of the oldest person in the array.
function oldestPerson(people) {
const oldest = people.reduce((acc, person) => person.age > acc.age ? person : acc, people[0]);
return oldest.name;
}
// Example usage
const people = [
{ name: 'Alice', age: 25 },
{ name: 'Bob', age: 30 },
{ name: 'Charlie', age: 20 }
];
console.log(oldestPerson(people)); // Output: 'Bob'
Write a function that takes a string as input and returns true if the string is a palindrome (reads the same forwards and backwards), and false otherwise.
function factorial(num) {
if (num === 0) {
return 1;
} else {
return num * factorial(num - 1);
}
}
// Example usage
console.log(factorial(5)); // Output: 120
Write a function that takes an array of numbers as input and returns the number of unique values in the array.
function isPalindrome(str) {
const len = str.length;
for (let i = 0; i < Math.floor(len / 2); i++) {
if (str[i] !== str[len - 1 - i]) {
return false;
}
}
return true;
}
// Example usage
console.log(isPalindrome('racecar')); // Output: true
Write a function that takes a string as input and returns the longest word in the string.
function gcd(num1, num2) {
if (num2 === 0) {
return num1;
} else {
return gcd(num2, num1 % num2);
}
}
// Example usage
console.log(gcd(12, 18)); // Output: 6
Write a function that takes an array of numbers as input and returns an array containing all the unique pairs of numbers that add up to a specified target value.
function removeDuplicates(arr) {
return arr.filter((item, index) => arr.indexOf(item) === index);
}
// Example usage
const nums = [1, 2, 3, 2, 4, 3];
console.log(removeDuplicates(nums)); // Output: [1, 2, 3, 4]
Learn more
JavaScript ile neler yapılabilir? JavaScript öğrenmek ne kadar sürer? JavaScript hangi program? JavaScript neden popüler?¿Qué proyectos se pueden hacer con JavaScript? ¿Qué es un proyecto en JavaScript? ¿Cómo empezar un proyecto de JavaScript? ¿Qué programas se han hecho con JavaScript? Wird JavaScript noch benötigt? Was kann man alles mit JavaScript machen? Ist JavaScript für Anfänger? Wie schwierig ist JavaScript? مشاريع جافا سكريبت للمبتدئين مشاريع جافا سكريبت جاهزة pdf مشروع جافا سكريبت javascript مشروع جافا سكريبت github تفعيل جافا سكريبت على الهاتف مشاريع جافا للمبتدئين جافا سكريبت تحميل تحميل جافا سكريبت للاندرويد
0 Comments