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.