一道logicmonitor的前端js面试题

愤怒的晃晃 发布于 2017/04/14 12:32
阅读 592
收藏 0

领取 1000元人民币SageMaker大礼包!>>>

前几天面试logicmonitor的一道js面试题。要求对以下房间信息进行排序:

var rooms = [
    "floor12room3",
    "floor1room12",
    "floor1room8", 
    "floor3room21", 
    "floor3room7", 
    "floor12room31",
    "floor1room24", 
    "floor2room13", 
    "floor3room14"
];

要求得到结果:

var rooms = [
    "floor1room8", 
    "floor1room12",
    "floor1room24", 
    "floor2room13", 
    "floor3room7",     
    "floor3room14",
    "floor3room21", 
    "floor12room3",
    "floor12room31"
];

当时脑壳晕晕的,只说了大概的思路。然而最终还是被刷了。各位路过的前端大佬能一试身手么?

加载中
0
北极熊js
北极熊js
var rooms = [
    "floor12room3",
    "floor1room12",
    "floor1room8", 
    "floor3room21", 
    "floor3room7", 
    "floor12room31",
    "floor1room24", 
    "floor2room13", 
    "floor3room14"
];
let roomObjs = []
for(var i=0;i<rooms.length;i++){
	var temp = rooms[i].match(/floor(\d*)room(\d*)/)
	var objs = {
		"floor":temp[1],
		"room":temp[2],
		"all":rooms[i]
	}
	roomObjs.push(objs)
}
roomObjs.sort(function(a,b){
	if(a.floor - b.floor == 0){
		return a.room-b.room
	}
	return a.floor - b.floor
})
var newRooms = []
for(var i=0;i<roomObjs.length;i++){

	newRooms.push(roomObjs[i].all)
}
console.log(newRooms)

也可以直接在sort函数内直接提取并比较

0
愤怒的晃晃
愤怒的晃晃

引用来自“北极熊js”的评论

var rooms = [
    "floor12room3",
    "floor1room12",
    "floor1room8", 
    "floor3room21", 
    "floor3room7", 
    "floor12room31",
    "floor1room24", 
    "floor2room13", 
    "floor3room14"
];
let roomObjs = []
for(var i=0;i<rooms.length;i++){
	var temp = rooms[i].match(/floor(\d*)room(\d*)/)
	var objs = {
		"floor":temp[1],
		"room":temp[2],
		"all":rooms[i]
	}
	roomObjs.push(objs)
}
roomObjs.sort(function(a,b){
	if(a.floor - b.floor == 0){
		return a.room-b.room
	}
	return a.floor - b.floor
})
var newRooms = []
for(var i=0;i<roomObjs.length;i++){

	newRooms.push(roomObjs[i].all)
}
console.log(newRooms)

也可以直接在sort函数内直接提取并比较

不错不错,很好的实现方式哈,学习了!

0
学无止境气有浩然
学无止境气有浩然
 
         var rooms  = [
            "floor12room3",
            "floor1room12",
            "floor1room8",
            "floor3room21",
            "floor3room7",
            "floor12room31",
            "floor1room24",
            "floor2room13",
            "floor3room14"
        ];
 
         const realArr  = [];
        rooms.forEach( element  => {
 
             let tempArr  = element.split("floor");
 
             let tempStr  = tempArr[ 1];
             let dataArr  = tempStr.split("room");
 
             let tempObj  = {
                floor: parseInt(dataArr[ 0]),
                room: parseInt(dataArr[ 1])
            }
            realArr.push(tempObj);
        });
 
         for ( let i  =  0; i  < realArr.length; i ++) {
             for ( let j  =  0; j  <= i; j ++) {
                 let temp  = {};
                 if (realArr[i].floor  > realArr[j].floor) {
                   
                    temp  = realArr[i];
                    realArr[i]  = realArr[j];
                    realArr[j]  = temp;
                }  else  if (realArr[i].floor  == realArr[j].floor) {
                     if (realArr[i].room  >= realArr[j].room) {
                        
                        temp  = realArr[i];
                        realArr[i]  = realArr[j];
                        realArr[j]  = temp;
                    }
                }
            }
        }
         for ( let i  =  0; i  < realArr.length; i ++) {
             let tempStr1  = `floor${realArr[i].floor}room${realArr[i].room}`;
            rooms[i]  = tempStr1; 
        }
        console.log(rooms);
0
此去经年henry
此去经年henry

let obj = {}

rooms.forEach(v => {

  let lastIndex = v.lastIndexOf('r')

  let str = v.substring(0,lastIndex)

  if(obj.hasOwnProperty(str)){

    obj[str].push(v)

  }else{

    obj[str] =[v]

  }

})

for(let key in obj){

  for(let i =0;i<obj[key].length;i++ ){

    for(let j=i;j<obj[key].length;j++){

      let temp = obj[key][i]

      let left =  parseInt(obj[key][i].substring(obj[key][i].indexOf('m')+1))

      let right = parseInt(obj[key][j].substring(obj[key][j].indexOf('m')+1))

      if(right<left){

        obj[key][i] = obj[key][j]

        obj[key][j] = temp

      } 

    }

  }

}

0
见山不是山
见山不是山

var rooms = [
        "floor12room3",
        "floor1room12",
        "floor1room8", 
        "floor3room21", 
        "floor3room7", 
        "floor12room31",
        "floor1room24", 
        "floor2room13", 
        "floor3room14"
    ];
    rooms.sort(function (x,y) {
        if (parseInt(x.split("room")[ 0].split("floor")[ 1]) < parseInt(y.split("room")[ 0].split("floor")[ 1])) {
            return -1;
        }
        if (parseInt(x.split("room")[ 0].split("floor")[ 1]) > parseInt(y.split("room")[ 0].split("floor")[ 1])) {
            return 1;
        }
        return 0;
    })
    rooms.sort(function (x,y) {
        if (parseInt(x.split("room")[ 1]) < parseInt(y.split("room")[ 1])&&parseInt(x.split("room")[ 0].split("floor")[ 1])==parseInt(y.split("room")[ 0].split("floor")[ 1])) {
            return -1;
        }
        if (parseInt(x.split("room")[ 1]) > parseInt(y.split("room")[ 1])&&parseInt(x.split("room")[ 0].split("floor")[ 1])==parseInt(y.split("room")[ 0].split("floor")[ 1])) {
            return 1;
        }
        return 0;
    })
    console.log(rooms)

0
sharkchen
sharkchen

rooms.sort((a,b) => {
  let c = a.replace('floor','').split('room');
  let d = b.replace('floor','').split('room');
  return (c[ 0 ] - d[ 0 ]) || (c[ 1 ] - d[ 1 ]);
})

返回顶部
顶部