0-ECMAScript题集练习

1、日程安排

var weekday = window.prompt('请输入星期几');
var time = window.prompt('请输入上午或下午');
switch(weekday){
	case '星期一':
		if(time === '上午'){
			console.log('看书');
		}else if(time === '下午'){
			console.log('逛街');
		}
		break;
  case '星期二':
		if(time === '上午'){
			console.log('追剧');
		}else if(time === '下午'){
			console.log('跑步');
		}
		break;
  case '星期三':
		if(time === '上午'){
			console.log('打游戏');
		}else if(time === '下午'){
			console.log('钓鱼');
		}
		break;  
  case '星期三':
		if(time === '上午'){
			console.log('看电影');
		}else if(time === '下午'){
			console.log('学英语');
		}
		break;  	
  case '星期四':
		if(time === '上午'){
			console.log('学韩语');
		}else if(time === '下午'){
			console.log('听相声');
		}
		break;    
  case '星期五':
		if(time === '上午'){
			console.log('练琴');
		}else if(time === '下午'){
			console.log('打乒乓');
		}
		break; 		
  case '星期六':
		if(time === '上午'){
			console.log('逛公园');
		}else if(time === '下午'){
			console.log('唱歌');
		}
		break; 
  case '星期天':
		if(time === '上午'){
			console.log('看动漫');
		}else if(time === '下午'){
			console.log('吃火锅');
		}
		break; 
  default:
  	console.log('休息');
}

2、斐波那契数列也叫黄金分割数列 、兔子数列

//斐波那契数列
var n = parseInt(window.prompt('请输入第几位'));
if(n <= 0){
  console.log("输入错误")
}else{
  var n1 = 1,
      n2 = 1,
      n3;
  if(n <= 2){
     console.log(1);
  }else{
			for(var i = 2; i < n; i++){
					n3 = n1 + n2;
					n1=n2;
					n2=n3;
	    }
	console.log(n3)
	}    
}

3、定义一个函数,从wp接收一个饮料的名称,函数返回对应的价格

  function price(name){
          var name = window.prompt("请输入一个饮料名称");
     			switch(name){
    			 case "可乐":
    			 			console.log("3元");
    			 			break;
     			case "雪碧":
     						console.log("2元");
     						break;
     			default:
     						console.log("暂时没有对应的饮料");
    			 }
     }
     price(name);

4、 (+ - * / %)

       function computer(a,b,c){
                var a = Number(window.prompt("请输入第一个数:"));
                var b = window.prompt("请输入运算符");
                var c = Number(window.prompt("请输入第二个数:"));
                switch(b){
                    case "+":
                        return a + c;
                    case "-":
                        return a - c;
                    case "*":
                        return a * c;
                    case "/":
                        return a / c;  
                    case "%":
                         return a % c;     
                    default:
                        return "暂不支持其它运算"
                }
            }
             console.log(computer(1,'+',2));  

5、定义一个函数,从wp接收一个n,计算斐波那契数列的的第n位,不能用for循环

function fb(n){
  if(n <=0){
    return 0;
  }
  if(n <= 2){
    return 1;
  }
  return fb(n-1) + fb(n-2);
}
console.log(fb(5)); //5

6、定义一个函数,不使用for循环求n的阶乘(递归)

function fact(n){
  if(n == 1){
    return 1;
  }
  return n * fact(n-1);
}
console.log(fact(5));  //120

7、写个闭包 实现累加器

function test(){
    var num = 0;
    function add(){
       console.log(++num);
    }
    return add;
}
var add = test();
add();
add();

8、闭包的混存机制
一个班级,学生名字保存在一个数组里,
两个方法写在函数中的一个对象中,第一个方法加入班级,第二个方法离开班级
每次加入或者离开都需要打印新的学生名单

function myClass(){
 var students = [];
 var operations = {
    join: function(name){
        students.push(name);
        console.log(students);
    },
    leave: function(name){
      for(var i = 0; i < students.length; i++){
          var item = students[i];
          if(item === name){
            students.splice(i,1);
          }
     }
     console.log(students);
  }
 }
 return operations;
}

var obj = myClass();
obj.join('张三');
obj.join('李四');
obj.join('王五');
obj.leave('李四');

10、
一个班级,学生名字保存在一个数组里,
两个方法写在函数中的一个对象中,第一个方法加入班级,第二个方法离开班级
每次加入或者离开都需要打印新的学生名单
indexOf可以找到下标,这样就不用for循环了。


function myClass(){
 var students = [];
 var operations = {
    join: function(name){
        studnets.push(name);
        console.log(students);
    },
    leave: function(name){
     var idx = students.indexOf(name);
     if(idx !== -1){
       students.splice(idx,1);
     }
     console.log(students);
  }
 }
 return operations;
}

var obj = myClass();
obj.join('张三');
obj.join('李四');
obj.join('王五');
obj.leave('李四');

11、
写一个构造函数,接收数字类型的参数,参数数量不定,完成参数相加或相乘的功能
写法1

function Compute(){
   var args = arguments,
       res;
   this.plus = function(){
       res = 0;
       loop('add',res);
   }
   this.times = function(){
       res = 1;
       loop('mul',res);
   }
   function loop(method,res){
      for(var i = 0; i < args.length; i++){
        var item = args[i];
        if(method === 'add'){
          res += item;
        }else if(method === 'mul'){
          res *= item;
        }
      }
      console.log(res);
   }
}
 var compute = new Compute(2,4,6);
 compute.plus();
 compute.times();

写法2

function Compute(){
   var res = 0;
   this.plus = function(){
       loop(arguments,'add',res);
   }
   this.times = function(){
       res = 1;
       loop(arguments,'mul',res);
   }
   function loop(args,method,res){
      for(var i = 0; i < args.length; i++){
        var item = args[i];
        if(method === 'add'){
          res += item;
        }else if(method === 'mul'){
          res *= item;
        }
      }
      console.log(res);
   }
}
 var compute = new Compute();
 compute.plus(2,4,6);
 compute.times(3,5,7);

12、
写一个构造车的函数,可设置车的品牌,颜色,排量
再写一个构造消费者的函数,设置用户的名字,年龄,收入,
通过选择的方法实例化该用户喜欢的车,再设置车的属性

function Car(opt){
 this.brand = opt.brand;
 this.color = opt.color;
 this.displacement = opt.displacement;
}
function Person(opt){
  this.name = opt.name;
  this.age = opt.age;
  this.income = opt.income;
  this.selectCar = function(){
     var myCar = new Car(opt.carOpt);
     console.log(this.name + '挑选了一辆排量为'+myCar.displacement+'的'+myCar.color+ myCar.brand);
  }
}

var jone = new Person({
   name:'约翰',
   age:29,
   income: '20K',
   carOpt:{
   		brand:'马自达',
   		color:'红色',
   		displacement:'2.0'
   }
})
jone.selectCar();

13、

ASCII码表中的所有字符都是一个字节 表1 0~127 表2 128~ 255

UNICODE码 涵盖ASCII码 从256位开始就是2个字节

var str = 'abc';
var pos = str.charCodeAt(0);
console.log(pos);   //97
var getBytes = function(str){
   var bytes = 0;
   for(var i = 0; i < str.length; i++){
       var pos = str.charCodeAt(i);
       if(pos <= 255){
           bytes++;
       }else{
           bytes +=2;
       }
   }
   return bytes;
}
console.log(getBytes('你好,世界!Hello World!'))
function getBytes(str){
  var bytes = str.length;
  for(var i = 0; i < str.length; i++){
      var pos = str.charCodeAt(i);
      if(pos > 255){
          bytes++;
      }
  }
  return bytes;
}
console.log(getBytes('你好,世界!Hello World!'));

14、写一个插件,任意传两个数字,调用插件内部方法可进行加减乘除功能。
(写一个立即执行函数,在里面写构造函数,构造函数写函数声明也可以写表达式也可以,把构造函数抛出去)
写法(1)

;(function(){
  var Compute = function(opt){
     this.x = opt.firstNum || 0;
     this.y = opt.secondNum || 0;
  }
  Compute.prototype = {
    plus:function(){
      return this.x + this.y;
    },
    minus:function(){
      return this.x - this.y;
    },
    mul:function(){
      return this.x * this.y;
    },
    div:function(){
      return this.x / this.y;
    }
  }
  window.Compute = Compute;
})();  

var compute = new Compute({
   firstNum:1,
   secondNum:2
});

var res = compute.div();
console.log(res);

写法(2)


;(function(){
  var Compute = function(){}
  Compute.prototype = {
    plus:function(a,b){
      return a + b;
    },
    minus:function(){
      return a - b;
    },
    mul:function(){
      return a * b;
    },
    div:function(){
      return a / b;
    }
  }
  window.Compute = Compute;
})();  

var compute = new Compute();

var res = compute.div(1,2);
console.log(res);

15、
call /apply重写
年龄为多少岁姓名为XX买了一辆排量为XX的什么颜色的什么牌子的车。
function Car(){}
function Person(){}


function Car(brand,color,displacement){
   this.brand = brand;
   this.color = color;
   this.displacement = displacement;
   this.info = function(){
     return '排量为'+ this.displacement + '的' + this.color + this.brand;
   }
}
function Person(opt){
   Car.apply(this,[opt.brand,opt.color,opt.displacement]);
   this.name = opt.name;
   this.age = opt.age;
   this.say = function(){
     console.log('年龄'+ this.age + '岁姓名为'+this.name +'买了一辆'+ this.info());
   }
}
var p = new Person({
  brand:'奔驰',
  color:'红色',
  displacement: '3.0',
  name:'张三',
  age:'25'
})

p.say();

16、
模块化开发
打印一个参数值(100)以内能被3或5或7整除的数
打印斐波那契数列的第n位
打印从0到一个数的累加值

  window.onload = function(){
   	   	init();
  }
  function init(){
   	  	console.log(initFb(10));
   	  	console.log(initDiv(100));
         console.log(initAdd(100));
   }

   var initFb = (function(){
   	  	function fb(n){
   	  	   if(n <= 0){
   	  		return 0;
   	  	   }

   	  	   if(n <= 2){
   	  		return 1;
   	  	    }

   	  	    return fb(n - 1) + fb(n - 2);
   	  	}
    	  return fb;
  })();


   	  var initDiv = (function(){
   	  	function div(n){
   	  		var arr = [];
   	  		for(var i = 0; i <= n; i++){
   	  			if(i % 3 === 0 || i % 5 === 0 || i % 7 === 0){
   	  				arr.push(i);
   	  			}
   	  		}
   	  		return arr;
   	  	}
   	  	return div;
   	  })();

        var initAdd = (function(){
           function add(n){
              sum = 0;
              for(var i = 0; i <=n; i++){
                  sum+=i;
              }
              return sum;
           }

           return add;
        })();

17、请用window.prompt接收用户输入的年份,判断是否是闰年(请用三目运算来做)

  var year = window.prompt('请输入年份');

    console.log(isLeapYear(year));

    function isLeapYear(year){

      // if((year % 4 === 0 && year % 100 !== 0) || year % 400 === 0){

      //    return '是闰年';
      // }else{
      //    return  '不是闰年';
      // }

      return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0
              ? '是闰年'
              : '不是闰年';
    }   

18、笔试题

function Foo(){
  getName = function(){
  	console.log(1);
  }
  return this;
}
Foo.getName = function(){
  console.log(2);
}
Foo.prototype.getName = function(){
	console.log(3);
}
var getName = function(){
  console.log(4);
}
function getName(){
  console.log(5);
}

Foo.getName(); //2
getName();     //4
Foo().getName(); //1
getName();    // 1
new Foo.getName(); //2
new Foo().getName();//3   
                    // 实际上是
                    //var foo = new Foo();
                    //foo.getName();
new new Foo().getName();//3

//.的优先级是高于new的  
//new 2;是没有任何意义的,new等于没写

//()的优先级比.优先级高,()先执行
//在执行Foo()的同时,new会跟着一起执行,这是一个规律
//Foo()函数里面并没有this.getName(),会去原型链上去找
//new 3;是没有任何意义的,new等于没写

--------------------------
Foo.getName();
Foo是一个functionfunction.length(计算形参的数量).语法访问都是对象的形式,有内置的属性,也就可以在Foo这个函数上设置属性和方法
在JavaScript中,函数就是特殊的对象

//函数表达式
var getName = function(){
  console.log(4);
}
//函数声明式
function getName(){
  console.log(5);
}

预编译GO
1、变量声明
2、函数声明
3、运行 -> 赋值

19、请按照字节数排序下列数组

var arr = ['我爱你', 'OK', 'Hello','你说WHAT','可以'];

      arr.sort(function(a,b){
         return getBytes(a) - getBytes(b);
      });

      console.log(arr);

      function getBytes(str){
         var bytes = str.length;

         for(var i = 0; i < str.length; i++){
            if(str.charCodeAt(i) > 255){
               bytes++;
            }
         }

         return bytes;
      }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值