“去哪儿网”2015春招前端面试题

第一题:给定有序数组,从中找出指定数字下标,如果没找到,返回-1。(记忆中题目描述大概是这样子)

    /*num:待查找的数据*/

         Array.prototype.searchIndex = function(num){

             var result = [];

             for(var i = 0,len = this.length; i < len; i++){

                 if(this[i] == num){

                     result.push(i);

                 }

             }

             if(result.length == 0)

                 result.push(-1);

             return result;

         }



        var arr = [1,2,5,5,5,5,5,6,9];

        console.log(arr.searchIndex(5));//[2,3,4,5,6]

        console.log(arr.searchIndex(0));//[-1]    

第二题:给出一个字符串,遇到数字n,则将前方字母输出n次

样例输入:abc3dd1zz2

样例输出:abcabcabcddzzzz

//注意:数字可能是2位数以上
function print(str){ var string = ""; var _str = ""; var n = 0; for(var i = 0, len = str.length; i < len; i++){ if(str[i] >= 'a' && str[i] <= 'z'){ if(n == 0){ _str += str[i]; }else{ for(var j = 0; j < n; j++){ string += _str; } n = 0; _str = str[i]; } }else{ n = n * 10 + parseInt(str[i]); if(i == len - 1){ for(var j = 0; j < n; j++){ string += _str; } } } } return string; } console.log(print('aa3bb5zz6'));//aaaaaabbbbbbbbbbzzzzzzzzzzzz console.log(print('3a5c6e'));//aaaaacccccc console.log(print('ad13bc10'));//adadadadadadadadadadadadadbcbcbcbcbcbcbcbcbcbc console.log(print('22222'));//空字符串 console.log(print('aaaaa'));//空字符串

 

第三题:输出文件名的后缀

样例输入:file.js

样例输出:js

方案一:

        function fileSuffix(filename){

            var point = filename.lastIndexOf('.');

            return filename.substring(point + 1);

        }

            console.log(fileSuffix('file.min.js'));//js

方案二:

        function fileSuffix(filename){

            var point = filename.split('.');

            return point[point.length - 1];

        }

            console.log(fileSuffix('file.min.js'));//js

            console.log(fileSuffix('file.js'));//js

 

 

第四题:递归,大致题型如下

样例输入:

 

        var json = {

                name:'john',

                age:'80',

                child:[{                    

                    name:'sindy',

                    age:'60'

                    child:[{

                        name:'bob',

                        age:'40',

                        child:[{

                            name:'sany',

                            age:'20'

                        }]

                    }]

                },

                {

                    name:'susan',

                    age:'70'

                }]

                

        };

样例输出:

<ol>

    <li>john</li>

    <li>80</li>

    <ol>

        <li>sindy</li>

        <li>60</li>

        <ol>

            <li>bob</li>

            <li>40</li>

            <ol>

                <li>sany</li>

                <li>20</li>

            </ol>

        </ol>

    </ol>

    <ol>

        <li>susan</li>

        <li>70</li>

    </ol>

</ol>

我的答案

    function getHtml(obj){

                var ol = document.createElement('ol');

                if(obj.name){

                    var li = document.createElement('li');

                    li.innerHTML = obj.name;

                    ol.appendChild(li);

                }

                if(obj.age){

                    var li = document.createElement('li');

                    li.innerHTML = obj.age;

                    ol.appendChild(li);

                }

                if(obj.child){

                    for(var i = 0, len = obj.child.length; i < len; i++){

                        ol.appendChild( getHtml(obj.child[i]) );

                    }                   

                }    

                return ol;

            }

        document.getElementById('ol').innerHTML = '<ol>' + getHtml(json).innerHTML + '</ol>';

 

你可能感兴趣的:(面试题)