...?첫날 배열관련 작성하는데도 한세월이다 이제 Ex1했음

pull/63/head
Seungmin Baek 5 years ago
parent 008c282632
commit 4e39e124b2

@ -0,0 +1,135 @@
// Declare an empty array;
const myarr1 = []
// Declare an array with more than 5 number of elements
const myarr2 = [1, 2, 3, 4, 5]
// Find the length of your array
console.log(myarr2.length)
// Get the first item, the middle item and the last item of the array
let myarr2_length = myarr2.length
let myarr2_middle = 0
if (myarr2_length%2 === 0) {
myarr2_middle = myarr2_length/2
console.log("myarr2_middle: ", myarr2_middle)
} else {
myarr2_middle = (myarr2_length - 1)/2
console.log("myarr2_middle: ", myarr2_middle)
}
console.log("first item:\t", myarr2[0], " / middle item:\t", myarr2[myarr2_middle], " / last item:\t", myarr2[myarr2_length - 1])
// 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 = [
1234,
"myName",
{class: 'math', numberOfStudents: 32 },
{classes: ['math', 'science', 'literature']},
true,
"1234",
['a', 'b', 'c']
]
console.log(mixedDataTypes)
// Declare an array variable name itCompanies and assign initial values Facebook, Google, Microsoft, Apple, IBM, Oracle and Amazon
const Companies = ['Facebook', 'Google', 'Microsoft', 'Apple', 'IBM', 'Oracle', 'Amazon']
// Print the array using console.log()
console.log(Companies)
// Print the number of companies in the array
console.log(Companies.length)
// Print the first company, middle and last company
let companyLength = Companies.length
let companyMiddle = 0
if (companyLength%2 === 0) {
companyMiddle = companyLength/2
} else {
companyMiddle = (companyLength - 1)/2
}
console.log("first item:\t", Companies[0], " / middle item:\t", Companies[companyMiddle], " / last item:\t", Companies[companyLength - 1])
// Print out each company
Companies.forEach(company => {
console.log(company)
});
// Change each company name to uppercase one by one and print them out
Companies.forEach(company => {
console.log(company.toUpperCase())
})
// Print the array like as a sentence: Facebook, Google, Microsoft, Apple, IBM,Oracle and Amazon are big IT companies.
let temp = Companies.pop()
// console.log("poped Companies",Companies)
console.log(Companies.join(), "and", temp, "are big IT companies.")
Companies.push(temp)
// console.log("rejoin",Companies)
// Check if a certain company exists in the itCompanies array. If it exist return the company else return a company is not found
function isCompany () {
const company = document.getElementById("iscompany").value
if (Companies.includes(company)) {
document.getElementById("companyresult").innerText = company
} else {
document.getElementById("companyresult").innerText = (company + ' is not found')
}
}
// Filter out companies which have more than one 'o' without the filter method
function myfilter (array) {
const result = Array()
array.forEach(string => {
let o = 0;
let temp = string.split('')
temp.forEach(char => {
if (char === 'o') {
o++
}
})
if (o <= 1) {
result.push(string)
}
})
return result
}
const new_company = myfilter(Companies)
console.log(new_company)
// Sort the array using sort() method
Companies.sort()
console.log(Companies)
// Reverse the array using reverse() method
Companies.reverse()
console.log(Companies)
// Slice out the first 3 companies from the array
const slice1 = Companies.slice(3)
console.log(slice1)
// Slice out the last 3 companies from the array
const slice2 = Companies.slice(0, companyLength - 3)
console.log(slice2)
// Slice out the middle IT company or companies from the array
const slice3 = Companies.slice(0, 3).concat(Companies.slice(4))
console.log(slice3)
// Remove the first IT company from the array
const notOracle = Companies.splice(0, 1)
console.log(notOracle)
console.log(Companies)
Companies.push(notOracle.toString())
Companies.sort()
// Remove the middle IT company or companies from the array
Companies.splice(3, 1)
console.log(Companies)
// Remove the last IT company from the array
Companies.pop();
console.log(Companies)
console.log("after pop last item")
// Remove all IT companies
const afterSplice = Companies.splice()
console.log(afterSplice)

@ -0,0 +1,17 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="arr_level1.js"></script>
</head>
<body>
<h1>Exercises</h1>
<h2>arr_level1</h2>
<input id="iscompany" value="some company">
<button id='companybutton' onclick='isCompany()'>눌러</button>
<div id='companyresult'></div>
</body>
</html>

@ -1,4 +1,7 @@
자바스크립트를 적용하는 방법들 # 간단한 자바스크립트 내용
#### *자바스크립트를 적용하는 방법들*
1. Inline script 1. Inline script
2. Internal script 2. Internal script
3. External script 3. External script
@ -14,5 +17,44 @@ External의 경우
- JS파일을 따로 만들어서 넣는다 - JS파일을 따로 만들어서 넣는다
- <script src="자바스크립트 파일 경로"></script> - <script src="자바스크립트 파일 경로"></script>
--- ## 1. 변수
const 와 let (~~var은 죽었어! 더이상 없어!~~)
const는 한번 선언하면 값이 안바뀜(constant == 상수)
```js
let firstDay = "첫째 날"
firstDay = "둘째 날"
const first_day = "첫째 날" (위에서 firstDay가있어 중복 변수 안됨)
first_day = "둘째 날"
> Uncaught TypeError: Assignment to constant variable. (값 변경 불가능)
```
## 2. Data types
## 3. Arrays
```js
const arr1 = Array() // 보통 const로 배열을 선언한다고 한다
let arr2 = new Array()
const arr3 = []
arr1
> []
arr2
> []
```
```js
const perfectArr2 = [
'승민',
25,
true,
{country: 'Korea', city: 'Seoul'},
{classes: ['a','b','c','d']}
]
// 여러 데이터 타입도 넣을 수 있음
```

Loading…
Cancel
Save