본문 바로가기
Javascript

16진수 -> 10진수 변환 계산기 / 랜덤값 활용한 장미 1000송이 만들기

by Antonio Bae 2023. 7. 4.

16진수 -> 10진수 변환 계산기

<!DOCTYPE html>
<html lang="ko">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=, initial-scale=1.0">
    <title>16진수 변환 계산기</title>
</head>

<body>
    <!-- 1a1b 라는 16진수 입력시 -->
    16진수
    <input id="hexValue" type="text" value="0">
    10진수변환
    <button id="convertBtn">==></button>
    <span id="resultArea">0</span>
    <script>
        const convertBtn = document.getElementById("convertBtn");
        convertBtn.onclick = function () {
            const hexValue = document.getElementById("hexValue").value;
            const resultArea = document.getElementById("resultArea");
            let result = 0;
            for (let i = 0; i < hexValue.length; i++) {
                let conDex = 0;
                conDex = hexValue[i];
                (hexValue[i] == "a"||hexValue[i] == "A") && (conDex = 10);
                (hexValue[i] == "b"||hexValue[i] == "B") && (conDex = 11);
                (hexValue[i] == "c"||hexValue[i] == "C") && (conDex = 12);
                (hexValue[i] == "d"||hexValue[i] == "D") && (conDex = 13);
                (hexValue[i] == "e"||hexValue[i] == "E") && (conDex = 14);
                (hexValue[i] == "f"||hexValue[i] == "F") && (conDex = 15);

                result += conDex * 16 ** (hexValue.length - 1 - i);
                console.log(result);
            }
            resultArea.innerHTML = result.toString();
           
        }

    </script>
</body>

</html>

장미 1000송이 만들기

다음 공식을 활용합니다.

 Math.floor(Math.random() * (최대값 - 최소값 + 1)) + 최소값

<!DOCTYPE html>
<html lang="ko">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=, initial-scale=1.0">
    <title>원숭이장미</title>
    <style>
        .monkeyseki { position: absolute;}
    </style>
</head>

<body>

    <script>

            for(let i=0; i<100; i++) {
                let monkeySize = (Math.random() *100 - 10 +1) +10;
                let locX = Math.floor(Math.random() *1900 - 10 +1) +10;
                // X 10 ~ 1900
                let locY = Math.floor(Math.random() *850 - 12 +1) +12;
                // Y 12~ 850
                    document.write('<p class="monkeyseki" style="top:'+ locY +'px; left:'+ locX+ 'px; font-size:'
                    +monkeySize+'px;">🌹</p>');
            }
        //140 ~930
        // Math.floor(Math.random() * (최대값 - 최소값 + 1)) + 최소값


    </script>
</body>

</html>


정규표현식 다시 정리

^ : 시작의 의미
$ : 문자의 끝
[ns]a[^0-9]\.xls : 숫자빼고
myArray\[[0-9]\] : 역슬래시 붙혀 이스케이프
\r\n\r\n : 줄바꿈
\d : 숫자하나
\D : 숫자를 제외한 문자하나
myArray\[\d\] == myArray\[[0-9]\]
\d : 문자혹은 숫자 하나(영소,영대,숫자)
\c : Ctrl-Z
\w : 한글자
[0-9]+ : 숫자 한자리 이상 연속된 숫자
\s : 글자가 있던지 말던지
\w+@\w+\.\w+ : 일반 이메일 주소
[\w]+@[\w]+\.\w+
. : 점은 숫자,문자,언더바 포함!!!!
+ : 1 또는 여러개 == {1,}
* : 0 또는 여러개 == {0,}
? : 0 또는 1 == {0,1}
*? : 글자가 하나 이상 있으면 된다
ex) https? : s가 있던지 없던지
https:\/\/[\w.\/]+ : http,https://www.forta.com/
[\r]?\n[\r]?\n : 엔터 두번 즉, 줄바꿈 찾을때
. : 한글자
#[0-9A-Fa-f]{6} : 색상표기할때처럼 6개 문자 찾을때!
#0FOFOFF : 매칭! 한글자 넘더라도 찾아냄
\D{1,2}[-\/]\d{1,2}[-\/]\d{2,4} :01-01-01 , 54-67-9999 매칭!
\bcap : 앞공백+cap으로 시작하는 단어
cap\b : cap+뒤공백 으로 끝나는 단어
\B-\B : 알아오기
\^ZARD$ : ^ ==> 시작  $==>끝
^==> []에서 쓰면 빼고 [^0-9]

\d: \$\d{3,}\.\d{2} : 1001:$496.80   --->숫자:$숫자3개.숫자2개