欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  新闻

js反转数组的方法(前端面试题2021及答案)

程序员文章站 2022-09-16 23:07:13
备忘单是我们开发人员始终需要的参考。因此,这里我编译了许多javascript参考代码。查看分类并找到它。这篇文章对学习者和开发人员都有帮助。javascript number方法备忘单toexpon...

备忘单是我们开发人员始终需要的参考。因此,这里我编译了许多javascript参考代码。查看分类并找到它。这篇文章对学习者和开发人员都有帮助。

js反转数组的方法(前端面试题2021及答案)

javascript number方法备忘单

  • toexponential():以字符串形式返回表示number对象的字符串
  function expo(x, f) {
      return 
      number.parsefloat(x).toexponential(f);
  }

  console.log(expo(123456, 2)); 
  // -> 1.23e+5
  • tofixed():使用定点表示法格式化数字
  function financial(x) {
      return number.parsefloat(x).tofixed(2); 
  }

  console.log(financial(123.456)); 
  // -> 123.46
  • toprecision():以指定的精度返回表示number对象的字符串
  function precise(x) {
      return
      number.parsefloat(x).toprecision(4); 
  }

  console.log(precise(123.456)); 
  // -> 123.5
  • tostring():返回表示指定number对象的字符串
  function hexcolour(c) {
      if (c < 256) {
          return math.abs(c).tostring(16); 
      }
      return 0; 
  }

  console.log(hexcolour(233)); 
  // -> e9
  • valueof():返回数字对象的包装原始值
  const numobj = new number(42); 
  console.log(typeof numobj); 
  // -> object

  const num = numobj.valueof(); 
  console.log(num); 
  // -> 42

  console.log(typeof num); 
  // -> number


javascript循环备忘单

  • 对于循环
  for (var i = 0; < 10; i++) {
      console.log(i + ": " + i * 3 + "<br />"); 
  }
  // -> 0: 0<br />
  // -> 1: 3<br />
  // -> ...

  let a = [1, 2, 3]; 
  var sum = 0; 
  for (var i - 0; i <a.length; i++) {
      sum += a[i]; 
  } // pasing an array
  console.log(sum); 
  // -> 6
  • while循环
  var i = 1;                  // initialize
  while (i < 100) {          // enters the cycle if statement is true
      i *= 2;                 // increment to avoid infinte loop 
      console.log(i + ", "); // output
  } 
  // 2, 
  // 4, 
  // ...
  // 128, 
  • 循环执行
  var i = 1;                  // initialize
  while (i < 100) {          // enters the cycle asleast once
      i *= 2;                 // increment to avoid infinte loop 
      console.log(i + ", "); // output
  } while (1 < 100); // repeats cycle if statement is true at the end
  // 2, 
  // 4, 
  // ...
  // 128,
  • 打破
  for (var i = 0; i < 10; i++) {
      if (i == 5 ) { break; } // stops and exits the cycle
      console.log(i + ", ");  // lat output number is 4
  }
  // -> 0, 
  // -> 1, 
  // ...
  // -> 4, 
  • 继续
  for (var i = 0; i < 10; i++) {
      if (i == 5 ) { continue; } // skips the rest of the cycle
      console.log(i + ", ");  // skips 5
  }
  // -> 0, 
  // -> 1, 
  // ...
  // -> 9,

javascript字符串方法备忘单

  • charat():返回指定索引处的字符
  const sentence = "jeff bezos is now the second richest."; 

  const index = 4; 

  console.log(`the character at index ${index} is ${sentence.charat(index)}`); 
  // the character at index 4 is f
  • concat():连接两个或多个字符串,并返回所连接字符串的副本
  const str1 = "hello"; 
  cosnt str2 = "world"; 

  console.log(str1.concat(" ", str2)); 
  // -> hello world

  console.log(str2.concat(", ", str1)); 
  // -> world, hello
  • replace():搜索子字符串(或正则表达式)和字符串之间的匹配项,并将匹配的子字符串替换为新的子字符串
  const p = "talk is cheap. show me the work. - someone"; 

  console.log(p.replace("work", "code")); 
  // -> talk is cheap. show me the code. - someone
  • search():搜索正则表达式和字符串之间的匹配项,并返回匹配项的位置
  const paragraph = "the quick brown fox jumps over the lazy dog."; 

  // any character that is not a word character or whitespace
  const regex = /[^ws]/g;

  console.log(paragraph.search(regex)); 
  // -> 43
  • slice():提取字符串的一部分并返回新的字符串
  const str = "the quick brown fox jumps over the lazy dog."; 

  consolelog(str.slice(31)); 
  // -> the lazy dog

  console.log(str.slice(4, 19)); 
  // -> quick brown fox
  • trim():删除字符串两端的空格
  const greeting = "  hello world!   "; 

  console.log(greeting); 
  // -> hello world!

  console.log(greeting.trim()); 
  // -> hello world!
  • substr():从字符串中提取字符,从指定的起始位置开始,直到指定的字符数
  const str = "mozilla"; 

  console.log(str.substr(1, 2)); 
  // -> oz

  console.log(stre.substr(2)); 
  // -> zilla
  • tolowercase():将字符串转换为小写字母
  const sentence = "elon became the richest last night."; 

  console.log(sentence.tolowercase()); 
  // -> elon became the richest last night.

javascript数组方法指导表

  • concat():连接两个或多个数组,并返回联接数组的副本
  let array1 = ["a", "b", "c"]; 
  let array2 = ["d", "e", "f"]; 
  let array3 = array1.concat(array2); 

  console.log(array3); 
  // -> array(6) ["a", "b", "c", "d", "e", "f" ]
  • indexof():在数组中搜索元素并返回其位置
  let beasts = ["ant", "bison", "camel", "duck", "bison"]; 

  console.log(beasts.indexof("bison")); 
  // -> 1

  // start from index 2
  console.log(beasts.indexof("bison", 2)); 
  // -> 4
  • join():将数组的所有元素连接到一个字符串中
  let elements = ["fire", "air", "water"]; 

  console.log(elements.join()); 
  // -> fire,air,water

  console.log(elements.join(" ")); 
  // -> fire air water
  • pop():删除数组的最后一个元素,并返回该元素
  let plants = ["broccoli", "cauliflower", "cabbage", "kale", "tomato"]; 

  console.log(plants.pop()); 
  // -> tomato

  console.log(plants); 
  // -> array(4) ["brocxoli", "cauliflower", "cabbage", "kale"]
  • reverse():反转数组中元素的顺序
  let array1 = ["one", "two", "three"]; 
  console.log("array1:", array1); 
  // -> array1: array(3) [ "one", "two", "three" ]

  let reversed = array1.reverse(); 
  console.log("reversed", reversed); 
  // -> reversed: array(3) [ "three", "two", "one" ]
  • shift():删除数组的第一个元素,并返回该元素
  let array1 = [1, 2, 3]; 

  let firstelement = array1.shift(); 

  console.log(array1); 
  // -> array [ 2, 3 ]
  • sort():对数组的元素进行排序
  let months = ["march", "jan", "feb", "dec"]; 
  months.sort(); 

  console.log(months); 
  // -> array(4) [ "dec", "feb", "jan", "march" ]
  • tostring():将数组转换为字符串,并返回结果
  const array1 = [1, 2, "a", "1a"]; 

  console.log(array1.tostring()); 
  // -> 1,2,a,1a

javascript数据类型备忘单

var age = 18; // number

var name = "rahul"; // string

var name = {first:"rahul", last:"singh"}; // object

var truth = false; // boolean

var sheets = ["html", "css", "js"]; // array

var a; typeof a; // undefined 

var a = null; // value null

javascript运算符备忘单

a = b + c - d; // addition, substraction

a = b * (c / d); // multiplication, division

x = 100 % 48; // modulo. 100 / 48 remainder = 4

a++; b--; // postfix increment and decrement

变量备忘单

  • var:最常见的变量。可以重新分配,但只能在函数中访问。执行代码时,用var定义的变量移到顶部。
  • const:在出现在代码中之前无法重新分配并且无法访问
  • let:与const类似,但是可以重新分配let变量,但不能重新声明
var a;            // variable
var b = "init";   // string
var c = "hi" + "" + "rahul"; // "hi rahul"
var d = 1 + 2 + "3";   // "33"
var e = [2,3,5,8];   // array
var f = false;       // boolean
var g = /()/; // regex
var h = function(){};   // function object
const pi = 3.14;        // constant
var a = 1, b = 2, c = a + b;    // one line
let z = 'zzz';        // block scope local variable

获取日期方法提示表

  • getfullyear():根据当地时间返回指定日期的年份
  const moonlanding = new date("january 08, 69 00:20:10"); 

  console.log(moonlanding.getfullyear()); 
  // -> 1969

  • getmonth():根据本地时间返回指定日期中的月份,该值从零开始(其中零表示一年的第一个月)。
  const moonlanding = new date("january 08, 69 00:20:10"); 

  console.log(moonlanding.getmonth()); // (january gives 0)
  // -> 6
  • getdate():根据当地时间返回指定日期的月份
  const birthday = new date("june 16, 2004 23:14:00"); 
  const date1 = birthday.getdate(); 

  console.log(date1); 
  // -> 19
  • gethours():根据当地时间返回指定日期的小时
  const birthday = new date("june 16, 04 4:20"); 

  console.log(birthday.gethours()); 
  // -> 4
  • getminutes():根据当地时间返回指定日期的分钟
  const birthday = new date("june 16, 04 04:10"); 

  console.log(birthday.getminutes());
  // -> 20
  • getseconds()根据当地时间返回指定日期中的秒数
  const moonlanding = newdate("june 16, 69 00:23:11"); 

  console.log(moonlanding.getseconds()); 
  // -> 18