写的烂,不要见怪。。
JavaScript代码
- <script>
- function QRank(scores){
- this._scores=this._rank = 0;
- this._max = 10000;
- this.setScore(scores);
- }
- QRank.prototype.setScore = function(scores){
- this._scores = parseInt(scores);
- }
- QRank.prototype.getScore = function(){
- return this._scores;
- }
- QRank.prototype.setRank = function(){
- //this._rank = Math.floor(Math.sqrt(this._scores+4));
- var score = this.getScore();
- var j=0;
- for(var i=0;i<this._max;i++){
- if(this.getScore() < j){
- this._rank = (i-1);
- break;
- }
- j+=(5+i*2);
- }
- }
- QRank.prototype.getRank = function(){
- return this._rank;
- }
- QRank.prototype.getStar = function(){
- var rank;
- if((rank = this.getRank()) == 0){
- this.setRank();
- rank = this.getRank();
- }
- var rank4 = rank.toString(4).split("").reverse(); //convert
- var icon = '';
- var iconarr = ['☆','★','○','●','$']; //对应,星星,月亮,太阳,皇冠,美元对应未知。
- for (var i in rank4){
- icon = iconarr[i].repeat(rank4[i]) + icon;
- }
- document.write(icon);
- }
- String.prototype.repeat = function(num){
- str = [];
- for (var i=0;i <num ; i++){
- str.push(this.toString().split(""));
- }
- return str.join("");
- }
- var q = new QRank(1000);
- q.getStar();
- alert(q.getRank());
- </script>