OneCompiler

javascript practice

162
  let dimensions = [
  	{
  		rectangle1: {
  			length: 10,
  			width: 5
  		}
  	},
  	{
  		rectangle2: {
  			length: 20,
  			width: 10
  		}
  	},
  	{
  		rectangle3: {
  			length: 30,
  			width: 15
  		}
  	}
  ];
  
  dimensions.forEach(rectangleObj => {
  	const rectangle = Object.keys(rectangleObj)
  	const { length, width } = rectangleObj[rectangle]
  	console.log(`The area of ${rectangle} is: ${length * width}`);
  });
  
  //==================================================================================
  // * Array function 
  
  // 1. push()
  let fruits = ['apple', 'banana', 'orange', 'kiwi', 'grape', 'strawberry'];
  fruits.push('peach');
  console.log(fruits); // Output: ['apple', 'banana', 'orange','kiwi', 'grape', 'strawberry', 'peach']
  
  // // 2. pop()
  let lastFruit = fruits.pop();
  console.log(lastFruit); // Output: 'peach'
  
  // // 3. shift()
  let firstFruit = fruits.shift();
  console.log(firstFruit); // Output: 'apple'
  
  // // 4. unshift()
  fruits.unshift('strawberry');
  console.log(fruits); // Output: ['strawberry', 'apple', 'banana', 'orange', 'kiwi', 'grape', 'strawberry']
  
  // // 5. concat()
  let moreFruits = ['grape', 'kiwi'];
  let allFruits = fruits.concat(moreFruits);
  // let allFruits = [...fruits,...moreFruits]
  console.log(allFruits); // Output: ['apple', 'banana', 'orange', 'kiwi', 'grape', 'strawberry', 'grape', 'kiwi']
  
  // // 6. slice()
  let someFruits = fruits.slice(0, 3);
  console.log(someFruits); // Output: [ 'apple', 'banana', 'orange' ]
  
  // // 7. splice()
  let removedFruits = fruits.splice(0, 2, 'peach', 'plum');
  console.log(fruits); // Output: [ 'peach', 'plum', 'orange', 'kiwi', 'grape', 'strawberry' ]
  
  // // 8. forEach()
  fruits.forEach(fruit => console.log(fruit)); // Output: apple banana orange kiwi grape strawberry
  
  // // 9. map()
  let fruitLengths = fruits.map(fruit => fruit.length);
  console.log(fruitLengths); //Output: [ 5, 6, 6, 4, 5, 10 ]
  
  // // 10. filter()
  let shortFruits = fruits.filter(fruit => fruit.length <= 5);
  console.log(shortFruits); // Output: [ 'apple', 'kiwi', 'grape' ]
  
  // // 11. reduce()
  let totalLength = fruits.reduce((acc, fruit) => acc + fruit.length, 0);
  console.log(totalLength); // Output: 36
  
  // // 12. every()
  let allShort = fruits.every(fruit => fruit.length <= 5);
  console.log(allShort); // Output: false
  
  // // 13. some()
  let hasShortFruit = fruits.some(fruit => fruit.length <= 5);
  console.log(hasShortFruit); // Output: true
  
  // // 14. find()
  let foundFruit = fruits.find(fruit => fruit.startsWith('a'));
  console.log(foundFruit); // Output: 'apple'
  
  // // 15. findIndex()
  let foundIndex = fruits.findIndex(fruit => fruit.startsWith('b'));
  console.log(foundIndex); // Output: 1
  
  // // 16. indexOf()
  let index = fruits.indexOf('apple');
  console.log(index); // Output: 1
  
  // // 17. lastIndexOf()
  let lastIndex = fruits.lastIndexOf('kiwi');
  console.log(lastIndex); // Output: 1
  
  // // 18. includes()
  let includesFruit = fruits.includes('banana');
  console.log(includesFruit); // Output: true
  
  // // 19. isArray()
  let isArray = Array.isArray(fruits);
  console.log(isArray); // Output: true
  
  // // 20. JavaScript Array splice() Method is an inbuilt method in JavaScript that is used to change the contents of an array by removing or replacing existing elements
  // and/or adding new elements. It modifies the original array and returns an array of the removed elements.
  
  let webDvlop = ["HTML", "CSS", "JS", "Bootstrap"]
  console.log(webDvlop);
  
  // Add 'React_Native' and 'Php' after removing 'JS'.
  let removed = webDvlop.splice(2, 1, 'PHP', 'React_Native')
  
  const array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
  const removedElementSlice = array.slice(0, 5);
  console.log("removedElementSlice",removedElementSlice); // Output: [ 2, 3, 4, 5 ]
  console.log(array); // Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] (original array not modified)
  const removedElementSplice = array.splice(0, 5);
  console.log("removedElementSplice",removedElementSplice); // Output: [ 2, 3, 4, 5, 6 ]
  console.log(array); // Output: [ 1, 7, 8, 9, 10 ] (original array modified)



  //=====================================
  const numbArr = [-10,-5,-3,-9,-2]
  const findMax= Math.max(...numbArr)

  console.log("findMax==>",findMax) // output: -2
  
  //========================================
  
    const factorial = (num) => {
      let fact = 1
      for (let i = 1; i <= num; i++) {
        fact = fact * i
      }
      return fact
    }
    console.log("factorial",factorial(5)) //output: 25

//======================

const calculateAverage = (val) => {
        let val2=JSON.stringify(val).split('')
        console.log("cas",val2)

        let sum = val2.reduce((acc,curVal)=> acc + parseInt(curVal), 0)
       let average = sum / val2.length
      return { sum, average };
}
console.log(calculateAverage(1234))
//==================================

const removeDuplicates = (arr) => {
  let newArr = [...new Set(arr)]
  return newArr
} 
console.log(removeDuplicates([1,2,3,2,1,4])) //output: [1,2,3,4]

//===========================================

  const a = [1,2,3,[4,[5,6]],7,8]

function flattenArray(a, flattenedArray) {
for (let i = 0; i < a.length ; i++) {
if(typeof a[i] === 'number'){
flattenedArray.push(a[i])
}else{
flattenArray(a[i], flattenedArray)
}
}
return flattenedArray
}

console.log("flattenArray", flattenArray(a, [])) // output: [1,2,3,4,5,6,7,8]