קורס Front End למתכנתים שיעור תרגול תחביר שפת JavaScript


זהו נושא דיון מלווה לערך המקורי שב־https://www.tocode.co.il/bundles/35/lessons/js-lab

זה הפתרון שלי לתרגיל 4 - מימוש ROT13:
מימשתי שתי אופציות (הראשונה בcomment) , הרעיון בהתחלה היה להגביל את המשתמש להכניס רק מחרוזת שניתן לקודד , לאחר מכן שיניתי את ההגדרה והפונקציה רצה על התווים שצריכים לקודד…

אשמח לקבל פידבק או רעיונות נוספים לייעול… תודה @ynonp על התרגילים המהנים

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

<head>
    <meta charset="UTF-8">
</head>

<body>
    <!-- <script>
        // VERSION 1 - accept only range of string
        const FIRST_DIGIT = "a"
        const LAST_DIGIT = "z"
        const first_digit_value = FIRST_DIGIT.charCodeAt()
        const last_digit_value = LAST_DIGIT.charCodeAt()
        let userStr = acceptOnlyString()
        alert(`${userStr} rot13 => ${rot13(userStr)}`)
        function rot13(str=""){
            let temp = str
            let result = ""
            while(temp){
                const digit = temp.substring(0,1)
                let encodedDigitValue = (digit.charCodeAt()+13)
                if(encodedDigitValue > last_digit_value){
                    encodedDigitValue = first_digit_value + encodedDigitValue - last_digit_value - 1//add to first digit
                }
                const encodedDigit = String.fromCharCode(encodedDigitValue)
                console.log(`${digit} => ${encodedDigit}`)
                result += encodedDigit
                temp = temp.substring(1)
            }
            return result
        }
        function acceptOnlyString() {
            let user_select = ""
            const pattern = `^[${FIRST_DIGIT}-${LAST_DIGIT}]+$`
            while (!user_select || ! RegExp(pattern).test(user_select)) {
                user_select = prompt(`insert a string ${pattern} to decode`)
            }
            return user_select
        }
    </script> -->
    <script>
        // VERSION 2 - accept all string => modify only range
        const FIRST_DIGIT = "a"
        const LAST_DIGIT = "z"
        const first_digit_value = FIRST_DIGIT.charCodeAt()
        const last_digit_value = LAST_DIGIT.charCodeAt()
        let userStr = acceptAnyString()
        alert(`${userStr} rot13 => ${rot13(userStr)}`)
        function rot13(str = "") {
            let temp = str
            let result = ""
            while (temp) {
                const digit = temp.substring(0, 1)
                let encodedDigit = encodedDigit(digit,first_digit_value,last_digit_value)
                console.log(`${digit} => ${encodedDigit}`)
                result += encodedDigit
                temp = temp.substring(1)
            }
            return result
        }
        function encodedDigit(digit = "a", rangeStart, rangeEnd) {
            const digitValue = digit.charCodeAt()
            if(digitValue > rangeEnd || digitValue < rangeStart) return digit

            let encodedDigit = digit
            let encodedDigitValue = (digitValue + 13)
            if (encodedDigitValue > last_digit_value) {
                encodedDigitValue = first_digit_value + encodedDigitValue - last_digit_value - 1//add to first digit
            }
            encodedDigit = String.fromCharCode(encodedDigitValue)
            return encodedDigit
        }
        function acceptAnyString() {
            let user_select = ""
            while (!user_select) {
                user_select = prompt(`insert a string to decode`)
            }
            return user_select
        }
    </script>
</body>

</html>