with JavaScripts Study Days 14-15

with JavaScripts Study Days 14-15

210606 ์ผ์š”์ผ 13~14์ผ์ฐจ

(์ธํ„ดํ•˜๋ฉด์„œ ํ‡ด๊ทผ ํ›„ ์กฐ๊ธˆ์”ฉ ์Šคํ„ฐ๋””ํ•˜๊ณ  ์ผ์š”์ผ์— ๋ณต์Šตํ•˜๋ฉด์„œ ์ง„๋„๋ฅผ ๋‚˜๊ฐ)

# 12~13์ผ์ฐจ ๋ณต์Šต

CH6 ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ

  • ๋ฐฐ์—ด์˜ ๋‹จ์  : ๋Œ€๋ถ€๋ถ„ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์—์„œ๋Š” ๋ฐฐ์—ด์ด ๊ฝ‰ ์ฐจ๋ฉด ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•˜๊ธฐ๊ฐ€ ์–ด๋ ต๋‹ค. ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ฑฐ๋‚˜ ์‚ญ์ œํ•  ๋•Œ๋„ ๋‚˜๋จธ์ง€ ์š”์†Œ๋ฅผ ์ด๋™ํ•ด์•ผ ํ•˜๋ฏ€๋กœ ์–ด๋ ค์›€์ด ๋”ฐ๋ฅธ๋‹ค. ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋Š” ๋ฐฐ์—ด์˜ split()์„ ์ด์šฉํ•˜๋ฉด ๊ฐ„๋‹จํ•˜๋‹ค. ํ•˜์ง€๋งŒ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๋ฐฐ์—ด์€ ๊ฐ์ฒด๋ผ์„œ C++์ด๋‚˜ ์ž๋ฐ”๋ณด๋‹ค ๋ฐฐ์—ด์˜ ํšจ์œจ์ด ๋–จ์–ด์ง„๋‹ค. ๋”ฐ๋ผ์„œ ๋ฐฐ์—ด์ด ๋„ˆ๋ฌด ๋Š๋ฆฌ๋‹ค๊ณ  ํŒ๋‹จ๋˜๋ฉด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‹ค๋งŒ ์ž„์˜์˜ ์š”์†Œ์— ์ ‘๊ทผํ•ด์•ผ ํ•  ๋•Œ๋Š” ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ๋ณด๋‹ค ๋ฐฐ์—ด์ด ๋‚ซ๋‹ค.
  • ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ ์ •์˜ : node๋ผ ๋ถˆ๋ฆฌ๋Š” ๊ฐ์ฒด๊ฐ€ ๋ชจ์—ฌ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ตฌ์„ฑํ•œ๋‹ค. ๊ฐ ๋…ธ๋“œ๋Š” ๊ฐ์ฒด ๋ ˆํผ๋Ÿฐ์Šค๋ฅผ ํ†ตํ•ด ๋ฆฌ์ŠคํŠธ์˜ ๋‹ค๋ฅธ ๋…ธ๋“œ์™€ ์—ฐ๊ฒฐ๋œ๋‹ค. ๋‹ค๋ฅธ ๋…ธ๋“œ๋ฅผ ์ฐธ์กฐํ•˜๋Š” ๋ ˆํผ๋Ÿฐ์Šค๋ฅผ Link๋ผ๊ณ  ํ•œ๋‹ค.
  • ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ์˜ ์š”์†Œ๋Š” ๋‹ค๋ฅธ ์š”์†Œ์™€์˜ ๊ด€๊ณ„๋ฅผ ํ†ตํ•ด ์›ํ•˜๋Š” ์š”์†Œ๋ฅผ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, Bread๊ฐ€ ๋‘ ๋ฒˆ์งธ ์œ„์น˜์— ์žˆ๋‹ค๋ผ๊ณ  ํ‘œํ˜„ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ "Milk" ๋‹ค์Œ์— "Bread"๊ฐ€ ์žˆ๋‹ค๋ผ๊ณ  ํ‘œํ˜„ํ•œ๋‹ค.
  • ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ์—์„œ๋Š” ์ฒซ ๋ฒˆ์งธ ๋…ธ๋“œ์—์„œ ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ๋กœ ์ด๋™ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ์ „์ฒด ์š”์†Œ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ์ƒˆ ๋…ธ๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜๋ ค๋ฉด ์‚ฝ์ž…ํ•˜๋ ค๋Š” ๋…ธ๋“œ์˜ ์ด์ „ ๋…ธ๋“œ ๋งํฌ๊ฐ€ ์ƒˆ ๋…ธ๋“œ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋„๋ก ๋ฐ”๊ฟ”์•ผ ํ•˜๊ณ  ์ƒˆ ๋…ธ๋“œ์˜ ๋งํฌ๋Š” ์ด์ „ ๋…ธ๋“œ๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋˜ ๋…ธ๋“œ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋„๋ก ์„ค์ •ํ•ด์•ผ ํ•œ๋‹ค.
  • ํ•ญ๋ชฉ์„ ์‚ญ์ œํ•  ๋•Œ๋Š” ์‚ญ์ œํ•˜๋ ค๋Š” ๋…ธ๋“œ์˜ ์ด์ „์— ์žˆ๋Š” ๋…ธ๋“œ ๋งํฌ๋ฅผ ์‚ญ์ œํ•˜๋ ค๋Š” ๋…ธ๋“œ๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ๋…ธ๋“œ๋กœ ๋ฐ”๊พผ ๋‹ค์Œ, ์‚ญ์ œํ•˜๋ ค๋Š” ๋…ธ๋“œ์˜ ๋งํฌ๋ฅผ NULL๋กœ ์„ค์ •ํ•˜๋ฉด ๋…ธ๋“œ๊ฐ€ ์‚ญ์ œ๋œ๋‹ค.
  • Node ํด๋ž˜์Šค
// # : ๋…ธ๋“œ ํด๋ž˜์Šคfunction Node(element) {
    this.element = element;
    this.next = null;
}
  • ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ ํด๋ž˜์Šค
// # : ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ ํด๋ž˜์Šคfunction LList() {
    this.head = new Node("head");
    this.find = find;
    this.insert = insert;
    this.findPrevious = findPrevious;
    this.remove = remove;
    this.display = display;
}
LList ํด๋ž˜์Šค๋Š” ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ์˜ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•œ๋‹ค. LList ํด๋ž˜์Šค๋Š” ์ƒˆ ๋…ธ๋“œ์˜ ์‚ฝ์ž…, ๊ธฐ์กด ๋…ธ๋“œ์˜ ์‚ญ์ œ, ๋ฆฌ์ŠคํŠธ์˜ ํŠน์ • ๋ฐ์ดํ„ฐ ๊ฒ€์ƒ‰ ๋“ฑ์˜ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•œ๋‹ค. ์ƒˆ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“œ๋Š” ๋ฐ ์‚ฌ์šฉํ•  ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋„ ์ œ๊ณตํ•œ๋‹ค. ์ดˆ๊ธฐ์—๋Š” head์˜ next ํ”„๋กœํผํ‹ฐ๋ฅผ null๋กœ ์„ค์ •ํ•œ๋‹ค. ๋ฆฌ์ŠคํŠธ์— ๋…ธ๋“œ๊ฐ€ ์ถ”๊ฐ€๋˜๋ฉด next ํ”„๋กœํผํ‹ฐ ๊ฐ’์„ ๋ฐ”๊พผ๋‹ค.
  • ์ƒˆ๋กœ์šด ๋…ธ๋“œ ์‚ฝ์ž…ํ•˜๊ธฐ
์ƒˆ ๋…ธ๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜๋ ค๋ฉด ๊ธฐ์กด ๋…ธ๋“œ๋ฅผ ์ฐพ์•„์•ผ ํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ์—์„œ ํŠน์ • ๋ฐ์ดํ„ฐ๋ฅผ ํฌํ•จํ•˜๋Š” ๋…ธ๋“œ๋ฅผ ๊ฒ€์ƒ‰ํ•˜๋Š” ํ—ฌํผ ํ•จ์ˆ˜ find()๋ฅผ ๊ตฌํ˜„ํ•œ๋‹ค.
// # : ์ƒˆ๋กœ์šด ๋…ธ๋“œ ์ถ”๊ฐ€ํ•˜๊ธฐ : ๊ธฐ์กด ๋…ธ๋“œ ๋’ค๋ผ๊ณ  ๊ฐ€์ •, ๊ธฐ์กด ๋…ธ๋“œ๋ฅผ ์ฐพ์•„์•ผ ํ•œ๋‹ค.(find) ์ฐพ์œผ๋ฉด ํ•ด๋‹น ๋…ธ๋“œ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.function find(item) {
    let currNode = this.head;
// ! : ์šฐ์„  ์ƒˆ ๋…ธ๋“œ๋ฅผ ๋งŒ๋“ค๊ณ  head ๋…ธ๋“œ๋กœ ์„ค์ •while (currNode.element !== item) {
// ! : ๋‹ค์Œ ๋…ธ๋“œ๋กœ ๋ฐ˜๋ณต ์ด๋™ํ•˜๋ฉด์„œ ํ˜„์žฌ ๋…ธ๋“œ์˜ element ํ”„๋กœํผํ‹ฐ๊ฐ€ ํƒ์ƒ‰ํ•˜๋ ค๋Š” ๊ฐ’๊ณผ ๊ฐ™์€ ๊ฐ’์„ ํฌํ•จํ•˜๋Š”์ง€ ํ™•์ธํ•œ๋‹ค.// ! : ๊ธฐ์กด ๋…ธ๋“œ๋ฅผ ์ฐพ์œผ๋ฉด ์ƒˆ ๋…ธ๋“œ์˜ next ํ”„๋กœํผํ‹ฐ๋ฅผ ๊ธฐ์กด ๋…ธ๋“œ์˜ next ํ”„๋กœํผํ‹ฐ์˜ ๊ฐ’์œผ๋กœ ์„ค์ •ํ•œ๋‹ค.
        currNode = currNode.next;
    }
    return currNode;
}
function insert(newElement, item) {
    let newNode = new Node(newElement);
    let current = this.find(item);
    newNode.next = current.next;
    newNode.previous = current;
    current.next = newNode;
}
  • ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ์˜ ์š”์†Œ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ•จ์ˆ˜
function display() {
    let currNode = this.head;
    while (!(currNode.next === null)) {
        console.log(currNode.next.element);
        currNode = currNode.next;
    }
}
  • ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ์—์„œ ๋…ธ๋“œ ์‚ญ์ œํ•˜๊ธฐ
๋…ธ๋“œ๋ฅผ ์‚ญ์ œํ•˜๋ ค๋ฉด ๋ฐ”๋กœ ์ด์ „ ๋…ธ๋“œ๋ฅผ ์ฐพ์•„์•ผ ํ•œ๋‹ค. ์ด์ „ ๋…ธ๋“œ๋ฅผ ์ฐพ์œผ๋ฉด ์ด์ „ ๋…ธ๋“œ์˜ next ํ”„๋กœํผํ‹ฐ๋ฅผ ์‚ญ์ œํ•˜๋ ค๋Š” ๋…ธ๋“œ์˜ ๋‹ค์Œ ๋…ธ๋“œ๋กœ ์„ค์ •ํ•ด์•ผ ํ•œ๋‹ค. findPrevious()๋Š” ๋‹ค์Œ ๋…ธ๋“œ์—์„œ ์‚ญ์ œํ•˜๋ ค๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ํฌํ•จํ•˜๊ณ  ์žˆ์œผ๋ฉด ํƒ์ƒ‰์„ ๋ฉˆ์ถ˜๋‹ค. ์‚ญ์ œํ•˜๋ ค๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ํฌํ•จํ•˜๋Š” ๋…ธ๋“œ์˜ ์ด์ „ ๋…ธ๋“œ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ๊ทธ๋ž˜์•ผ๋งŒ ์ด์ „ ๋…ธ๋“œ์˜ next ํ”„๋กœํผํ‹ฐ๋ฅผ ๊ณ ์น  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.
// # : ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ์—์„œ ๋…ธ๋“œ ์‚ญ์ œํ•˜๋ ค๋ฉด ์‚ญ์ œํ•˜๋ ค๋Š” ๋ฐ”๋กœ ์ด์ „ ๋…ธ๋“œ๋ฅผ ์ฐพ์•„์•ผ ํ•œ๋‹ค. ์ด์ „ ๋…ธ๋“œ๋ฅผ ์ฐพ์•˜์œผ๋ฉด ์ด์ „ ๋…ธ๋“œ์˜ next๋ฅผ ์‚ญ์ œํ•˜๋ ค๋Š” ๋…ธ๋“œ์˜ ๋‹ค์Œ ๋…ธ๋“œ๋กœ ์„ค์ •ํ•ด์•ผ ํ•œ๋‹ค.function findPrevious(item) {
    let currNode = this.head;
    while (!(currNode.next === null) && currNode.next.element !== item) {
        currNode = currNode.next;
    }
    return currNode;
}
function remove(item) {
    let prevNode = this.findPrevious(item);
    if (!(prevNode.next === null)) {
        prevNode.next = prevNode.next.next;
    }
}
  • ์–‘๋ฐฉํ–ฅ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ
์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๋Š” ์—ญ๋ฐฉํ–ฅ์œผ๋กœ ๋…ธ๋“œ๋ฅผ ํƒ์ƒ‰ํ•˜๋Š” ๊ฒƒ์ด ์‰ฝ์ง€ ์•Š๋‹ค. ๋…ธ๋“œ์— ์ด์ „ ๋…ธ๋“œ์˜ ๋งํฌ๋ฅผ ์ €์žฅํ•˜๋Š” ํ”„๋กœํผํ‹ฐ๋ฅผ ์ถ”๊ฐ€ํ•˜๋ฉด ์ด ๋ฌธ์ œ๋ฅผ ์‰ฝ๊ฒŒ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค. ํ•˜์ง€๋งŒ ๋งํฌ๋ฅผ ์ถ”๊ฐ€ํ•˜๋ฉด ๋…ธ๋“œ๋ฅผ ์ถ”๊ฐ€ํ•  ๋•Œ ๋” ๋งŽ์€ ์ž‘์—…์„ ํ•ด์•ผ ํ•œ๋‹ค. ๋Œ€์‹  ๋…ธ๋“œ๋ฅผ ์‚ญ์ œํ•  ๋•Œ๋Š” ๋”์ด์ƒ ์ด์ „ ๋…ธ๋“œ๋ฅผ ์ฐพ์„ ํ•„์š”๊ฐ€ ์—†์–ด ์ข€๋” ๊ฐ„๋‹จํ•ด์ง„๋‹ค.
 function Node(element) {
    this.element = element;
    this.next = null;
    this.previous = null;
}
  • insert() :
์ƒˆ ๋…ธ๋“œ์˜ previous ํ”„๋กœํผํ‹ฐ๋ฅผ ์ด์ „ ๋…ธ๋“œ๋กœ ์„ค์ •ํ•ด์•ผ ํ•œ๋‹ค.
function insert(newElement, item) {
    let newNode = new Node(newElement);
    let current = this.find(item);
    newNode.next = current.next;
    newNode.previous = current;
    current.next = newNode;
}
  • remove() : ๋จผ์ € ์‚ญ์ œํ•˜๋ ค๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ํฌํ•จํ•˜๋Š” ๋…ธ๋“œ๋ฅผ ์ฐพ๋Š”๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์‚ญ์ œํ•˜๋ ค๋Š” ๋…ธ๋“œ ์ด์ „ ๋…ธ๋“œ์˜ next ํ”„๋กœํผํ‹ฐ๋ฅผ ์‚ญ์ œํ•˜๋ ค๋Š” ๋…ธ๋“œ์˜ next ๊ฐ’์œผ๋กœ ํ•˜๊ณ , ์‚ญ์ œํ•˜๋ ค๋Š” ๋…ธ๋“œ ๋‹ค์Œ ๋…ธ๋“œ์˜ previous ํ”„๋กœํผํ‹ฐ๋ฅผ ์‚ญ์ œํ•˜๋ ค๋Š” ๋…ธ๋“œ์˜ previous ๊ฐ’์œผ๋กœ ๊ฐ๊ฐ ์„ค์ •ํ•œ๋‹ค.
function remove(item) {
    let currNode = this.find(item);
    if (!(currNode.next === null)) {
        currNode.previous.next = currNode.next;
        currNode.next.previous = currNode.previous;
        currNode.next = null;
        currNode.previous = null;
    }
}
  • findLast() : ์–‘๋ฐฉํ–ฅ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ์˜ ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ๋กœ ์ด๋™ํ•˜๋Š” ์œ ํ‹ธ๋ฆฌํ‹ฐ ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด ์—ญ์ˆœ์œผ๋กœ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ๋‹ค.
// # : ์–‘๋ฐฉํ–ฅ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ์˜ "๋งˆ์ง€๋ง‰ ๋…ธ๋“œ๋กœ ์ด๋™ํ•˜๋Š” ์œ ํ‹ธ๋ฆฌํ‹ฐ ํ•จ์ˆ˜"๋ฅผ ์ด์šฉํ•ด ์—ญ์ˆœ์œผ๋กœ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๋™์ž‘์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค.function findLast() {
    let currNode = this.head;
    while (!(currNode.next === null)) {
        currNode = currNode.next;
    }
    return currNode;
}
  • dispReverse() : ์—ญ์ˆœ์œผ๋กœ ์–‘๋ฐฉํ–ฅ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ์˜ ์š”์†Œ ์ถœ๋ ฅ
// # : ์—ญ์ˆœ์œผ๋กœ ์–‘๋ฐฉํ–ฅ ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ์˜ ์š”์†Œ ์ถœ๋ ฅfunction dispReverse() {
    let currNode = this.findLast();
    while (!(currNode.previous === null)) {
        console.log(currNode.element);
        currNode = currNode.previous;
    }
}
  • ์ˆœํ™˜ํ˜• ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ
์ˆœํ™˜ํ˜•์€ ๋‹จ๋ฐฉํ–ฅ๊ณผ ๊ฐ™์€ ์ข…๋ฅ˜์˜ ๋…ธ๋“œ๋ฅผ ํฌํ•จํ•œ๋‹ค. ์œ ์ผํ•˜๊ฒŒ ๋‹ค๋ฅธ ์ ์€ ์ˆœํ™˜ํ˜•์€ ํ—ค๋“œ์˜ next ํ”„๋กœํผํ‹ฐ๊ฐ€ ์ž์‹ ์„ ๊ฐ€๋ฆฌํ‚จ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. head.next = head;๋Š” ์ „์ฒด ๋ฆฌ์ŠคํŠธ์— ์ ์šฉ๋˜์–ด ํ•ญ์ƒ ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ์˜ next ํ”„๋กœํผํ‹ฐ๋Š” head๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.
  • ๋ณต์žกํ•œ ์–‘๋ฐฉํ–ฅ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ค์ง€ ์•Š๊ณ  ๊ฐ„๋‹จํ•˜๊ฒŒ ์—ญ๋ฐฉํ–ฅ์œผ๋กœ ํƒ์ƒ‰ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด ๊ฐ•์ ์ด๋‹ค. ๋…ธ๋“œ์˜ ๋์„ ์ง€๋‚˜ ๊ณ„์† ํƒ์ƒ‰ํ•˜๋ฉด ๊ฒฐ๊ตญ ์—ญ๋ฐฉํ–ฅ์— ์žˆ๋Š” ๋…ธ๋“œ๋กœ ์ด๋™ํ•  ์ˆ˜ ์žˆ๋‹ค.
function LList() {
    this.head = new Node("head");
    this.head.next = this.head;
    this.find = find;
    this.insert = insert;
    this.display = display;
    this.findPrevious = findPrevious;
    this.remove = remove;
}
  • display() :ย display() ํ•จ์ˆ˜๋Š” ์ˆœํ™˜ํ˜• ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ์— ์‚ฌ์šฉํ•˜๋ ค๋ฉด while ๋ฃจํ”„ ์‹คํ–‰ ์กฐ๊ฑด์„ ํ—ค๋“œ์—์„œ ๋ฉˆ์ถ”๋„๋ก ๋ฐ”๊ฟ”์•ผ ํ•œ๋‹ค.
function display() {
    let currNode = this.head;
    while (!(currNode.next === null) && !(currNode.next.element == "head")) {
        console.log(currNode.next.element);
        currNode = currNode.next;
    }
}

# 14~15์ผ์ฐจ ํ•™์Šต

CH7 ๋”•์…”๋„ˆ๋ฆฌ

๋”•์…”๋„ˆ๋ฆฌ๋Š” ์ „ํ™”๋ฒˆํ˜ธ๋ถ€์— ์ด๋ฆ„๊ณผ ์ „ํ™”๋ฒˆํ˜ธ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ๋ฐ์ดํ„ฐ๋ฅผ ํ‚ค(key)์™€ ๊ฐ’(value) ์Œ์œผ๋กœ ์ €์žฅํ•˜๋Š” ์ž๋ฃŒ๊ตฌ์กฐ๋‹ค. ๋ˆ„๊ตฐ๊ฐ€์˜ ์ „ํ™”๋ฒˆํ˜ธ๋ฅผ ๊ฒ€์ƒ‰ํ•˜๋ ค๋ฉด ๋จผ์ € ๊ทธ ์‚ฌ๋žŒ์˜ ์ด๋ฆ„์„ ๊ฒ€์ƒ‰ํ•ด์•ผ ํ•œ๋‹ค. ์ด๋ฆ„์„ ์ฐพ์œผ๋ฉด ๊ทธ ์˜†์— ํ‘œ์‹œ๋œ ์ „ํ™”๋ฒˆํ˜ธ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. ํ‚ค๋กœ ๊ฒ€์ƒ‰์„ ์ˆ˜ํ–‰ํ•˜๊ณ  ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ๋กœ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ Object ํด๋ž˜์Šค๋Š” ๋”•์…”๋„ˆ๋ฆฌ ํ˜•์‹์œผ๋กœ ๋™์ž‘ํ•˜๊ฒŒ ์„ค๊ณ„๋˜์—ˆ๋‹ค. []๋ณด๋‹ค๋Š” ()๋ฅผ ์ด์šฉํ•ด ํ‚ค๋ฅผ ์ฐธ์กฐํ•˜๋Š” ํŽธ์ด ํ›จ์”ฌ ์‰ฝ๊ณ  ๋”•์…”๋„ˆ๋ฆฌ์˜ ๋ชจ๋“  ํ•ญ๋ชฉ์„ ์ถœ๋ ฅ์‹œํ‚ค๋Š” ์œ ์šฉํ•œ ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด ๋†“์œผ๋ฉด ํŽธํ•˜๋ฏ€๋กœ Dictionary ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค๋„๋ก ํ•œ๋‹ค.

7.1 Dictionary ํด๋ž˜์Šค

๋‚ด๋ถ€์ ์œผ๋กœ Object ํด๋ž˜์Šค๊ฐ€ ์•„๋‹Œ Array ํด๋ž˜์Šค๋ฅผ ์ด์šฉํ•œ๋‹ค. ๋”•์…”๋„ˆ๋ฆฌ์˜ ํ‚ค๋ฅผ ์ •๋ ฌํ•ด์•ผ ํ•˜๋Š”๋ฐ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋Š” Object ํด๋ž˜์Šค์˜ ํ”„๋กœํผํ‹ฐ๋ฅผ ์ •๋ ฌํ•˜์ง€ ๋ชปํ•œ๋‹ค. ํ•˜์ง€๋งŒ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๋ชจ๋“  ๊ฒƒ์€ ๊ฐ์ฒด(Object)์ด๋ฏ€๋กœ ๋ฐฐ์—ด ์—ญ์‹œ ๊ฐ์ฒด์ž„์„ ๊ธฐ์–ตํ•˜์ž.
function Dictionary() {
    this.datastore = new Array();
    this.add = add;
    this.find = find;
    this.remove = remove;
    this.showAll = showAll;
}
add()๋Š” ํ‚ค์™€ ๊ฐ’์„ ๋ฐ›๋Š”๋‹ค. ํ‚ค๋Š” ๊ฐ’์„ ์ฐพ๋Š” ๋ฐ ์‚ฌ์šฉํ•˜๋Š” ์ธ๋ฑ์Šค๋‹ค.
function add(key, value) {
    this.datastore[key] = value;
}
find()๋Š” ํ‚ค๋ฅผ ์ธ์ž๋กœ ๋ฐ›์•„ ํ‚ค์™€ ๊ด€๋ จ๋œ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค
function find(key) {
    return this.datastore[key];
}
๋”•์…”๋„ˆ๋ฆฌ์˜ ํ‚ค์™€ ๊ฐ’ ์Œ์„ ์ง€์šธ ๋–„๋Š” ๋‚ด์žฅ ํ•จ์ˆ˜ delete๋ฅผ ์ด์šฉํ•œ๋‹ค. ์ด๋Š” Objectํด๋ž˜์Šค์˜ ์ผ๋ถ€๋ฉฐ ์ธ์ž๋กœ ํ‚ค ๋ ˆํผ๋Ÿฐ์Šค๋ฅผ ๋ฐ›๋Š”๋‹ค.
function remove(key) {
    delete this.datastore[key];
}
๋”•์…”๋„ˆ๋ฆฌ์˜ ๋ชจ๋“  ํ‚ค์™€ ๊ฐ’ ์Œ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋Šฅ์ด ํ•„์š”ํ•˜๋‹ค.
function showAll() {
// console.log(Object.keys(this.datastore));for (let key in this.datastore) {
        console.log(key + " -> " + this.datastore[key]);
    }
}
์ฑ…์—์„œ๋Š” for each ( var key in Object.keys(this.datastore)) {
๋กœ ์„ ์–ธํ•˜์—ฌ ์‚ฌ์šฉํ–ˆ๋Š”๋ฐ ์‹ค์ œ๋กœ Object.keys(this.datastore)๋ฅผ ์ฝ˜์†”์— ์ฐ์–ด๋ณด๋‹ˆ [ "Mike", "Cynthia" ] ๋กœ ๋˜์–ด ์žˆ์–ด ์•„๋ž˜์˜ ์ฝ˜์†” ์ถœ๋ ฅ๋ฌธ์—๋Š” ์ ํ•ฉํ•˜์ง€ ์•Š์•˜๋‹ค. key๊ฐ€ "0", "1"๋กœ ๋‚˜์˜ค๊ณ  this.datastore[key]๋„ undefined๋กœ ์ถœ๋ ฅ๋˜์—ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์›ํ•˜๋Š” ์ถœ๋ ฅ๊ฐ’์„ ์–ป๊ธฐ ์œ„ํ•ด์„œ for in ๋ฌธ์„ ์ด์šฉํ•˜์˜€๋‹ค.
for( let key in this.datastore){ ๋กœ ์„ ์–ธํ•œ ๊ฒฐ๊ณผ this.datastore์— ์กด์žฌํ•˜๋Š” ํ‚ค์™€ ๊ฐ’ ์Œ์˜ ํ‚ค ๊ฐ’๋“ค์ด ํ•˜๋‚˜์”ฉ ํŠ€์–ด๋‚˜์˜ค๊ณ  ์ด์— ํ•ด๋‹นํ•˜๋Š” ์ธ๋ฑ์Šค๋ฅผ ์ฐพ์•„ ๊ทธ ๊ฐ’์„ ์ถœ๋ ฅ์‹œ์ผœ์ฃผ์—ˆ๋‹ค.
let pbook = new Dictionary();
pbook.add("Mike", "123");
pbook.add("David", "345");
pbook.add("Cynthia", "456");
console.log("David's extension: " + pbook.find("David"));
pbook.remove("David");
pbook.showAll();
notion image

7.2 Dictionary ํด๋ž˜์Šค์˜ ๋ถ€๊ฐ€ ํ•จ์ˆ˜

๋”•์…”๋„ˆ๋ฆฌ์— ๋ช‡ ๊ฐœ์˜ ํ•ญ๋ชฉ์ด ์ €์žฅ๋˜์–ด ์žˆ๋Š”์ง€ ์•Œ๊ธฐ ์œ„ํ•œ count() ํ•จ์ˆ˜,( length ํ”„๋กœํผํ‹ฐ๋Š” ๋ฌธ์ž์—ด ํ‚ค์—์„œ๋Š” ์ œ๋Œ€๋กœ ์ž‘๋™ํ•˜์ง€ ์•Š์•„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•˜๋‹ค.) ๋˜ํ•œ ๋ชจ๋“  ํ•ญ๋ชฉ์„ ์‚ญ์ œํ•˜๋Š” clear() ํ•จ์ˆ˜๋ฅผ ์ถ”๊ฐ€ํ–ˆ๋‹ค.
function Dictionary() {
    this.add = add;
    this.datastore = new Array();
    this.find = find;
    this.remove = remove;
    this.showAll = showAll;
    this.count = count;
    this.clear = clear;
}

function add(key, value) {
    this.datastore[key] = value;
}

function find(key) {
    return this.datastore[key];
}

function remove(key) {
    delete this.datastore[key];
}

function showAll() {
// console.log(Object.keys(this.datastore));for (let key in this.datastore) {
        console.log(key + " -> " + this.datastore[key]);
    }
}
// # : Count elements in dictionary, length ํ”„๋กœํผํ‹ฐ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ์ด์œ ๋Š” ๋ฌธ์ž์—ด ํ‚ค์—์„œ๋Š” ์ œ๋Œ€๋กœ ๋™์ž‘ํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธfunction count() {
    let n = 0;
    for (let key in this.datastore) {
        ++n;
    }
    return n;
}

let nums = new Array();
nums[0] = 1;
nums[1] = 2;
console.log(nums.length);
let pbook = new Array();
pbook["David"] = 1;
pbook["Jennifer"] = 2;
console.log(pbook.length);

function clear() {
    for (let key in this.datastore) {
        delete this.datastore[key];
    }
}

let pbook = new Dictionary();
pbook.add("Raymond", "123");
pbook.add("David", "345");
pbook.add("Cynthia", "456");
console.log("Number of entries: " + pbook.count());
console.log("David's extension: " + pbook.find("David"));
pbook.showAll();
pbook.clear();
console.log("Number of entries : " + pbook.count());
notion image

7.3 Dictionary ํด๋ž˜์Šค์— ์ •๋ ฌ ๊ธฐ๋Šฅ ์ถ”๊ฐ€ํ•˜๊ธฐ

ํ‚ค๋ฅผ ์ด์šฉํ•ด ๊ฐ’์„ ์–ป๋Š” ๊ฒƒ์ด ๋”•์…”๋„ˆ๋ฆฌ์˜ ํ•ต์‹ฌ ๊ธฐ๋Šฅ์ด๋‹ค. ์ €์žฅ๋œ ํ•ญ๋ชฉ์˜ ์ˆœ์„œ๋Š” ์ค‘์š”ํ•˜์ง€ ์•Š๋‹ค. ํ•˜์ง€๋งŒ ์ •๋ ฌ๋œ ์ˆœ์„œ๋กœ ํ™•์ธํ•ด์•ผ ํ•  ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋‹ค. sort() ํ•จ์ˆ˜๊ฐ€ ์žˆ์œผ๋‚˜ ๋ฌธ์ž์—ด ํ‚ค์—์„œ๋Š” ์•ˆ๋œ๋‹ค. ๋”ฐ๋ผ์„œ ๋‹ค์‹œ ํ•œ๋ฒˆ Object.keys()๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค๋Š”๋ฐ ๋‹ค์‹œ ํ•œ๋ฒˆ ์ œ๋Œ€๋กœ ์ž‘๋™ํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ ์œ„์—์„œ key๋“ค๋งŒ ๋ฝ‘์•„์„œ ๋ฐฐ์—ด์„ ๊ตฌ์„ฑํ•œ ๋‹ค์Œ for๋ฌธ์—์„œ ์ด key๋“ค์„ ๋ฝ‘์•„ ์ถœ๋ ฅ์‹œํ‚ค๋„๋ก ๊ตฌํ˜„ํ•˜์˜€๋‹ค.
// ํ•˜์ง€๋งŒ ๋ฌธ์ž์—ด ํ‚ค๋Š” ์ด ๋ฐฉ์‹์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค. ์•„๋ฌด ๊ฒฐ๊ณผ๋„ ์ถœ๋ ฅ๋˜์ง€ ์•Š๋Š”๋‹ค. Object.keys()๋ฅผ ์ด์šฉํ•ด์•ผ ํ•œ๋‹ค.function showAll() {
    this.datastore = Object.keys(this.datastore).sort();// for๋ฌธ ์•ˆ์—์„œ ๋ฐ”๋กœ key๋ฅผ ๋นผ๋ฉด sort๊ฐ€ ์ ์šฉ๋˜์ง€ ์•Š์•„ ์œ„์—์„œ ํ• ๋‹นfor (let key in this.datastore) {
        console.log(key + " -> " + this.datastore[key]);
    }
}
์œ„ showAll() ๋ฉ”์„œ๋“œ๋ฅผ ๋‹ค์‹œ ์ถœ๋ ฅํ•ด๋ณด๋ฉด์„œ ์ž˜๋ชป ๊ตฌํ˜„ํ–ˆ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ๊ณ  key๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ๊ฒŒ ์•„๋‹ˆ๋ผ ํ•ด๋‹นํ•˜๋Š” ์ธ๋ฑ์Šค์˜ ๊ฐ’์„ ๊ฐ€์ ธ์˜ค๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ์„ ๊ฒƒ์œผ๋กœ ๋ณด์•„์„œ for of ๋ฌธ์„ ์‹œ๋„ํ•ด๋ณธ ๊ฒฐ๊ณผ key ๊ฐ’์— 0, 1, 2,,,,์™€ ๊ฐ™์€ ์ธ๋ฑ์Šค ๊ฐ’๋“ค์ด ๋“ค์–ด์˜ค๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ํ•ด๋‹นํ•˜๋Š” ์ธ๋ฑ์Šค์˜ key๊ฐ’์ด ์ œ๋Œ€๋กœ ๋“ค์–ด์˜ค๊ณ  ์›ํ•˜๋Š” ์ถœ๋ ฅ์„ ์–ป์„ ์ˆ˜ ์žˆ์—ˆ๋‹ค. Object.keys์™€ Object.values์˜ ๋ฌธ์ œ์ธ์ค„ ์•Œ์•˜๋Š”๋ฐ ์ฑ…์—์„œ ๋‚˜์˜จ for each ๋ฌธ์ด for of๋ฌธ์œผ๋กœ ๋Œ€์ฒด๋œ๋‹ค๋Š” ์‚ฌ์‹ค์„ ์•Œ๊ฒŒ ๋˜์—ˆ๊ณ  for of๋ฌธ์— ๋Œ€ํ•ด์„œ ์ž˜ ์ˆ™์ง€ํ•˜๊ณ  ์žˆ์–ด์•ผ ๊ฒ ๋‹ค.
notion image
https://yjshin.tistory.com/entry/JavaScript-%EC%9E%90%EB%B0%94%EC%8A%A4%ED%81%AC%EB%A6%BD%ED%8A%B8-for-%EB%AC%B8-for-in-%EB%AC%B8-for-of-%EB%AC%B8
notion image
https://yjshin.tistory.com/entry/JavaScript-%EC%9E%90%EB%B0%94%EC%8A%A4%ED%81%AC%EB%A6%BD%ED%8A%B8-for-%EB%AC%B8-for-in-%EB%AC%B8-for-of-%EB%AC%B8

7.4 ์—ฐ์Šต๋ฌธ์ œ

#1. ํ…์ŠคํŠธ ํŒŒ์ผ์—์„œ ์ด๋ฆ„๊ณผ ์ „ํ™”๋ฒˆํ˜ธ๋ฅผ ์ฝ์–ด Dictionary ๊ฐ์ฒด์— ์ €์žฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ๊ตฌํ˜„ํ•˜์‹œ์˜ค. ๋˜ํ•œ ํŠน์ • ์ „ํ™”๋ฒˆํ˜ธ ์ถœ๋ ฅ ๊ธฐ๋Šฅ, ๋ชจ๋“  ์ „ํ™”๋ฒˆํ˜ธ ์ถœ๋ ฅ ๊ธฐ๋Šฅ, ์ƒˆ๋กœ์šด ์ „ํ™”๋ฒˆํ˜ธ ์ถ”๊ฐ€ ๊ธฐ๋Šฅ, ๊ธฐ์กด ์ „ํ™”๋ฒˆํ˜ธ ์‚ญ์ œ ๊ธฐ๋Šฅ, ๋ชจ๋“  ์ „ํ™”๋ฒˆํ˜ธ ์‚ญ์ œ ๊ธฐ๋Šฅ์„ ์ถ”๊ฐ€ํ•˜์‹œ์˜ค.
#2. Dictionary ํด๋ž˜์Šค๋ฅผ ์ด์šฉํ•ด ์–ด๋–ค ํ…์ŠคํŠธ์—์„œ ๋‹จ์–ด๊ฐ€ ๋ช‡ ๋ฒˆ ๋‚˜์˜ค๋Š”์ง€๋ฅผ ์ €์žฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ๊ตฌํ˜„ํ•˜์‹œ์˜ค. ํ”„๋กœ๊ทธ๋žจ์€ ํ…์ŠคํŠธ์—์„œ ๊ฐ ๋‹จ์–ด๊ฐ€ ๋ช‡ ํšŒ ๋‚˜์˜ค๋Š”์ง€ ํ‘œ์‹œํ•ด์•ผ ํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด 'the brown fox jumped over the blue fox.'๋ฅผ ์ž…๋ ฅํ–ˆ์„ ๋•Œ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฒฐ๊ณผ๊ฐ€ ์ถœ๋ ฅ๋ผ์•ผ ํ•œ๋‹ค.
the : 2
brown: 1
fox : 2
jumped : 1
over : 1
blue : 1
// #2. Dictionary ํด๋ž˜์Šค๋ฅผ ์ด์šฉํ•ด ์–ด๋–ค ํ…์ŠคํŠธ์—์„œ ๋‹จ์–ด๊ฐ€ ๋ช‡ ๋ฒˆ ๋‚˜์˜ค๋Š”์ง€๋ฅผ ์ €์žฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ๊ตฌํ˜„ํ•˜์‹œ์˜ค.// ํ”„๋กœ๊ทธ๋žจ์€ ํ…์ŠคํŠธ์—์„œ ๊ฐ ๋‹จ์–ด๊ฐ€ ๋ช‡ ํšŒ ๋‚˜์˜ค๋Š”์ง€ ํ‘œ์‹œํ•ด์•ผ ํ•œ๋‹ค.// ์˜ˆ๋ฅผ ๋“ค์–ด 'the brown fox jumped over the blue fox.'๋ฅผ ์ž…๋ ฅํ–ˆ์„ ๋•Œ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฒฐ๊ณผ๊ฐ€ ์ถœ๋ ฅ๋ผ์•ผ ํ•œ๋‹ค.// the : 2// brown: 1// fox : 2// jumped : 1// over : 1// blue : 1function Dictionary() {
    this.add = add;
    this.datastore = new Array();
    this.find = find;
    this.remove = remove;
    this.showAll = showAll;
}

function add(text) {
    textArr = text.substr(0, text.length - 1).split(" ");// ๋งˆ์นจํ‘œ ์ œ๊ฑฐ ํ›„ ๋‹จ์–ด ๋‹จ์œ„๋กœ ์ž๋ฆ„

    textArr.forEach((word) => {
// ์ฒ˜์Œ์— ๋“ค์–ด์žˆ๋Š” ๊ฐ’์€ 0์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•  ๋•Œ ์ธ๋ฑ์Šค์— ์ˆซ์ž๋ฅผ ๋„ฃ์–ด์•ผ ๋˜๋ฏ€๋กœ// if๋ฌธ์œผ๋กœ ์ดˆ๊นƒ๊ฐ’ undefined์ผ ๊ฒฝ์šฐ 0์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•œ ๋‹ค์Œ ๊ฐœ์ˆ˜๋ฅผ ์ถ”๊ฐ€์‹œ์ผœ์ค€๋‹ค.if (this.datastore[word] === undefined) {
            this.datastore[word] = 0;
        }
        this.datastore[word] = this.datastore[word] + 1;//value;
    });
}

function find(key) {
    return this.datastore[key];
}

function remove(key) {
    delete this.datastore[key];
}

function showAll() {
    for (let key of Object.keys(this.datastore)) {
        console.log(key + " -> " + this.datastore[key]);
    }
}

let pbook = new Dictionary();
pbook.add("the brown fox jumped over the blue fox.");
pbook.showAll();
#3. 2๋ฒˆ ์˜ˆ์ œ์˜ ๊ฒฐ๊ณผ๋ฅผ ์ •๋ ฌํ•ด์„œ ์ถœ๋ ฅํ•˜๋„๋ก ํ”„๋กœ๊ทธ๋žจ์„ ๊ณ ์น˜์‹œ์˜ค.
// #3. 2๋ฒˆ ์˜ˆ์ œ์˜ ๊ฒฐ๊ณผ๋ฅผ ์ •๋ ฌํ•ด์„œ ์ถœ๋ ฅํ•˜๋„๋ก ํ”„๋กœ๊ทธ๋žจ์„ ๊ณ ์น˜์‹œ์˜ค.// #2. Dictionary ํด๋ž˜์Šค๋ฅผ ์ด์šฉํ•ด ์–ด๋–ค ํ…์ŠคํŠธ์—์„œ ๋‹จ์–ด๊ฐ€ ๋ช‡ ๋ฒˆ ๋‚˜์˜ค๋Š”์ง€๋ฅผ ์ €์žฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ๊ตฌํ˜„ํ•˜์‹œ์˜ค.// ํ”„๋กœ๊ทธ๋žจ์€ ํ…์ŠคํŠธ์—์„œ ๊ฐ ๋‹จ์–ด๊ฐ€ ๋ช‡ ํšŒ ๋‚˜์˜ค๋Š”์ง€ ํ‘œ์‹œํ•ด์•ผ ํ•œ๋‹ค.// ์˜ˆ๋ฅผ ๋“ค์–ด 'the brown fox jumped over the blue fox.'๋ฅผ ์ž…๋ ฅํ–ˆ์„ ๋•Œ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฒฐ๊ณผ๊ฐ€ ์ถœ๋ ฅ๋ผ์•ผ ํ•œ๋‹ค.// the : 2// brown: 1// fox : 2// jumped : 1// over : 1// blue : 1function Dictionary() {
    this.add = add;
    this.datastore = new Array();
    this.find = find;
    this.remove = remove;
    this.showAll = showAll;
}

function add(text) {
    textArr = text.substr(0, text.length - 1).split(" ");// ๋งˆ์นจํ‘œ ์ œ๊ฑฐ ํ›„ ๋‹จ์–ด ๋‹จ์œ„๋กœ ์ž๋ฆ„

    textArr.forEach((word) => {
// ์ฒ˜์Œ์— ๋“ค์–ด์žˆ๋Š” ๊ฐ’์€ 0์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•  ๋•Œ ์ธ๋ฑ์Šค์— ์ˆซ์ž๋ฅผ ๋„ฃ์–ด์•ผ ๋˜๋ฏ€๋กœ// if๋ฌธ์œผ๋กœ ์ดˆ๊นƒ๊ฐ’ undefined์ผ ๊ฒฝ์šฐ 0์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•œ ๋‹ค์Œ ๊ฐœ์ˆ˜๋ฅผ ์ถ”๊ฐ€์‹œ์ผœ์ค€๋‹ค.if (this.datastore[word] === undefined) {
            this.datastore[word] = 0;
        }
        this.datastore[word] = this.datastore[word] + 1;//value;
    });
}

function find(key) {
    return this.datastore[key];
}

function remove(key) {
    delete this.datastore[key];
}

function showAll() {
    for (let key of Object.keys(this.datastore).sort()) {
        console.log(key + " -> " + this.datastore[key]);
    }
}

let pbook = new Dictionary();
pbook.add("the brown fox jumped over the blue fox.");
pbook.showAll();