《javascrip編程精解》第二版習題練習(未完,根據自己學習進度更新)

第二章:

1:LoopinaTriangle

編寫一個循環,調用7次console.log打印三角形

#

##

###

####

#####

######

#######

var xx="";
for(var b=1;b<=7;b++){
	xx = xx+"#"
	console.log(xx)
}


2.1:FizzBuzz

編寫一個程序,使用console.log打印出1到100的所有數字,當數字被3整除打印"Fizz",當數字被5整除且不被3整除打印"Buzz"

for(var a=1;a<=100;a++){
	if(a%3===0){
		console.log("Fizz");
	}
	else if(a%3!==0 && a%5===0){
		console.log("Buzz");
	}
	else{
		console.log(a);
	}
}

2.2:修改程序,當程序遇到同時被3和5整除的情況打印"FizzBuzz"

for(var a=1;a<=100;a++){
	if(a%3===0 && a%5===0){
		console.log("BuzzFizz");
	}
	else if(a%3!==0 && a%5===0){
		console.log("Buzz");
	}
	else if(a%3===0){
		console.log("Fizz");
	}
	else{
		console.log(a);
	}
}


3:棋盤

創建一個棋盤網格每個位置可是空格或者"#",用console.log輸出結果

棋盤定義size=8,用於擴展任意長寬棋盤

# # # # 

 # # # # 

# # # # 

 # # # # 

# # # # 

 # # # # 

# # # # 

 # # # #

var size=8;
for(var b=1;b<=size;b++){
	var x=""
	for(var c=1;c<=size;c++){
		if(b%2===0){
			x=x+" #"
		}
		else{
			x=x+"# "
		}
		if(x.length===size){
			console.log(x);
		}
	}
}

第三章:

1:最小值

實現一個和Math.min一樣功能的函數,接收兩個參數,返回最小值

function MathMin(x,y){
	if(!isNaN(x)&&!isNaN(y)){
    	return x<y?x:y;
    }
	else{
		return "Not a Number!"
	}
}
console.log(MathMin("7",4));


2:遞歸

判斷一個數的奇偶性,0是偶數,1是奇數,對於其它任何數字N,其奇偶性與N-2相同,說白了就是一個數一直減去2最後是1就是奇數,是0就是偶數,根據這些寫一個遞歸函數isEven,接收一個參數number,返回一個布爾值,使用50和75作爲測試參數;如果參數是-1會發生什麼,並且修正函數

function isEven(num){
	if(num===0){
		return true
	}
	else if(num===1){
		return false
	}
	else if(num<0){
		return false
	}
	else{
		return isEven(num-2)
	}
}
console.log(isEven(50));
console.log(isEven(75));
console.log(isEven(-1));


3:字符計數

編寫一個函數countBs,接收一個字符串參數,並返回“B”在這個字符串中出現的次數

function countBs(str){
	if(typeof str!=="string"){
		return "Not a String!"
	}
	var count=0;
	for(var a=0;a<str.length;a++){
		if("B"===str.charAt(a)){
			count +=1;
		}
	}
	return count;
}
console.log(countBs("abfbBhBhhhB"));


編寫函數countChat,接收兩個參數,第一個參數是一個字符串,第二個參數是一個字符,返回第二參數字符在第一參數字符串中出現次數

function countChar(str,char){
	if(typeof str!=="string"){
		return "Not a String!"
	}
	var count=0;
	for(var a=0;a<str.length;a++){
		if(char===str.charAt(a)){
			count +=1;
		}
	}
	return count;
}
console.log(countChar("abfbBhBhhhB","h"));


第四章:

1:特定範圍數字求和

編寫一個range函數,接收兩個參數,start和end,返回start到end之間的所有數字

function range(start,end){
	var numlist = [];
	for(start;start<=end;start+=1){
		numlist.push(start);
	}
	return numlist;
}


編寫一個sum函數接收一個數字數組,返回所有數字的和

function sum(nums){
	var count=0;
	for(var x in nums){
		count = nums[x]+count;
	}
	return count;
}



修改range函數,增加一個step參數,表示步數,如果沒指定步數默認是1,調用函數rang(1,10,2)返回[1,3,5,7,9],步數是負數就是逆序range(5,2,-1)返回[5,4,3,2]

function rangex(start,end,step){
	if(!step){
		step=1;
	}
	var numlist = [];
	if(step>=0){
		for(start;start<=end;start+=step){
			numlist.push(start);
		}
	}
	else{
		for(start;start>=end;start+=step){
			numlist.push(start);
		}
	}
	return numlist;
}


2:逆轉數組

編寫兩個函數,reverseArray和reverseArrayPlace,實現逆轉數組,不同的是,reverseArray返回一個新數組,原數組不改變;reverseArrayPlace原數組也發生改變

function reverseArray(list){
	var newList = [];
	var listLen = list.length-1;
	for(listLen;listLen>=0;listLen--){
		newList.push(list[listLen]);
	}
	return newList;
}

var xx = [1,2,3,4,5];
console.log(reverseArray(xx));
console.log(xx);
function reverseArrayPlace(list){
	var newList = list;
	var listLen = list.length-1;
	for(listLen;listLen>=0;listLen--){
		newList.push(list[listLen]);
	}
	var NewListLen = newList.length;
	var nums = (NewListLen/2);
	for(NewListLen;NewListLen>nums;NewListLen--){
		newList.shift();
	}
	return newList;
}
var xx = [1,2,3,4,5];
console.log(reverseArrayPlace(xx));
console.log(xx);
function reverseArrayPlace(array){  
    for(var i=0,length=array.length;i<length;i++){  
			//Math.ceil()向下取整;Math.round()四捨五入;Math.floor()向上取整  
			var halfIndex=length%2==0?halfIndex=length/2:Math.ceil(length/2);  
        if(i<halfIndex){ 
        	console.log(halfIndex);
         //替換位置,第一個和最後,第二和倒數第二,以此類推
          var leftvalue=array[i],  
              rightvalue=array[length-1-i];
              console.log(i);
              console.log(length-1-i);
              array[i]=rightvalue;  
              array[length-1-i]=leftvalue;  
        }  
    } 
    return array;
}
var array=[1,2,3,4,5,6,7,8,9];
console.log(reverseArrayPlace(array));
console.log(array);
















發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章