欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

2022 年前端春季招聘 - CVTE 编程笔试题

最编程 2024-07-19 07:44:36
...

第一题:将十进制表示的rgba字符串转为十六进制表示的字符串,透明度直接用字符串表示即可。
例:

输入:rgba(125,125,125,0.4)
输出:["#ffffff", “0.4”]

<script>
        // 将十进制的rgba表示转为16进制
        let str = "rgba(125,25,255,0.4)";
        let rgba = str.match(/^rgba\((\d*),(\d*),(\d*),(.*)\)$/);
        // console.log(rgba);
        let result = '#'
        for (let i = 1; i < rgba.length - 1; i++) {
            // console.log(parseInt(rgba[i]).toString(16));
            result += parseInt(rgba[i]).toString(16);
        }
        console.log([result, rgba.slice(-1).toString()]);
    </script>

第二题:将URL中的query string转为json格式
例:

url为:http:www.jsdk.demo.html?x=2&y=3&y=4&y=5&z=0
输出:{x:2, y:[3,4,5], z:0}

<script>
        // 将URL中的query string转为json格式   例如[x:1,y:[3,4,5],z:0]
        // let url = "http:www.jsdk.demo.html?x=2&y=3&y=4&y=5&z=0";
        let query = window.location.search.substring(1).split('&');
        let json = {};
        for (let i = 0; i < query.length; i++) {
            let temp = query[i].split('=');
            if (json.hasOwnProperty(temp[0])) {
                console.log(typeof (json[temp[0]]));
                if (typeof (json[temp[0]]) === 'number') {
                    json[temp[0]] = [json[temp[0]], parseInt(temp[1])];
                    console.log(json[temp[0]]);
                }
                else {
                    console.log(json[temp[0]]);
                    json[temp[0]].push(parseInt(temp[1]));
                }
            }
            else {
                json[temp[0]] = parseInt(temp[1]);
            }
        }
        console.log(json);

    </script>