[go: up one dir, main page]

0% found this document useful (0 votes)
15 views9 pages

Arrays

Dish soch h skbibsoc bhid hal Fr Chl ý dj UGC

Uploaded by

namandaali72
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views9 pages

Arrays

Dish soch h skbibsoc bhid hal Fr Chl ý dj UGC

Uploaded by

namandaali72
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Javascript Notes

Arrays

Add Element to End of Array:

function addElementToEnd(arr, element) {

arr.push(element);

return arr;

Remove Last Element from Array:

function removeLastElement(arr) {

return arr.pop();

Sum of Array Elements:

function sumArray(arr) {

return arr.reduce((sum, num) => sum + num, 0);

Reverse Array:

function reverseArray(arr) {
return arr.reverse();

Merge Arrays without Duplicates:

function mergeArrays(arr1, arr2) {

const mergedArray = [...new Set([...arr1, ...arr2])];

return mergedArray;

Intersection of Two Arrays:

function intersectionOfArrays(arr1, arr2) {

return arr1.filter(value => arr2.includes(value));

Capitalize First Letter of Strings in Array:

function capitalizeStrings(arr) {

return arr.map(str => str.charAt(0).toUpperCase() + str.slice(1));

Find Longest String in Array:

function findLongestString(arr) {

return arr.reduce((longest, current) => (current.length > longest.length ? current : longest), '');

}
Strings
Reverse a String:

function reverseString(str) {

return str.split('').reverse().join('');

Check if String is a Palindrome:

function isPalindrome(str) {

return str === str.split('').reverse().join('');

Convert String to Title Case:

function titleCase(str) {

return str.split(' ').map(word => word.charAt(0).toUpperCase() + word.slice(1)).join(' ');

Find the Longest Word in a String:

function longestWord(str) {

return str.split(' ').reduce((longest, current) => (current.length > longest.length ? current : longest),
'');

Count the Number of Vowels in a String:

function countVowels(str) {

const vowels = str.match(/[aeiou]/gi);


return vowels ? vowels.length : 0;

Check if String Contains a Substring:

function containsSubstring(mainString, substring) {

return mainString.includes(substring);

Count Occurrences of a Character in a String:

function countOccurrences(str, char) {

return str.split('').filter(ch => ch === char).length;

Replace a Substring in a String:

function replaceSubstring(mainString, oldSubstring, newSubstring) {

return mainString.replace(oldSubstring, newSubstring);

Objects
Add a New Key-Value Pair to an Object:

function addKeyValue(obj, key, value) {

obj[key] = value;

return obj;

}
Delete a Property from an Object:

function deleteProperty(obj, key) {

delete obj[key];

return obj;

Check if a Key Exists in an Object:

function keyExists(obj, key) {

return key in obj;

Get All Keys of an Object:

function getAllKeys(obj) {

return Object.keys(obj);

Get All Values of an Object:

function getAllValues(obj) {

return Object.values(obj);

Find the Length of an Object (number of properties):

function objectLength(obj) {

return Object.keys(obj).length;
}

Merge Two Objects:

function mergeObjects(obj1, obj2) {

return { ...obj1, ...obj2 };

Deep Copy an Object:

function deepCopy(obj) {

return JSON.parse(JSON.stringify(obj));

DOM

Change the Text of an HTML Element:

document.getElementById('myText').textContent = 'Hello World';

Hide an HTML Element:

document.getElementById('myElement').style.display = 'none';

Show a Hidden HTML Element:

document.getElementById('myHiddenElement').style.display = 'block';

Create a New HTML Element and Add It to the DOM:


const newParagraph = document.createElement('p');

newParagraph.textContent = 'Hello, DOM!';

document.body.appendChild(newParagraph);

Remove an HTML Element from the DOM:

const elementToRemove = document.getElementById('myElementToRemove');

elementToRemove.parentNode.removeChild(elementToRemove);

Set an Attribute for an HTML Element:

document.getElementById('myImage').setAttribute('src', 'image.jpg');

Get the Value of an Input Field:

const inputValue = document.getElementById('myInput').value;

console.log(inputValue);

Change the Style of an HTML Element:

document.getElementById('myElement').style.backgroundColor = 'blue';

Functions

Write a Function that Returns the Square of a Number:

function squareNumber(num) {

return num ** 2;

}
Write a Function to Check if a Number is Even or Odd:

function checkEvenOdd(num) {

return num % 2 === 0 ? 'even' : 'odd';

Write a Function that Concatenates Two Arrays:

function concatenateArrays(arr1, arr2) {

return arr1.concat(arr2);

Write a Function that Converts Hours into Seconds:

function hoursToSeconds(hours) {

return hours * 3600;

Write a Function to Calculate the Factorial of a Number:

function factorial(num) {

if (num === 0 || num === 1) {

return 1;

} else {

return num * factorial(num - 1);

}
Write a Function that Returns the N-th Fibonacci Number:

function fibonacci(n) {

if (n <= 1) {

return n;

} else {

return fibonacci(n - 1) + fibonacci(n - 2);

Write a Function to Check if a String Ends with a Given Suffix:

function endsWithSuffix(str, suffix) {

return str.endsWith(suffix);

Write a Function to Find the Greatest Number in an Array:

function findGreatestNumber(arr) {

return Math.max(...arr);

You might also like