js basic exercises (1)
1. Write a function to calculate the sum, difference, product and quotient of two numbers. Requirements: Use the form of reference
function f1(a,b){ console.log("and"+(a+b)+"difference"+(a-b)+"product"+(a*b)+"merchant"+(a/b)); } f1(2,4); f1(5,8);
2. Write a function to calculate the size of three digits and output them in order from small to large.
function f2(a,b,c){ //Default a<b<c var min=a; if(a>b){ a=b; b=min; } if(a>c){ a=c; c=min; } if(b>c){ min=b; b=c; c=min; } console.log("The order from small to large is"+a+"<"+b+"<"+c); } f2(3,4,1);
3. Write functions of sum, difference, product and quotient of any number
function f3(){ var array=[1,2,3,4,5]; var he=array[0]; var cha=array[0]; var ji=array[0]; var shang=array[0]; for(var i=1;i<5;i++){ he+=array[i]; cha-=array[i]; ji*=array[i]; shang/=array[i]; } console.log("Digital sum"+he+","+"The number difference is"+cha+","+"Digital product"+ji+","+"Digital quotient"+shang); } f3();
4. Write a function that generates 4-digit validation codes 10 times, and store the results in an array.
function f4(){ var empty=[]; for(var i=0;i<10;i++){ var a=Math.floor(Math.random()*10); var b=Math.floor(Math.random()*10); var c=Math.floor(Math.random()*10); var d=Math.floor(Math.random()*10); empty[i]=a.toString()+b.toString()+c.toString()+d.toString(); } console.log(empty); } f4();
5. Write a function to calculate the odd number of any two digits. The number must be a digit. For example, to calculate the odd number of components between 0 and 3 is 01, 21, 03, 13, 23, 31.
function f5(a,b){ var array1=[];//Number stored between a and B var array2=[];//Store odd numbers between a and B var array3=[];//Store all odd numbers var array4=[];//Store no equal odd numbers if(a>b){ //Judging the sizes of a and b var tmp=a; a=b; b=tmp; } for(var i=a;i<=b;i++){ array1[array1.length]=i; } for(j=0;j<=Math.abs(b-a);j++){ if(array1[j]%2!==0){ array2[array2.length]=array1[j] } } for(var k=0;k<array1.length;k++){ for(var m=0;m<array2.length;m++){ array3[array3.length]=String(array1[k])+String(array2[m]); if(String(array1[k])!=String(array2[m])){ array4[array4.length]=String(array1[k])+String(array2[m]); } } } //console.log(array1); //console.log(array2); //console.log(array3); console.log(array4); } f5(9,0);