new file: 01_Day_JavaScript_Refresher/arrays/README.md new file: 01_Day_JavaScript_Refresher/arrays/array-methods.js new file: 01_Day_JavaScript_Refresher/arrays/excercise1.js new file: 01_Day_JavaScript_Refresher/arrays/excercise2/countries.js new file: 01_Day_JavaScript_Refresher/arrays/excercise2/main.js new file: 01_Day_JavaScript_Refresher/arrays/excercise2/web_techs.js new file: 01_Day_JavaScript_Refresher/arrays/excercise3/excercise3.jspull/253/head
parent
d0c066fcb9
commit
61e91eaf1e
@ -0,0 +1 @@
|
||||
#trabajando con arrays en JavaScript
|
@ -0,0 +1,138 @@
|
||||
// These are some of the available methods to deal with arrays:
|
||||
|
||||
// printing title, result of the method, print an empty line
|
||||
|
||||
function print(title, results){
|
||||
console.log("\n **********************" + title + "*********************\n")
|
||||
results.forEach(element => {
|
||||
console.log(element)
|
||||
});
|
||||
console.log('\n#######################END########################\n\n\n')
|
||||
}
|
||||
|
||||
|
||||
// Array
|
||||
const arr = Array()
|
||||
print('Empty Array', ['array = ' + arr])
|
||||
|
||||
|
||||
// length
|
||||
const arrayLenght1 = Array(2)
|
||||
const arrayLenght2 = [1,2,3]
|
||||
print('length', [
|
||||
'array1 = ' + arrayLenght1,
|
||||
'array1 Length = ' + arrayLenght1.length,
|
||||
'array2 = ' + arrayLenght2,
|
||||
'array2 Length = ' + arrayLenght2.length
|
||||
])
|
||||
|
||||
|
||||
// concat
|
||||
const arrayConcat1 = [1,2,3]
|
||||
const arrayConcat2 = [4, 5, 6]
|
||||
const concat = arrayConcat1.concat(arrayConcat2)
|
||||
print('concat', [
|
||||
'array = ' + arrayConcat1,
|
||||
'array = ' + arrayConcat2,
|
||||
'concat = ' + concat
|
||||
])
|
||||
|
||||
|
||||
// indexOf
|
||||
const arrayIndexOf = [1,2,3,4,5]
|
||||
print('indexOf', [
|
||||
'array = ' + arrayIndexOf,
|
||||
'array.indexOf(5) = ' + arrayIndexOf.indexOf(5),
|
||||
'array.indexOf(5) = ' + arrayIndexOf.indexOf(0),
|
||||
'array.indexOf(5) = ' + arrayIndexOf.indexOf(1),
|
||||
'array.indexOf(5) = ' + arrayIndexOf.indexOf(6),
|
||||
])
|
||||
|
||||
|
||||
// // slice
|
||||
// console.log('slice')
|
||||
|
||||
|
||||
|
||||
// // splice
|
||||
// console.log('splice')
|
||||
|
||||
|
||||
|
||||
// // join
|
||||
// console.log('join')
|
||||
|
||||
|
||||
// toString
|
||||
const arrayToString = [1,2,3,4,5]
|
||||
print('toString', [
|
||||
'array filled with numbers = ' + arrayToString,
|
||||
'array.toString() = ' + arrayToString.toString()
|
||||
])
|
||||
// console.log('toString')
|
||||
|
||||
|
||||
// // includes
|
||||
const includeNumbers = [1, 2, 3, 4, 5]
|
||||
const includeWebTechs = [
|
||||
'HTML',
|
||||
'CSS',
|
||||
'JavaScript',
|
||||
'React',
|
||||
'Redux',
|
||||
'Node',
|
||||
'MongoDB',
|
||||
] // List of web technologies
|
||||
print('includes', [
|
||||
'array = ' + includeNumbers,
|
||||
'includeNumbers(5) ? ' + includeNumbers.includes(5),
|
||||
'includeNumbers(0) ? ' + includeNumbers.includes(0),
|
||||
'includeNumbers(1) ? ' + includeNumbers.includes(1),
|
||||
'includeNumbers(6) ? ' + includeNumbers.includes(6),
|
||||
'\n',
|
||||
'WebTechs = ' + includeWebTechs,
|
||||
'include(node) ? = ' + includeWebTechs.includes('Node'),
|
||||
'include(C) ? = ' + includeWebTechs.includes('C'),
|
||||
])
|
||||
|
||||
// lastIndexOf
|
||||
const arraylastIndexOf = [1,2,3,4,5,3,1,2]
|
||||
print('lastIndexOf', [
|
||||
'array = ' + arraylastIndexOf,
|
||||
'array.lastIndexOf(2) = ' + arraylastIndexOf.lastIndexOf(2),
|
||||
'array.lastIndexOf(0) = ' + arraylastIndexOf.lastIndexOf(0),
|
||||
'array.lastIndexOf(1) = ' + arraylastIndexOf.lastIndexOf(1),
|
||||
'array.lastIndexOf(4) = ' + arraylastIndexOf.lastIndexOf(4),
|
||||
'array.lastIndexOf(6) = ' + arraylastIndexOf.lastIndexOf(6)
|
||||
])
|
||||
|
||||
// // isArray
|
||||
const numbersisArray1 = [1, 2, 3, 4, 5]
|
||||
const variable = 100
|
||||
print('isArray',[
|
||||
'array1 = ' + numbersisArray1,
|
||||
'Array.isArray(numbersisArray1) ? ' + Array.isArray(numbersisArray1),
|
||||
'variable = ' + variable,
|
||||
'Array.isArray(variable) ? ' + Array.isArray(variable)
|
||||
])
|
||||
|
||||
|
||||
// // fill
|
||||
|
||||
// console.log('fill')
|
||||
|
||||
|
||||
// // push
|
||||
// console.log('push')
|
||||
|
||||
// // pop
|
||||
// console.log('pop')
|
||||
|
||||
// // shift
|
||||
// console.log('shift')
|
||||
|
||||
// // unshift
|
||||
// console.log('unshift')
|
||||
|
||||
|
||||
|
@ -0,0 +1,141 @@
|
||||
// Exercise: Level 1
|
||||
|
||||
const countries = [
|
||||
'Albania',
|
||||
'Bolivia',
|
||||
'Canada',
|
||||
'Denmark',
|
||||
'Ethiopia',
|
||||
'Finland',
|
||||
'Germany',
|
||||
'Hungary',
|
||||
'Ireland',
|
||||
'Japan',
|
||||
'Kenya',
|
||||
]
|
||||
|
||||
const webTechs = [
|
||||
'HTML',
|
||||
'CSS',
|
||||
'JavaScript',
|
||||
'React',
|
||||
'Redux',
|
||||
'Node',
|
||||
'MongoDB',
|
||||
]
|
||||
|
||||
// Declare an empty array;
|
||||
const task1 = Array()
|
||||
|
||||
|
||||
// Declare an array with more than 5 number of elements
|
||||
const task2 = [1, 2, 3, 4]
|
||||
|
||||
// Find the length of your array
|
||||
const task3 = task2.length
|
||||
// console.log(task3)
|
||||
|
||||
// Get the first item, the middle item and the last item of the array
|
||||
const task4Firt = task2[0]
|
||||
const task4Middle = task2[(Math.round(task3 - 1) / 2, 2)]
|
||||
const task4Last = task2[task3 - 1]
|
||||
// console.log(`
|
||||
// First Item: ${task4Firt}
|
||||
// Middle Item: ${task4Middle}
|
||||
// Last Item: ${task4Last}
|
||||
// `)
|
||||
|
||||
// Declare an array called mixedDataTypes, put different data types in the array and find the length of the array.
|
||||
// The array size should be greater than 5
|
||||
const mixedDataTypes = ['string', 'c', 1, false, { name: 'nico', favfood: 'pasta' }]
|
||||
// console.log(`
|
||||
// mixedDataTypes = ${mixedDataTypes}
|
||||
// mixedDataTypes.length = ${mixedDataTypes.length}
|
||||
// `)
|
||||
|
||||
|
||||
// Declare an array variable name itCompanies and assign initial values Facebook, Google, Microsoft, Apple, IBM, Oracle and Amazon
|
||||
const itCompanies = ['Facebook', 'Google', 'Microsoft', 'Apple', 'IBM', 'Oracle', 'Amazon']
|
||||
|
||||
// Print the array using console.log()
|
||||
// console.log(itCompanies)
|
||||
|
||||
// Print the number of companies in the array
|
||||
|
||||
// console.log(`Numbe of companies = ${itCompanies.length}`)
|
||||
// Print the first company, middle and last company
|
||||
|
||||
// console.log(`
|
||||
// first ${itCompanies[0]}
|
||||
// middle ${itCompanies[(Math.round((itCompanies.length - 1) / 2, 2))]}
|
||||
// last ${itCompanies.length - 1}
|
||||
// `)
|
||||
|
||||
// Print out each company
|
||||
// console.log(itCompanies)
|
||||
|
||||
// Change each company name to uppercase one by one and print them out
|
||||
// itCompanies[0] = 'FACEBOOK'
|
||||
// itCompanies[1] = 'GOOGLE'
|
||||
// itCompanies[2] = 'MICROSOFT'
|
||||
// itCompanies[3] = 'APPLE'
|
||||
// itCompanies[4] = 'IBM'
|
||||
// itCompanies[5] = 'ORACLE'
|
||||
// itCompanies[6] = 'AMAZON'
|
||||
// console.log(itCompanies)
|
||||
|
||||
// Print the array like as a sentence: Facebook, Google, Microsoft, Apple, IBM,Oracle and Amazon are big IT companies.
|
||||
// console.log(`
|
||||
// ${itCompanies.slice(0,itCompanies.length-1)} and ${itCompanies[itCompanies.length-1]} are big IT companies.
|
||||
// `)
|
||||
|
||||
// Check if a certain company exists in the itCompanies array. If it exist return the company else return a company is not found
|
||||
const companyToSearch1 = 'SiteHost'
|
||||
const companyToSearch2 = 'Amazon'
|
||||
const results1 = itCompanies.includes(companyToSearch1)
|
||||
const results2 = itCompanies.includes(companyToSearch2)
|
||||
|
||||
// console.log(`
|
||||
// SiteHost ? = ${results1}
|
||||
// Amazon ? = ${results2}
|
||||
// `)
|
||||
|
||||
// Filter out companies which have more than one 'o' without the filter method
|
||||
|
||||
|
||||
// Sort the array using sort() method
|
||||
const arrSorted = itCompanies.sort()
|
||||
// console.log(`arrSorted = ${arrSorted}`)
|
||||
|
||||
|
||||
// Reverse the array using reverse() method
|
||||
const arrReverse = arrSorted.reverse()
|
||||
console.log(`arrReverse = ${arrReverse}`)
|
||||
|
||||
|
||||
// Slice out the first 3 companies from the array
|
||||
console.log(arrReverse.slice(3))
|
||||
|
||||
// Slice out the last 3 companies from the array
|
||||
console.log(arrReverse.slice(0,3))
|
||||
|
||||
// Slice out the middle IT company or companies from the array
|
||||
|
||||
|
||||
// Remove the first IT company from the array
|
||||
console.log(`
|
||||
arrReverse.shift() = ${arrReverse.shift()}
|
||||
arrReverse = ${arrReverse}
|
||||
`)
|
||||
|
||||
// Remove the middle IT company or companies from the array
|
||||
|
||||
// Remove the last IT company from the array
|
||||
console.log(`
|
||||
arrReverse.pop() = ${arrReverse.pop()}
|
||||
arrReverse = ${arrReverse}
|
||||
`)
|
||||
|
||||
|
||||
// Remove all IT companies
|
||||
// arrReverse = ['']
|
@ -0,0 +1,15 @@
|
||||
const countries = [
|
||||
'Albania',
|
||||
'Bolivia',
|
||||
'Canada',
|
||||
'Denmark',
|
||||
'Ethiopia',
|
||||
'Finland',
|
||||
'Germany',
|
||||
'Hungary',
|
||||
'Ireland',
|
||||
'Japan',
|
||||
'Kenya',
|
||||
]
|
||||
|
||||
exports.countries = countries
|
@ -0,0 +1,62 @@
|
||||
// 1. Create a separate countries.js file and store the countries array into this file, create a separate file web_techs.js and store the webTechs array into this file. Access both file in main.js file
|
||||
|
||||
|
||||
const { countries } = require('./countries');
|
||||
const { webTechs } = require('./web_Techs');
|
||||
|
||||
// 2. First remove all the punctuations and change the string to array and count the number of words in the array
|
||||
let text = 'I love teaching and empowering people I teach HTML CSS JS React, Python'
|
||||
|
||||
text = text.split(' ')
|
||||
// console.log(text)
|
||||
// console.log(text.length)
|
||||
|
||||
|
||||
|
||||
// 3. In the following shopping cart add, remove, edit items
|
||||
|
||||
const shoppingCart = ['Milk', 'Coffe', 'Tea', 'Honey']
|
||||
|
||||
if (shoppingCart[0] !== 'Meat') {
|
||||
shoppingCart.unshift('Meat')
|
||||
}
|
||||
if (shoppingCart[0] !== 'Sugar') {
|
||||
shoppingCart.push('Sugar')
|
||||
}
|
||||
|
||||
const indexHoney = shoppingCart.indexOf('Honey')
|
||||
shoppingCart.splice(indexHoney, 1)
|
||||
|
||||
const indexTea = shoppingCart.indexOf('Tea')
|
||||
shoppingCart[indexTea] = 'Green Tea'
|
||||
|
||||
// console.log(shoppingCart)
|
||||
|
||||
|
||||
|
||||
// 4. In countries array check if 'Ethiopia' exists in the array if it exists print 'ETHIOPIA'. If it does not exist add to the countries list.
|
||||
// console.log(countries)
|
||||
|
||||
if (countries.indexOf('Ethiopia')) {
|
||||
// console.log('ETHIOPIA')
|
||||
} else {
|
||||
countries.push('ETHIOPIA')
|
||||
}
|
||||
|
||||
|
||||
|
||||
// 5. In the webTechs array check if Sass exists in the array and if it exists print 'Sass is a CSS preprocess'. If it does not exist add Sass to the array and print the array.
|
||||
if (webTechs.indexOf('Sass')) {
|
||||
// console.log('Sass is a CSS preprocess')
|
||||
} else {
|
||||
webTechs.push('Sass')
|
||||
}
|
||||
|
||||
|
||||
// 6. Concatenate the following two variables and store it in a fullStack variable.
|
||||
|
||||
const frontEnd = ['HTML', 'CSS', 'JS', 'React', 'Redux']
|
||||
const backEnd = ['Node', 'Express', 'MongoDB']
|
||||
const fullStack = frontEnd.concat(backEnd)
|
||||
|
||||
console.log(fullStack)
|
@ -0,0 +1,11 @@
|
||||
const webTechs = [
|
||||
'HTML',
|
||||
'CSS',
|
||||
'JavaScript',
|
||||
'React',
|
||||
'Redux',
|
||||
'Node',
|
||||
'MongoDB',
|
||||
]
|
||||
|
||||
exports.webTechs = webTechs
|
@ -0,0 +1,65 @@
|
||||
// Exercise: Level 3
|
||||
|
||||
// The following is an array of 10 students ages: js const ages = [19, 22, 19, 24, 20, 25, 26, 24, 25, 24] - Sort the array and find the min and max age - Find the median age(one middle item or two middle items divided by two) - Find the average age(all items divided by number of items) - Find the range of the ages(max minus min) - Compare the value of (min - average) and (max - average), use abs() method
|
||||
// 1.Slice the first ten countries from the countries array
|
||||
|
||||
const ages = [19, 22, 19, 24, 20, 25, 26, 24, 25, 24]
|
||||
|
||||
/*
|
||||
Required:
|
||||
- sort the Age array
|
||||
- find the min and max age
|
||||
- find the median age (one middle item or two middle items divided by two)
|
||||
- find the avg age (all items divided by numbers of items)
|
||||
- range of ages (max - min)
|
||||
- Compare values (Min - Avg)
|
||||
- Compare values (max - avg)
|
||||
- MUST use abd() for the values.
|
||||
*/
|
||||
ages.sort()
|
||||
const length = ages.length
|
||||
const isEven = length % 2 === 0 ? true : false
|
||||
const min = ages[0]
|
||||
const max = ages[length - 1]
|
||||
let medianAge
|
||||
function sumArray(arr) {
|
||||
return arr.reduce((total, currentValue) => total + currentValue);
|
||||
}
|
||||
let avg = sumArray(ages) / length
|
||||
const rangeOfAges = Math.abs(max - min)
|
||||
const comparingMinAvg = (Math.abs(min - avg)).toFixed(2)
|
||||
const comparingMaxAvg = (Math.abs(max - avg)).toFixed(2)
|
||||
|
||||
if (isEven) {
|
||||
const index1 = Math.round(length / 2, 2)
|
||||
const index2 = index1 + 1
|
||||
medianAge = (ages[index1] + ages[index2]) / 2
|
||||
|
||||
} else {
|
||||
const index1 = (length - 1) / 2
|
||||
console.log('index = ' + index1)
|
||||
medianAge = ages[index1] / 2
|
||||
}
|
||||
|
||||
|
||||
|
||||
console.log(`
|
||||
Array Sorted = ${ages}
|
||||
length = ${length}
|
||||
is the array even? ${isEven}
|
||||
min = ${min}
|
||||
max = ${max}
|
||||
median age = ${medianAge}
|
||||
Average age = ${avg}
|
||||
range of ages = ${rangeOfAges}
|
||||
Values to compare (min - Avg) = ${comparingMinAvg}
|
||||
Values to compare (max - Avg) = ${comparingMaxAvg}
|
||||
`)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
Loading…
Reference in new issue