From 5ef15da4e68fd2504e8f9a67a361119bce9458b4 Mon Sep 17 00:00:00 2001 From: mohamedali0122 Date: Thu, 15 Feb 2024 17:59:23 +0300 Subject: [PATCH 1/3] done file array baeics --- arrays-basics.js | 60 ++++++++++++++++++++++++++++++++++++++---------- index.html | 2 +- 2 files changed, 49 insertions(+), 13 deletions(-) diff --git a/arrays-basics.js b/arrays-basics.js index 0de468d..884c98b 100644 --- a/arrays-basics.js +++ b/arrays-basics.js @@ -2,46 +2,82 @@ Task 8 (logArray): Create a function logArray that receives an array as a parameter and logs each item in the array. **************************************************************/ -function logArray(arr) {} -// logArray([1, 2, 3, 4, 5]); +function logArray(arr) { + arr.forEach((value , index) => {console.log(value);}) +}; + +logArray([1, 2, 3, 4, 5]); /************************************************************** Task 9 (logArrayWithIndex): Create a function logArrayWithIndex that receives an array as a parameter and logs each item in the array along with its index. **************************************************************/ -function logArrayWithIndex(arr) {} -// logArrayWithIndex(["apple", "banana", "orange"]); +function logArrayWithIndex(arr) { + arr.forEach((value, index) => { + console.log(value, " ", index); + }); +} + +logArrayWithIndex(["apple", "banana", "orange"]); /************************************************************** Task 10 (logEvenNumbers): Create a function logEvenNumbers that receives an array of numbers as a parameter and logs only the even numbers in the array. **************************************************************/ -function logEvenNumbers(arr) {} -// logEvenNumbers([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); +function logEvenNumbers(arr) { + arr.forEach((value, index) => { + if (value % 2 == 0) { + console.log(value); + } + }); +} + +logEvenNumbers([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); /************************************************************** Task 11 (logArrayBackwards): Create a function logArrayBackwards that receives an array as a parameter and logs each item in the array in reverse order. **************************************************************/ -function logArrayBackwards(arr) {} -// logArrayBackwards(["one", "two", "three", "four"]); +function logArrayBackwards(arr) { + for (let i = arr.length - 1; i >= 0; i--) { + console.log(arr[i]); + } +} + +logArrayBackwards(["one", "two", "three", "four"]); /************************************************************** Task 12 (logLastItem): Create a function logLastItem that receives an array as a parameter and logs the last item in the array. **************************************************************/ -function logLastItem(arr) {} -// logLastItem(["a", "b", "c", "d"]); + function logLastItem(arr) { + arr.map((value, index) => { + console.log(value); + + if (index == arr.length - 1) { + console.log("---------------------"); + console.log("last item is :", value); + } + }); +} + +logLastItem(["a", "b", "c", "d"]); /************************************************************** Task 13 (logArrayInChunks): Create a function logArrayInChunks that receives an array and a chunk size as parameters and logs the items in the array in chunks of the specified size. **************************************************************/ -function logArrayInChunks(arr, chunkSize) {} -// logArrayInChunks([1, 2, 3, 4, 5, 6, 7, 8, 9], 3); +function logArrayInChunks(arr, chunkSize) { + for (let i = 0; i < arr.length; i=i+chunkSize) { + const count = arr.slice(i, i +chunkSize); + console.log(count); + } +} + +logArrayInChunks([1, 2, 3, 4, 5, 6, 7, 8, 9], 3); diff --git a/index.html b/index.html index d971aeb..100b99f 100644 --- a/index.html +++ b/index.html @@ -7,6 +7,6 @@ Arrays -