카테고리 없음

스파르타 AI-8기 TIL (9/15)

kimjunki-8 2024. 9. 15. 22:00

Today, I finished studying with variable, operator, list, and dictionary.

For variable, it's like a box with a number or any type of code inside.

Ex.

let a = 10 -> I'm putting 10 inside to 'a'

Then I don't have to write 10 every time I need but I can use variable 'a' like console.log (a)

or I can use it with operator

console.log(a+10)

**

%

/

+

whatever

as well as the list and dictionary

It will be very boring if I have to write all the variables one by one right?

let a = 1

let b = 2

let c = 3......

but I can put them all inside the same box by list code

let a_list = [1, 2, 3, 4....] -> seems '_' means putting some specific type of code

since I made variable 'a' as a list type, I can get the number I want

x_list = [add info]

console.log(a_list[1]) -> means taking the 1th item from a_list->remember in coding, it always starts from 0

Then it will get '2'

If I want to add something to a list code, 

x(name of the variable)_list.push('info to add')

Then the info I want to add will be added to the list

 

For the dictionary, it's like 

x_dict = {add info} but add info like->  {'key' : 'value}

key -> name of value

value -> info that will be shown on to screen

If I use the key, it will show the value.(not the key)

The list and dictionary work similarly, so I was not that hard to understand 

 

So I made a funny thing today

        let item1 = 'wood stick'
        let item2 = 'wooden sword'
        let item3 = 'wooden shield'
        let item4 = 'stone'
        let item5 = 'stone sword'
        let item6 = 'stone shield'
        let item7 = 'metal stick'
        let item8 = 'steel sword'
        let item9 = 'metal shield'

        let itemset1_list = [item1, item2, item3]
        let itemset2_list = [item4, item5, item6]
        let itemset3_list = [item7, item8, item9]

        console.log('Here is the boss! choose the weapon')
        console.log(itemset1_list)
        console.log(itemset2_list)
        console.log(itemset3_list)

        item = [{'wood stick' : 'Damage : 2', 'wooden sword' : 'Damage : 2', 'wooden shield' : 'shield : 1'},
        {'stone' : 'Damage : 2', 'stone sword' : 'Damage : 4', 'stone shield' : 'shield : 3'}, {'metal stick' : 'Damage : 5', 'steel sword' : 'Damage : 8', 'metal shield' : 'shield : 7'}]
       
        console.log('You have chosen the weapon: ' + item5 + ', ' + item[1]['stone sword'])

Here is the boss! choose the weapon
list.html:53 (3) ['wood stick', 'wooden sword', 'wooden shield']
list.html:54 (3) ['stone', 'stone sword', 'stone shield']
list.html:55 (3) ['metal stick', 'steel sword', 'metal shield']
list.html:60 You have chosen the weapon: stone sword, Damage : 4

hehe..