split & join

๋ฐฐ์—ด์„ ๋ฌธ์ž๋กœ ์ชผ๊ฐค ๋•Œ๋Š” split์„ ์‚ฌ์šฉํ•˜๊ณ , ๋ฌธ์ž๋ฅผ ๋ฐฐ์—ด๋กœ ํ•ฉ์น  ๊ฒฝ์šฐ์—๋Š” join์„ ์‚ฌ์šฉํ•œ๋‹ค.

var ์ˆซ์ž๋ฐฐ์—ด = ์ž…๋ ฅ์ฐฝ.value;//2879
String(์ˆซ์ž๋ฐฐ์—ด[0])+String(์ˆซ์ž๋ฐฐ์—ด[1])+String(์ˆซ์ž๋ฐฐ์—ด[2])+String(์ˆซ์ž๋ฐฐ์—ด[3]);//String์„ ๋ถ™์ด์ง€ ์•Š์œผ๋ฉด ์ˆซ์ž ๋ง์…ˆ์‹์ด ๋˜๊ธฐ ๋•Œ๋ฌธ์— String์„ ๋ถ™์—ฌ์ค€๋‹ค.
์ˆซ์ž๋ฐฐ์—ด.join('');;//์œ„์˜ ์ค„๊ณผ ๊ฐ™์€ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜จ๋‹ค.

์ˆซ์ž์•ผ๊ตฌ๋ฅผ ๊ตฌํ˜„ํ•  ๋•Œ, ๋Œ€๋‹ต๊ณผ ์ •๋‹ต์˜ ์›์†Œ๋ฅผ ๊ฐ๊ฐ ๋น„๊ตํ•˜์—ฌ ํ™ˆ๋Ÿฐ์ธ์ง€ ํ˜น์€ ์ŠคํŠธ๋ผ์ดํฌ์™€ ๋ถˆ์„ ์•Œ๋ ค์ค˜์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— split์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ๊ฐ์˜ ์›์†Œ๋ฅผ ๋”ฐ์ง„๋‹ค.

var body = document.body;//document.body๋ฅผ ํ†ตํ•ด์„œ ํ™”๋ฉด(document)์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค

var ์ˆซ์žํ›„๋ณด = [1,2,3,4,5,6,7,8,9];
var ์ˆซ์ž๋ฐฐ์—ด = [];//์ˆซ์ž์•ผ๊ตฌ ์ •๋‹ต!

for (var i =0; i <4 ;i += 1) {
    //var ๋ฝ‘์€๊ฒƒ = ์ˆซ์žํ›„๋ณด.pop();๋’ค์—์„œ๋ถ€ํ„ฐ ๋ฝ‘๋Š”๋‹ค.
    //var ๋ฝ‘์€๊ฒƒ = ์ˆซ์žํ›„๋ณด.splice(5,2);//5๋ฒˆ์งธ๋ถ€ํ„ฐ 2๊ฐœ 6,7์„ ๋ฝ‘๋Š”๋‹ค.
    var ๋ฝ‘์€๊ฒƒ = ์ˆซ์žํ›„๋ณด.splice(Math.floor(Math.random() * (9-i)),1)[0];//๋žœ๋คํ•œ index๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ๊ทธ ๋’ค 2๊ฐœ 6,7์„ ๋ฝ‘๋Š”๋‹ค, ํ•œ๋ฒˆ ์ˆซ์ž๋ฅผ ๋ฝ‘์œผ๋ฉด ๋ฐฐ์—ด ๊ธธ์ด๊ฐ€ 1์”ฉ ๊ฐ์†Œํ•˜๊ธฐ ๋•Œ๋ฌธ์— (9-i)๋กœ ํ•ด์ค€๋‹ค.
    ์ˆซ์ž๋ฐฐ์—ด.unshift(๋ฝ‘์€๊ฒƒ);//์•ž์—์„œ๋ถ€ํ„ฐ ๋„ฃ๋Š”๋‹ค. 4 3 2 1 ์ˆœ์„œ๋Œ€๋กœ ๋„ฃ์Œ.
}
console.log(์ˆซ์ž๋ฐฐ์—ด);
var fform = document.createElement('form');
document.body.append(fform);

var ์ž…๋ ฅ์ฐฝ = document.createElement('input');
์ž…๋ ฅ์ฐฝ.type = 'number';//์ด๋ ‡๊ฒŒ ํ•ด์ค˜๋„ ์ž…๋ ฅ์ฐฝ.value๋Š” ์ˆซ์ž๋กœ ๋˜์ง€ ์•Š๋Š”๋‹ค.
์ž…๋ ฅ์ฐฝ.maxLength = 4;
fform.append(์ž…๋ ฅ์ฐฝ);//document>body๊ฐ€ ์•„๋‹ˆ๋ผ form ํ•˜์œ„์— ๋งŒ๋“ค์–ด์ค˜์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์—.

var btn = document.createElement('button');
btn.textContent = '์ž…๋ ฅ';//ํƒœ๊ทธ ์•ˆ์— ๋“ค์–ด๊ฐ€๋Š” ๊ธ€์ž.
fform.append(btn);

var result = document.createElement('div');
document.body.append();

var trial = 0;//ํ‹€๋ฆฐ ํšŸ์ˆ˜
fform.addEventListener('submit',function ๋น„๋™๊ธฐ(e){//์—”ํ„ฐ๋ฅผ ์ณค์„ ๋•Œ
    e.preventDefault();
    var answer = ์ž…๋ ฅ์ฐฝ.value;
    //console.log(answer,์ˆซ์ž๋ฐฐ์—ด, ๋‹ต === ์ˆซ์ž๋ฐฐ์—ด.join(''));

    if(answer === ์ˆซ์ž๋ฐฐ์—ด.join('')){//๋‹ต์ด ๋งž์œผ๋ฉด
        result.textContent = 'ํ™ˆ๋Ÿฐ';//์ž…๋ ฅํ•œ answer์™€ ์ •๋‹ต๊ณผ ์ผ์น˜ํ•˜๋ฉด 'ํ™ˆ๋Ÿฐ'
        ์ž…๋ ฅ์ฐฝ.value = '';
        ์ž…๋ ฅ์ฐฝ.focus();
        //๋‹ค์Œ ๋ฌธ์ œ ๋‚ธ๋‹ค
        ์ˆซ์žํ›„๋ณด = [1,2,3,4,5,6,7,8,9];
        ์ˆซ์ž๋ฐฐ์—ด = [];
        for(var i = 0;i<4;i += 1) {
            var ๋ฝ‘์€๊ฒƒ = splice(Math.floor(Math.random * (9-i)),1)[0];
            ์ˆซ์ž๋ฐฐ์—ด.push(๋ฝ‘์€๊ฒƒ);
        }
        trial = 0;
    } else {//์ŠคํŠธ๋ผ์ดํฌ์™€ ๋ณผ์„ ์•Œ๋ ค์ค˜์•ผํ•˜๊ธฐ ๋–„๋ฌธ์— split์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ๊ฐ์˜ ์›์†Œ๋ฅผ ๋”ฐ์ง„๋‹ค.
        //9546์„ ๋ฐฐ์—ด๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.
        //๋ฌธ์ž.split(๊ตฌ๋ถ„์ž)->๋ฐฐ์—ด (๋ฌธ์ž๋ฅผ ๋ฐฐ์—ด๋กœ ๋ฐ”๊ฟ€ ๋• split)
        //๋ฐฐ์—ด.join(๊ตฌ๋ถ„์ž)->๋ฌธ์ž (๋ฐฐ์—ด์„ ๋ฌธ์ž๋กœ ๋ฐ”๊ฟ€ ๋• join)
        var ๋‹ต๋ฐฐ์—ด = answer.split('');//์ž…๋ ฅํ•œ ๋Œ€๋‹ต์„ ์ชผ๊ฐœ์„œ ๋‹ต๋ฐฐ์—ด์— ์ €์žฅํ•œ๋‹ค.
        var strike = 0;
        var ball = 0;
        trial += 1;
        if(trial > 10) {//10๋ฒˆ ๋„˜๊ฒŒ ํ‹€๋ฆฐ ๊ฒฝ์šฐ
            result.textContent = '10๋ฒˆ ๋„˜๊ฒŒ ํ‹€๋ ค์„œ ์‹คํŒจ! ๋‹ต์€' + ์ˆซ์ž๋ฐฐ์—ด.join(',')+'์˜€์Šต๋‹ˆ๋‹ค';
            ์ž…๋ ฅ์ฐฝ.value = '';
            ์ž…๋ ฅ์ฐฝ.focus();
            //๋ฌธ์ œ ์ƒˆ๋กœ ๋‚ธ๋‹ค.
            ์ˆซ์žํ›„๋ณด = [1,2,3,4,5,6,7,8,9];
            ์ˆซ์ž๋ฐฐ์—ด = [];
            for(var i = 0;i<4;i += 1) {
                var ๋ฝ‘์€๊ฒƒ = splice(Math.floor(Math.random * (9-i)),1)[0];
                ์ˆซ์ž๋ฐฐ์—ด.push(๋ฝ‘์€๊ฒƒ);
            }
            trial=0;
        } else {//10๋ฒˆ ๋ฏธ๋งŒ์œผ๋กœ ํ‹€๋ฆฐ ๊ฒฝ์šฐ
            console.log('๋‹ต์ด ํ‹€๋ฆฌ๋ฉด');//์—๋Ÿฌ์›์ธ์„ track ํ•˜๊ธฐ์œ„ํ•ด
            for(var i=0; i <4; i++) {//4์ž๋ฆฌ ์ˆซ์ž์ด๊ธฐ ๋•Œ๋ฌธ์— 4๋ฒˆ ๋ฐ˜๋ณต
                if(Number(๋‹ต๋ฐฐ์—ด[i]) === ์ˆซ์ž๋ฐฐ์—ด[i]){//๊ฐ™์€ ์ž๋ฆฌ, ๊ฐ™์€ ์ˆซ์ž์ธ์ง€ ํ™•์ธ.๋‹ต๋ฐฐ์—ด(์ž…๋ ฅํ•œ ๋Œ€๋‹ต)์™€ ์ˆซ์ž๋ฐฐ์—ด(์ˆซ์ž์•ผ๊ตฌ ์ •๋‹ต)์˜ ๊ฐ๊ฐ์˜ ์›์†Œ(i๋ฒˆ์งธ ์›์†Œ)๋ฅผ ๋น„๊ตํ•œ๋‹ค.
                    console.log('๊ฐ™์€ ์ž๋ฆฌ?',๋‹ต๋ฐฐ์—ด);
                    strike += 1;
                }
                else if (์ˆซ์ž๋ฐฐ์—ด.indexOf(Number(๋‹ต๋ฐฐ์—ด[i])) > -1){//๊ฐ™์€ ์ž๋ฆฌ๋Š” ์•„๋‹ˆ์ง€๋งŒ ์ˆซ์ž๊ฐ€ ๊ฒน์น˜๋Š”์ง€ ํ™•์ธ.๋ฐฐ์—ด.indexOf(๊ฐ’) : ๊ฐ’์˜ ์œ„์น˜๋ฅผ ์•Œ ์ˆ˜ ์žˆ๋‹ค. ์—†์œผ๋ฉด -1. ๊ทธ๋Ÿฌ๋ฏ€๋กœ -1๊ณผ ๋น„๊ตํ•˜์—ฌ -1๋ณด๋‹ค ํฌ๋ฉด ๋“ค์–ด์žˆ๋‹ค.
                    console.log('๊ฒน์น˜๋Š” ์ˆซ์ž?');
                    ball += 1;
                }
            } 
        }
         
    result.textContent = strike + '์ŠคํŠธ๋ผ์ดํฌ' + ๋ณผ + '๋ณผ์ž…๋‹ˆ๋‹ค.';
    ์ž…๋ ฅ์ฐฝ.value = '';
    ์ž…๋ ฅ์ฐฝ.focus();
    }    
});

Last updated