試題

1.數組排序

function mySort(){
  for (var i = 0; i < arr.length - 1; i++){
    for (var j = i + 1; j < arr.length; j++){
      if (arr[i] > arr[j]){
      	[arr[i],arr[j]] = [arr[j],arr[i]]
      }
    }
  } 
  console.log(arr); 
}
mySort(arr)
複製代碼

2.

出現時間滯後的緣由是由於var now = Date.now()只在程序中執行了一次。 解決辦法:css

foreach device in alldevices 
{
    workQueue.push(funciton(){
        seTime(device,Date.now())
    })
}
複製代碼

3.

//html部分
	<div class="father">
		<div class="son_1">
		</div>
		<div class="son_2">
			<div class="grandson_1"></div>
			<div class="grandson_2"></div>
		</div>
	</div>
//css部分
body,html{
	height: 100%;
}
.father{
	height: 100%;
	display: flex;
	flex-direction: column;
}
.son_1{
	flex-basis: 220px;
}
.son_2{
	flex-grow: 1;
	display: flex;
	justify-content: space-between;
}
.grandson_1{
	flex-basis: 330px;
}
.grandson_2{
	flex: 1;
}
複製代碼

4.

setTimeout(function(){
   divTime.innerHTML = Date.now()
   setTimeout(arguments.callee,1000);
},1000);

複製代碼

5.

let tree = {
			data:"+",
			left:{
				data:"+",
				left:{
					data:1,
					left:{},
					centre:{},
					right:{}
				},
				right:{
					data:2,
					left:{},
					centre:{},
					right:{}
				},
				centre:{}
			},
			right:{
				data:"/",
				left:{
					data:3,
					left:{},
					right:{},
					centre:{}
				},
				right:{
					data:4,
					left:{},
					right:{},
					centre:{}
				},
				centre:{}
			},
			centre:{
				data:"-",
				left:{
					data:"*",
					left:{
						data:5,
						left:{},
						right:{},
						centre:{}
					},
					right:{
						data:6,
						left:{},
						right:{},
						centre:{}
					},
					centre:{}
				},
				right:{
					data:7,
					left:{},
					right:{},
					centre:{}
				},
				centre:{}
			}
	}

		const highestKeys =["*","/"]
		const lowestKeys = ["+","-"]
		let nodeList = []
		function computed (node){
			if(node.data){
				computed(node.left)
				nodeList.push(node.data)//[1, "+", 2, "+", 5, "*", 6, "-", 7, 3, "/", 4]
				computed(node.centre)
				computed(node.right)
			}
		}
		

//乘除
function count_first(){
		let i = nodeList.findIndex(el=>{
			return highestKeys.includes(el)
		})
		let cases = {
			"*": nodeList[i-1] * nodeList[i+1],
			"/": nodeList[i-1] / nodeList[i+1]
		}
		if(i>-1){
			let result = cases[nodeList[i]]
			nodeList.splice(i-1,3,result)
			count_first()
		}
}
//加減
function count_second(){
    let i = nodeList.findIndex(el=>{
    		return lowestKeys.includes(el)
    	})
    	let cases = {
    		"+": nodeList[i-1]+nodeList[i+1],
    		"-": nodeList[i-1]-nodeList[i+1]
    	}
    	if(i>-1){
    		let result = cases[nodeList[i]]
    		nodeList.splice(i-1,3,result)
    		count_second()
    	}

}
computed(tree)
count_first()
count_second ()
let cases = {
	"*": reducer = (acc, cur) => acc * cur,
	"/": reducer = (acc, cur) => acc / cur,
	"+": reducer = (acc, cur) => acc + cur,
	"-": reducer = (acc, cur) => acc - cur
    }
const root = tree.data
console.log(nodeList.reduce(cases[root]))//26.75
複製代碼
相關文章
相關標籤/搜索