【神策数据面试】手撕题

翻转字符串

例如 sensorsdata@cn 翻转后变为nc@atadsrosnes

function reverseWords(source){
let result = ";
//TODO
return result;
}

  function reserveWords(source) {
            let result = '';
            for (let i = 0; i < source.length; i++) {
                let s = source.charAt(source.length - i - 1);
                result += s;
            }
            return result;
        }
        console.log(reserveWords('sensorsdata@cn'));

实现深度拷贝cloneDeep

function cloneDeep(source){
//TODO .
return target;
}

实现深拷贝

  function cloneDeep(source) {
            var target = Array.isArray(source);
            //进行深拷贝不能为空,并且是对象或者Object
            if (source && typeof source === 'object') {
                for (key in source) {
                    if (source.hasOwnProperty(key)) {
                        if (source[key] && typeof source[key] === 'object') {
                            target[key] = cloneDeep(source[key]);
                        } else {
                            target[key] = source[key];
                        }
                    }
                }
            }
            return target;
        }

手写快排

1、快速排序的基本思想:

快速排序所采用的思想是分治的思想。所谓分治,就是指以一个数为基准,将序列中的其他数往它两边“扔”。以从小到大排序为例,比它小的都“扔”到它的左边,比它大的都“扔”到它的右边,然后左右两边再分别重复这个操作,不停地分,直至分到每一个分区的基准数的左边或者右边都只剩一个数为止。这时排序也就完成了。

2、快速排序的三个步骤:

(1)选择基准:在待排序列中,按照某种方式挑出一个元素,作为 “基准”(pivot)

(2)分割操作:以该基准在序列中的实际位置,把序列分成两个子序列。此时,在基准左边的元素都比该基准小,在基准右边的元素都比基准大

(3)递归地对两个序列进行快速排序,直到序列为空或者只有一个元素。

  function sort(arr, left, right) {
            if (left >= right) {
                return;
            }
            var i = left;
            var j = right;
            var symbol = arr[left];
            while (i != j) {
                while (i < j && arr[j] >= symbol) {
                    j--;
                }
                while (i < j && arr[i] <= symbol) {
                    i++;
                }
                if (i > j) {
                    let temp = arr[i];
                    arr[i] = arr[j];
                    arr[j] = temp;
                }
            }
            var temp1 = arr[left];
            arr[left] = arr[i];
            arr[i] = temp1;
            sort(arr, 0, i - 1);
            sort(arr, i + 1, right);
        }

        var arr = [1, 5, 3, 9, 6, 5];
        sort(arr, 0, arr.length - 1);
        console.log(arr);

将一段嵌套数组,变成JSON数据?

<body>
    <p id="demo"></p>
    <button onclick="run()"></button>
    <p id="demo1"></p>
    <script>
        var demo = document.getElementById('demo');
        var demo2 = document.getElementById('demo1');
        var arr = [1, 2, 3, 4, 5];
        demo.innerHTML = arr;
        function run() {
            demo2.innerHTML = 'JSON Object' + JSON.stringify(Object.assign({}, arr))
        }
    </script>
</body>

嵌套数组 转json

给一个变量,输出它的类型?

   <script>
        console.log(Object.prototype.toString.call(1));
        console.log(Object.prototype.toString.call('1'));
        console.log(Object.prototype.toString.call([1]));
        console.log(Object.prototype.toString.call(function() {
            console.log('你好');
        }));
        console.log(Object.prototype.toString.call(true));
        console.log(Object.prototype.toString.call(undefined));
        console.log(Object.prototype.toString.call(null));
    </script>

三栏布局,左右两边载滚轮滚动的时候保持不变

    <div class="container">
        <div class="left"></div>
        <div class="right"></div>
        <div class="main">
            <ul>
               <li>1</li>
               ...
               <li>100</li>
            </ul>
        </div>
    </div>
 <style>
        /* 第一种 浮动+margin */
        
        .left {
            position: fixed;
            left: 0;
            float: left;
            width: 200px;
            background-color: pink;
            height: 500px;
        }
        
        .right {
            position: fixed;
            right: 0;
            float: right;
            width: 200px;
            background-color: yellow;
            height: 500px;
        }
        
        .main {
            /* width: 200px; */
            background-color: green;
            margin-left: 200px;
            margin-right: 200px;
            height: 2000px;
        }
    </style>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

纵有千堆雪与长街

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值