From d21d6a30a0b8924bf26e0a9324cdebab47772485 Mon Sep 17 00:00:00 2001 From: Ashutosh Sharma Date: Sun, 24 Sep 2023 23:36:20 +0530 Subject: [PATCH] java-arrays-refresher --- solutions/day-01/arraysExercise1.js | 60 ++++++++++++++++++++++++++ solutions/day-01/tempCodeRunnerFile.js | 5 +++ 2 files changed, 65 insertions(+) create mode 100644 solutions/day-01/arraysExercise1.js create mode 100644 solutions/day-01/tempCodeRunnerFile.js diff --git a/solutions/day-01/arraysExercise1.js b/solutions/day-01/arraysExercise1.js new file mode 100644 index 0000000..f8e48e1 --- /dev/null +++ b/solutions/day-01/arraysExercise1.js @@ -0,0 +1,60 @@ +// declare empty array +let arr = new Array(); +let arr1 = new Array(6) + +// find length of array +console.log(arr1.length) + +// get the first, middle and last item of the array +arr1 = [1,2,3,4,5,6] +let first = arr1[0]; +let last = arr1[arr1.length-1] +let middle = arr1[arr1.length/2-1] + +console.log(`first: ${first} \nmiddle: ${middle} \nlast: ${last}`) + +// +itCompanies = ['Facebook','Google','Microsoft','Apple','IBM','Oracle','Amazon'] +console.log(itCompanies) + +// print each company +for(const company of itCompanies){ + console.log(company) +} + +console.log("\n") + +itCompanies.forEach(element => { + console.log(element) +}); + +// change the company name to uppercase and print one by one +itCompanies.forEach(element => { + element = element.toUpperCase() + console.log(element) +}); + +let company1 = 'Amazon' +let company2 = 'PayPal' + +if(itCompanies.includes(company2)){ + console.log("The IT company is " + company1) +} +else{ + console.log("It is not an IT company") +} + +// Filter out companies which have more than one 'o' without the filter method + +// sort the array using sort method - the sorting is based on alphabetical order +console.log(itCompanies.sort()) + +// slicing the first three companies from the array +console.log(itCompanies.splice(0,3)) + +// slicing the last three companies from the array +console.log(itCompanies.splice(arr.length-3)) + +// ----------------------------------------------------------------------------- +// EXERCISE: LEVEL 2 + diff --git a/solutions/day-01/tempCodeRunnerFile.js b/solutions/day-01/tempCodeRunnerFile.js new file mode 100644 index 0000000..649ad66 --- /dev/null +++ b/solutions/day-01/tempCodeRunnerFile.js @@ -0,0 +1,5 @@ +if(itCompanies.includes(company1)){ +// console.log("The IT company is" + company1) +// } +// else{ +// co \ No newline at end of file