Example analysis of JS limit condition complementary problem _javascript skill

Source: Internet
Author: User
Tags javascript array

In this paper, we analyze the problem of JS limit condition completion. Share to everyone for your reference, specific as follows:

Topic I. A and b two variables, without the third variable to switch two variable values

var a=5;
var b=6;
A=a+b;
B=a-b;
A=a-b;
alert (a);
alert (b);

Topic Two. There is a number of n=5, without a For loop, how to return an array of [1,2,3,4,5]

Method I. Traversal with recursive return

var n=5;
Function Show () {
  var arr=[];
  Return (function () {
    arr.unshift (n);
    n--;
    if (n!=0) {
      Arguments.callee ();
    }
    return arr;
  }) ();
}
Alert (Show (n));

Method Two. Matching the number of simulations with regular

var n=5;
var arr=[];
arr.length=n+1;
var arr2=[];
var str=arr.join ("a"); The middle connector with a, to six number only five commas, so length=n+1
var re=/a/g;
Str.replace (Re,function () {
  arr2.unshift (n--);
});
alert (ARR2);

Topic Three. A few n, when N<100 returns N, otherwise returns 100, but cannot use if else, three mesh, switch, etc.

Method one. Math.min

var n=150;
Function Show () {return
  math.min (n,100);
}
Alert (Show (n));

Method two. Sort by array

var n=50;
Function Show () {
  var arr=[n,100];
  Arr.sort ();
  return arr[0];
}
Alert (Show (n));

Method Three. According to the length of the digital rotation string, use the For loop, if the length is less than 3, return itself, greater than or equal to 3 to return 100

var n=150;
Function Show () {
  var str= "n";
  for (;str.length<3;) {return
     n;
  }
  for (;str.length>=3;) {return
  }
}
Alert (Show (n));

Method Four. JSON

var n=50;
Function Show () {
  var json={};
  var m=n<100| | Json M is a Boolean value for
  (Var attr in M) {return
    ;
  }
  return n;
}
Alert (Show (n));

More readers interested in JavaScript-related content can view the site topics: "javascript array Operation tips Summary", "JavaScript Sorting algorithm Summary", "JavaScript traversal algorithm and Skills summary", " JavaScript Mathematical Operational Usage Summary, JavaScript data structure and algorithm skills summary, JavaScript Search algorithm Skills summary and JavaScript error and Debugging skills summary

I hope this article will help you with JavaScript programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.