这种方式比较简单,把编号组装为一个数组,设置一个浮标k,123的循环。每到3时将指向的值移除即可。
条件语句
- if语句
if(true){
console.log('我被执行了')
}else{
console.log('我永远不会被执行')
}
- switch语句
var f = 'apple';
if(f ==='banana'){
console.log('banana')
}else if(f==='apple'){
console.log('apple')
}
//多个if(){}else if{}嵌套时使用switch语句
switch(f){
case 'banana' : console.log('banana');
break;
case 'apple':console.log('apple');
break;
default: console.log('default');
}
- 三元运算符
- expression ? do(true): do(false);
let a = false;
let b = a ? 1:2;
console.log(b) // 2;
}```
- while循环语句
-
{
let i = 0;
while(i<10){
console.log(i); //0~9
i ;
}
}
{
let i = 11;
do{
console.log(i);
i--;
}while(i<10);
}
- for循环语句
for(let i=0;i<100;i ){
console.log(i);//0~99
}
- break和continue关键字
{
for(let i=0;i<10;i ){
if(i>=5){break;}
console.log(i); //0~4
}
}
{
for(let i=0; i<10;i ){
if(i<=5){continue;}
console.log(i);// 6~9
}
}
##### 数据类型
- 数值(number)
- 字符串(string)
- 布尔值(boolean)
- 5个假值(null,undefined,0,'',NaN)
- undefined
- null
- 对象(object)
- 数组(Array) 是一种对象
- 函数(Function) 是一种对象
- 普通对象
##### 类型转换
{
let number = 1;
let string = number '';
console.log(typeof string,string) //string,"1"
}
{
let number = 1;
let bool =!number;
console.log(typeof bool,bool) //boolean,false
}
{
let string = '123';
//let number = string -0;
let number = string;
console.log(typeof number,number) //number,123
}
{
let string = 'hello';
let number = string - 0;
console.log(typeof number,number) //NaN;
}
{
let bool = true;
let number = bool -0;
//let number = !bool -0; number, 0
console.log(typeof number,number) //number,1
}
- ##### 字符串方法以及遍历
- ES5
//遍历
{
let string = "hello";
for(let i = 0;i<string.length;i ){
console.log(string[i])
}
}
//method
{
let str = 'hello';
let newStr = str.substring(1,2); // [start,end)
console.log(str); // 'hello'
console.log(newStr) // 'e'
}
{
let str = 'world';
let newStr = str.substr(1,2); //start, deleteCount
console.log(str); // 'world'
console.log(newStr) // 'or'
}
- ES6
{
let string = "world";
for(let i of string){
console.log(i)
}
}
##### 声明对象以及读写属性、遍历对象
- Obejct是一种无序的集合
- ES5
{
let o = {
name:'小花',
age:18,
skill: function(){console.log('say')}
};
/*let o = new Object({
name:'小花'
}) */
console.log(o.name); //"小花"
o.name = '小草';
console.log(o['name']);//"小草"
console.log('name' in o); //true
delete o.name; //o = {};
}
{
let o = {
name:'小草',
age:18
}
for(let i in o){
console.log(i); //name,age
console.log(o[i]); //小草,18
}
}
- ES6
{
let name = 'xiaohua',age = 16;
let o = {
name,
age,
skill(){
console.log('say')
}
}
console.log(o.skill())
}
{
let a = 'b';
let es5_obj = {
a:'c',
b:'c'
}
let es6_obj ={
[a]:'c' //key可以用变量
}
console.log(es5_obj,es6_obj);
}
##### 声明数组、遍历数组
- Array是一种有序的集合
- 数组的一些方法
- ES5
{
let array = [1,2,3,[4,5,6],{5:"6",6:"7",7:"8"}]; //声明数组
console.log(array);
console.log(array.length);//5;
for(let i = 0; i<array.length;i ){
console.log(i,"-",array[i]);
}
array.push(9,10,11,[12,13,14],{name:"array"});
console.log(array);
array.pop();
console.log(array.length);
}
{
let arr = [2,3,1,4,5];
arr.sort();
console.log(arr);//[1,2,3,4,5]
arr.sort(function(a,b){return a<b});
console.log(arr);//[5,4,3,2,1]
}
{
let arr = [1,2,3,4,5];
let deleteArr = arr.splice(0,2,0,1,2);//array.splice(start, deleteCount,
item1, item2, ...)
console.log(arr);
console.log(deleteArr);
}
{
let arr = [1,2,3,4];
let arrStr = arr.join('--');
console.log(arr);
console.log(arrStr);
let newArrStr = arrStr.split('--');
console.log(newArrStr);
}
- ES6
{ //将伪数组转换成数组
function arg(){
argArray = Array.from(arguments,(item)=> item2);
//Array.from(arrayLike[, mapFn[, thisArg]])
console.log(argArray)
}
/
argArray = Array.from(arguments);
argArray.forEach(function(item){console.log(item)})
*/
arg(1,2,3,4,5)
}
{ //填充数组
let array = [1,2,3,4,5]; //arr.fill(value) arr.fill(value, start)
arr.fill(value, start, end)
newArray = array.fill(0);
console.log(newArray);
console.log(array);
console.log(array.fill(9,0,3));
console.log(array);
}
{ //遍历数组
let array = [1,2,3,4,5];
for(let i of array){
console.log(i) //1,2,3,4,5
}
for(let i of array.keys()){
console.log(i)//0,1,2,3,4
}
for(let [i,v] of array.entries()){
console.log(i,v)
}
console.log(array.find((item)=>item>3));
//查找满足条件,只返回第一个
console.log(array.findIndex(item=>item>3));
{
let array = [1,2,3,4,5];
console.log(array.includes(1,0))//arr.includes(searchElement, fromIndex)
//是否包含
}
}
##### 声明函数,函数提升,arguments及...rest,length属性,闭包,同步V.S.异步
- ES5
// var say = function(){}; 只会提升var say
function say(x){ //提升整个函数
console.log(x);
console.log(arguments)
//将传入所有实参生成一个伪数组,其实是一个key为有序下标的对象
return x //使函数具有返回值
}
say('hello'); //传入实参
console.log(say.length);//行参个数
var c =say('hello'); //返回值赋予变量c
console.log(c);
{ //立即执行函数 防止全局污染
!function(){
var a = 1;
console.log(a)
}();
!function(){
var a = 2;
console.log(a)
}();
}
{ //闭包
function f1(){
var a = 1;
function f2(){
a ;
console.log(a)
}
return f2;
}
let result = f1();
result();
}
{//同步
console.log(1);
console.log(2);
console.log(3);
}
{//异步
console.log(1);
setTimeout(function(){
console.log(2);
},3000)
console.log(3);
}
- ES6
{ //ES6存在块及作用域,不需要使用匿名函数来防止全局污染
let a =1 ;
console.log(a);
}
{
let a = 2;
console.log(a);
}
{
function say(x,y = 'world'){ //行参默认值
console.log(x,y);
}
say('hello');
}
{
let say = (...arg)=>{
console.log(arg);
for(let i of arg){
console.log(i);
}
console.log(typeof arg.push) //这是一个真数组,和arguments不同
}
say('hello','world');
}
{
let x = 'china';
let say = (x,y = x)=>{
console.log(x,y);
}
say('hello');//"hello hello"
}
{
let x = 'china';
let say = (z,y = x)=>{ //变量作用域,和上一个例子比较
console.log(z,y);
}
say('hello');//"hello china"
}
{
let say = (x)=> x ;//此处如果加{}就不会有返回值
/*
var say = function(x){
return x
}
*/
let result = say(100);
console.log(result)
}
{ //函数作为返回值,函数作为参数的例子
let qux= ()=> (callback)=> callback();
let result = qux();
console.log(result);
result(()=>{console.log("执行了")})
}
类、原型、继承(面向对象)
- ES5
{
function Person(name,age,gender){
this.name = name;
this.age =age;
this.gender = gender;
}
Person.prototype.born = function(){
console.log('born')
}
function Man(){
Person.apply(this,arguments)
this.sex = 'male'
}
let empty = function(){};
empty.prototype = Person.prototype;
Man.prototype = new empty();
console.log(Man.prototype.constructor = Man);
var man1 = new Man('张三',18,'male');
console.log(man1)
}
{
var name,age,gender;
var Person = {
name:name,
age:age,
gender:gender,
born:function(){console.log('born')}
}
var Man = Object.create(Person);
Man.sex = 'male';
console.log(Man)
}
- ES6
{//ES6 类
class Person{
constructor(name='张三',age= 18,gender='male'){
this.name = name;
this.age =age;
this.gender = gender;
};
born(){
console.log('born')
};
die(){
console.log('die')
}
}
console.log(new Person)
class Man extends Person{//类的继承
constructor(){
super();
this.sex = 'Man'
}
}
let man1 = new Man()
console.log(man1)
console.log(man1.born())
}
##### 标准库
- Array
- String
- Number
- Function
- Boolean
- Math(console.dir(Math) )
- Math.PI; //3.141592653589793
- Math.SQRT2; //1.4142135623730951
-Math.pow();
-Math.sqrt();
- Math.random()*50 50 ;// 50~100之间的伪随机数
- Date
- new Date()
-
```
{
let date = new Date();
console.log(date);//Sat Jun 03 2017 01:27:41 GMT 0800 (CST)
console.log(date.getFullYear()) //2017
console.log(date.getMonth()) // 5 0~11个月
console.log(date.getDate()) //3
console.log(date.getDay()) //6 星期日为0,星期一为1。
console.log(date.getHours());
console.log(date.getMinutes())
console.log(date.getSeconds())
}
toLocaleString()
Promise
{
function breakfast(callback){
console.log('吃早饭');
callback&&callback();
}
function lunch(){
console.log('吃午饭');
}
console.log(breakfast(lunch))
}
{
let breakfast = function(){
console.log('吃早饭');
return new Promise(function(resolve,reject){
resolve();
})
}
let lunch = function(){
console.log('吃午饭');
return new Promise(function(resolve,reject){
resolve();
})
}
let dinner = function(){
console.log('吃晚饭')
}
breakfast().then(lunch).then(dinner)
}
maxChar = key // 比较后存储次数多的字母
标识符
- 定义:识别具体对象的一个名称(大小写敏感),如变量名,函数名。
- 规则:
- 第一个字符,可是任意Unicode字母,以及美元符号($),和下划线(_)。
- 第二个字符以及后面的字符,除了Unicode,美元符号以及下划线,还可以是数字0-9。
- 保留字和关键字不能作为标识符(如:var 、class、false、true)。
正则写法:
区块(块级作用域)
- ES5:不存在块级作用域
{
var a = 1;
}
console.log(a); // 1
- ES6:使用let、const声明变量或常量(存在块级作用域)
{
let a = 1; const b =1;
}
console.log(a); // ReferenceError: a is not defined
console.log(b); // ReferenceError: a is not defined
{
let a = 1;
let a = 2;
console.log(a) //"SyntaxError: Identifier 'a' has already been declared(同一作用域重复声明一个变量报错)。
}
{
var a = 1;
var a = 2;
console.log(a);//2 var 重复声明同一变量取最后一次声明的赋值。
}
obj[char] // 次数加1
如何在浏览器中运行JavaScript?
- <script> console.log('运行JS') </script>
- <script src='./*'> </script>
注释
- 单行:/这是注释/。
- 多行:/*这是注释*/。
n.push(array[i)
JavaScript之父:Brendan Eich 。
-基本语法:借鉴了C语言和Java语言。
-数据结构:借鉴了Java,包括将值分成原始值和对象两大类。
- 函数的用法:借鉴了Scheme和Awk语言,将函数当成第一等公民,引入闭包。
- 原型继承模型:借鉴了Self语言。
- 正则表达式:借鉴了Perl语言。
- 字符串和数组处理:借鉴了Python语言。
* 获取字符串中出现次数最多的字母
JavaScript 声明变量
- var a;
- let a;
} )
JavaScript与ECMAScript的关系?
- ECMAScript规定了浏览器脚本语言的标准。
- ECMAScript是JavaScript的规格。
}
变量赋值
- ES5
var a = 1; //window.a = 1; 全局变量
function(){var a = 1;} //只能在函数体内访问到变量a
- ES6新增结构赋值
let a = 1; //window.a ===undefined;
{
let a,b,c;
[a,b,c=3] = [1,2]; // let a = 1; let b = 2; let b =3;
}
{
let a,b,c;
[a,,b,,c] = [1,2,3,4,5,6,7,8,9,10];
console.log(a,b,c); //1,3,5
}
{
let o = {a:1,b:2};
let {a,b} = o;
console.log(a,b);//1,2
}
{
let o = {a:1,b:2};
let {a=2,b} = o;
console.log(a,b);//1,2
}
{
let metaData = {
number:'1',
info:[{
name:'chen'
}]
};
let {number:Num,info:[{name:name}]} = metaData;
console.log(Num,name); // Num:'1',name:'chen'
}
{
function test(){
return [1,2,3,4,5,6,7]
}
let a;
[...a] = test(); // let a = [1,2,3,4,5,6,7];
}
{
let a = 1; let b = 2;
[a,b] = [b,a];
console.log(a,b) //变量交换
}
{
let a,b,c;
[a,b,...c] = [1,2,3,4,5,6,7]; // let a = 1;let b = 2; let c = [4,5,6,7];
}
{
let a,b;
({a,b} ={a:1,b:2});
console.log(a,b); // 1,2;
}
}
JavaScript 变量声明提升
- ES5
console.log(a); //undefind
var a = 1;
//等同如下
var a;
console.log(a); //undefind
a = 1;
- ES6:let声明变量不提升
console.log(a); // ReferenceError: a is not defined
let a = 1;
*/
JSON.parse(JSON.stringify(obj)
console.log(arr) // [{name: "赵六", age: 6},{name: "张三", age: 16},{name: "王五", age: 19},{name: "李四", age: 45}]
.sort((a, b) => a
if (src[prop].constructor === Array){
var n = []; //一个新数组
return -1 //顺序不改变
// console.log("对象");
if (obj[key] > maxNum) {
arr = Object
if(m>n){
.reduce((a, b) => a.concat(b), [])
var arry =[1,25,15,1,2,15,5,15,25,35,1]
console.log(arr); // 输出 [-1, 0, 1, 2, 3, 4, 5]
8.约瑟夫环问题
}
function getChar(str) {
for (let i = 0; i < str.length; i ) { // 遍历字符串每一个字母
let maxNum = 0 // maxChar字母对应的次数
console.log(getCamelCase("xiaoshuo-ss-sfff-fe"))
}
.sort((a, b) => a - b);
let str = 'aabbbccdd'
}})
for(let key in obj) { // 遍历obj
function getCase(){
编号为1到100的一百个人围成一圈,以123123的方式进行报数,数到3的人自动退出圈子,剩下的人继续报数,问最后剩下的人编号为几?
}
}
return arr.map( function( item, index ) {
obj[char] = obj[char] || 0 // 保证初始值为0
- b); // 如果需要从小到大排序加上这个
[...new Set(arr)]
或者 简写为
console.log(arr) // [0, 1, 3, 4, 25, 85]
同理:
浅拷贝只需要Object.assign();
})
}
循环一下字符串,第一次出现
}
if(n.indexOf(array[i) === -1){ //检测到没有 则添加
- getChar(str))
}).join( '' );
ES6 set法:
正则写法
return str.replace( /-([a-z])/g , function( all, i){ // 注意正则中的(),这里可以匹配到 -s 和s
arr.sort((m,n)=>{
https://www.cnblogs.com/zjfjava/p/5994437.html
1 function josephus2 (n,k){
2 var players = [];
3 for(var i=1;i<=n;i ){ // 组成数组
4 players.push(i);
5 }
6 var flag=0;
7 while(players.length>1){
8 var outNum = 0; //本次循环已出局人数,便于定位元素位置
9 var len = players.length;
10 for(var i=0;i<len;i ){
11 flag ;
12 if(flag==k){
13 flag=0;
14 console.log("出局:" players[i-outNum] "本次循环已出局人数:" outNum);
15 // i循环时受数组长度影响 每从头开始时i变为0; 但flag始终是123..k 123..k 固定循环,不受数组影响 ,
16 players.splice(i-outNum,1);
17 outNum ;
18 }
19 }
20 }
21 return players[0];
22 }
23 console.log(josephus2(100,3));
console.log([...set])
console.log('出现次数最多的字母为:'
arr.sort((m,n)=>{
return item;
let maxChar // 存储字母
function getCamelCase(str) {
var set =new Set(arry)
}
递归解法:
考虑去重(数组转化为对象,利用对象键名唯一性,然后.keys一次性取键名)
* @param {String} str
}
符合json要求(双引号)的对象深拷贝:
}
3.统计字符串中出现最多的字母
[{'name': '张三', age: 16},{'name': '李四', age: 45},{'name': '王五', age: 19},{'name': '赵六', age: 6}] 按年龄排序
}).join('');
}else{
return str.replace( /[A-Z]/g , function(i) {
deepCopy(src[prop], r[prop]);
console.log(arr) // [0, 1, 3, 4, 25, 85]
}
console.log(item)
.map(num => num) // 这行主要是将键名取出来之后,数组中全部是字符串,将其都转成数字,以便后面排序
http://tingyun.site/2018/04/26/约瑟夫环问题详解/
r[prop]={};
/**
function deepCopy(src, r) {
var arr = [[1, 2], [0, 3, 5], [-1, 4]];
}else{
本文由wns9778.com发布于计算机教程,转载请注明出处:JavaScript Foundation
关键词: wns9778.com