1. 程式人生 > >js簡單演算法練習(一)

js簡單演算法練習(一)

前端工程師為什麼要學習演算法?沒有這些,好像我們也可以做一些漂亮的工作。

實際上演算法是一種我們如何去做事情的方式,而演算法知識是對前人經驗的總結,以及應對未來出現問題的無數種可能。演算法不一定只有c或其他語言才能完成,那接下來的這塊演算法專題,我將以實際問題的方式去演示一些問題,當然,問題也會越來越深,開始的部分還是簡單的。實現語言javascript。

如何建立一個記錄學生成績的物件,提供一個新增成績的方法,以及一個顯示學生平均成績的方法。

function studentScore(){
	this.score=[];
	this.add=function(scroe){
		this.score.push(scroe);
		}
	this.showScore=function(){
		var num=0;
		for(var i=0;i<this.score.length;i++){
			num=num+this.score[i];
			}
		var aver=Math.floor(num/this.score.length);
		console.log(aver);
		}
	}
var score= new studentScore();
score.add(55);
score.add(58);
score.add(15);
score.add(25);
score.showScore();