๐ŸŒŒ Day 2 Algorithm Review

JBยท2022๋…„ 3์›” 15์ผ
0

Algorithms

๋ชฉ๋ก ๋ณด๊ธฐ
3/12

๐ŸŒš Replit

.fill()

function StringCheck(string){
  const arr = string.split('')
  const result =  arr.fill("*", 1,5)
  return result.join('')
}
StringCheck("0123456")

// result: '0****56'
  • fill method is only available in array.
  • If I want to replace the string * from "1" to "4 into "asdf", I should write ("asdf", 1,5)

.startsWith()

function cb(element) {
    // return (element[0] === '์กฐ')
  return element.startsWith("์กฐ")
  //์ด๊ฑธ ์ฝ˜์†”๋กœ ์ฐ์–ด๋ณด๋ฉด false, false, true, false ๊ฐ€ ๋œธ
}

const result = arr.findIndex(cb); //๊ทธ๋ ‡๊ฒŒ ๋œฌ boolean ๊ฐ’์˜ index num์„ ์ฐพ์•„๋‚ด๋Š”๊ฒŒ ๋ชฉํ‘œ

console.log(result); // 2

.filter()

function solution(front, back) {
  return front.filter( el => back.includes(el))
                    // back์—๋„ ํฌํ•จ๋˜์–ด์žˆ๋Š” element๋งŒ ๊ณจ๋ผ์„œ ์ถœ๋ ฅ
}
  • filter method is used in arrays only.
  • format: (element => element.legnth < 5)

๐ŸŒ Class

๐ŸŒœ [๋ฐฐ์—ด, Array]

: ์ˆซ์ž, ๋ฌธ์ž์—ด, ๋ฐฐ์—ด, ๊ฐ์ฒด ๋“ฑ ๋ชจ๋“  ๋ฐ์ดํ„ฐ ์ˆ˜์šฉ ๊ฐ€๋Šฅ

๐Ÿƒ ๋ฐฐ์—ด์˜ ์„ ์–ธ๊ณผ ํ• ๋‹น

const fruits = [];

fruits.push('์‚ฌ๊ณผ', "๋ฐ”๋‚˜๋‚˜",'ํŒŒ์ธ์• ํ”Œ')
// push๋Š” push ๋œ ์ดํ›„์˜ ๊ธธ์ด๊ฐ’์„ ๋ฆฌํ„ดํ•จ ๊ทธ๋ž˜์„œ ๋งŒ์•ฝ์—
// const newFruits = fruits.push('์‚ฌ๊ณผ', "๋ฐ”๋‚˜๋‚˜",'ํŒŒ์ธ์• ํ”Œ')
// ์œผ๋กœ ํ•œ๋‹ค๋ฉด push๋œ ๊ฒฐ๊ณผ๋Š” 3์ด ๋‚˜์˜ค๊ฒŒ ๋˜๋Š”๊ฑฐ์ž„
// push๋Š” ์›๋ณธ๊ฐ’ ์ž์ฒด๋ฅผ ์ˆ˜์ •ํ•˜๊ธฐ ๋–„๋ฌธ์— ๋”ฐ๋กœ ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•ด์„œ ๋‹ด์•„์ฃผ์ง€ ์•Š์•„๋„ ๋จ
fruits

// =========== All at once ===========
const secondFruits = ['์‚ฌ๊ณผ','๋ฐ”๋‚˜๋‚˜','ํŒŒ์ธ์• ํ”Œ']
console.log(secondFruits)

๐Ÿƒ ๋ฐฐ์—ด์˜ ๊ธฐ๋Šฅ

// ๋ฐฐ์—ด์˜ ๋งˆ์ง€๋ง‰ ๋ฐ์ดํƒ€ ๊ฐ€์ ธ์™€์„œ ์ƒˆ ๋ฐฐ์—ด์— ๋‹ด์•„์ฃผ๊ธฐ
const fruits = ['์‚ฌ๊ณผ','๋ฐ”๋‚˜๋‚˜','ํŒŒ์ธ์• ํ”Œ']

fruits[0]	//'์‚ฌ๊ณผ'
fruits[1]	//'๋ฐ”๋‚˜๋‚˜'
fruits[2]	//'ํŒŒ์ธ์• ํ”Œ'

const newFruits = fruits[fruits.length-1]
newFruits

๐ŸŒ› {"๊ฐ์ฒด": "Object"}

: ๋ฐ์ดํ„ฐ๋“ค์„ ์ข…๋ฅ˜์— ๋งž๊ฒŒ ๋ถ„๋ฅ˜ํ•˜์—ฌ ์ €์žฅ

  • key์™€ value๊ฐ’์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•จ
  • ๊ฐ์ฒด์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒํ•  ๋•Œ๋Š” ๊ฐ์ฒด์˜ key ๊ฐ’์„ ์ด์šฉํ•ด ์กฐํšŒํ•  ์ˆ˜ ์žˆ์Œ
  • ๊ฐ์ฒด ์•ˆ์— ๊ฐ์ฒด๋„ ๋“ค์–ด์˜ฌ ์ˆ˜ ์žˆ๊ณ  ๋ฐฐ์—ด๋„ ๋“ค์–ด์˜ฌ ์ˆ˜ ์žˆ์Œ
const Obj = {
  "name":"JB",
  "age":18,
  "hobby": {
    "ball":"volley ball",
    "water":"swim"
  }
}

Obj.name  // result: 'JB'		//key๊ฐ’์˜ ์ด๋ฆ„์„ ์ •ํ™•ํžˆ ์ž…๋ ฅํ•ด์•ผํ•จ
Obj["name"]  // result: 'JB'	

Obj.age // result: 18
Obj["age"] //result: 18

.key vs. ["key"]

let a = "age"

Obj[a] // result: 18  			//key๊ฐ’์˜ ์ด๋ฆ„์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๋ณ€์ˆ˜๋ฅผ ํ• ๋‹นํ•  ์ˆ˜ ์žˆ์Œ

๊ฐ์ฒด ๋ฐ์ดํ„ฐ ์ถ”๊ฐ€/๋ณ€๊ฒฝ

Obj.dog = "์ƒ์ด"

// result: 
	const Obj = {
  		"name":"JB",
  		"age":18,
  		"hobby": {
    		"ball":"volley ball",
    		"water":"swim"
  		}
      "dog":"์ƒ์ด"	// ์กด์žฌํ•˜๋Š” key๊ฐ’์ด ์—†๋‹ค๋ฉด ์ƒˆ๋กœ ์ถ”๊ฐ€ํ•˜๊ณ  ๋งŒ์•ฝ ์ด๋ฏธ ์กด์žฌํ•˜๋Š” key๊ฐ’์ด ์žˆ๋‹ค๋ฉด ๋ณ€๊ฒฝ์ด ๋˜๋Š”๊ฑฐ์ž„
	}

์—ฌ๊ธฐ์„œ ๋งŒ์•ฝ Obj.dog = "๊ฐ•์ฅ"๋ฅผ ํ•œ๋‹ค๋ฉด

// result: 
	const Obj = {
  		"name":"JB",
  		"age":18,
  		"hobby": {
    		"ball":"volley ball",
    		"water":"swim"
  		}
      "dog":"๊ฐ•์ฅ"
	}

๊ฐ์ฒด ๋ฐ์ดํ„ฐ ์‚ญ์ œ

delete Obj.dog

// result:
	const Obj = {
  		"name":"JB",
  		"age":18,
  		"hobby": {
    		"ball":"volley ball",
    		"water":"Swim"
  		}
	}

๊ฐ์ฒด ์† ๊ฐ์ฒด ๋ฐ์ดํ„ฐ ์ถ”๊ฐ€-๋ณ€๊ฒฝ/์‚ญ์ œ

Obj.hobby.art = "architecture"
delete Obj.hobby.water

//result:
	const Obj = {
  		"name":"JB",
  		"age":18,
  		"hobby": {
    		"ball":"volley ball",
          	"art":"architecture"
  		}
	}

๐Ÿƒ ๊ฐ์ฒด์˜ ์„ ์–ธ๊ณผ ํ• ๋‹น

const student = {}
student.name = "JB"
student
// resut: { name: 'JB' }

๐Ÿƒ ๊ฐ์ฒด์˜ ํ‚ค์™€ ๊ฐ’ ์ถ”๊ฐ€

const student = {
  name:"aba",
  age: 8
}

const school = {
  name: "aaa",
  teacher: "aa"
}


student["school"] = school //ํ• ๋‹น
	//or student.school = school
console.log(student)	//์ฐ์–ด๋‚ด๊ธฐ

// result: 
// {
//  name: 'aba',
//  age: 8,
//  school: { name: 'aaa', teacher: 'aa' }
// }
profile
๋‘๋น„๋‘๋ฐฅ๋ฐฅ

0๊ฐœ์˜ ๋Œ“๊ธ€