Good programmers web front-end share exercises a front-end javascript

Good programmers web front-end tutorial we will continue to share front-end javascript series of exercises.
Math object
1. Write a function to obtain a random color hexadecimal string (e.g.: # 20CD4F)
Method:
function F2 () {
var STR = "0123456789abcdef";
var Color = "#";
for (var 0 = I; I <. 6; I ++) {
var NUM = Math.floor (Math.random () * str.length);
Color = Color + [NUM] STR;
}
the console.log (Color);} F2 () ;
DATE objects
digital clock
ideas analysis: the pictures of minutes and seconds, named according to certain rules, to facilitate a subsequent image path in accordance with time setting
method:
<div ID = "PIC">
<IMG the src = "IMG / 0.png" Alt = " "/>
<IMG the src =" IMG / 0.png "Alt =" "/>
when <span> </ span>
<IMG the src =" IMG / 0.png "Alt =" "/>
<IMG the src =" img / 0.png "alt =" " />



<span> s </ span> </ div>
<Script>
function F1 () {
var STR = "";
// get all of the pictures stored in the variable by imgid tag
(img ") var imgid = document.getElementsByTagName " ;
var = new new Odate a Date (); // creation time objects
var h = oDate.getHours (); // get the current to each of minute and second
var oDate.getMinutes FEN = ();
var Miao oDate.getSeconds = ();
var h1 = h> = 10 h : "0" + h; // are guaranteed by two bits?
var FENl FEN => 10 = FEN:? "0" + FEN;
var = miao1 Miao> = 10 Miao? : "0" + Miao;
STR = h1 of STR + + + FENl miao1; // combined into a new string
for (var i = 0; i <str.length; i ++) {// iterate string
// analog = src "IMG / 0.png";
imgid [I] = .src 'IMG /' + STR [I] + '. PNG'; // set the path for each image src
}
}
the setInterval (f1,1000);// After the timer parameter milliseconds
wrapper function
encapsulation methods: as a function of the parameter object
eg1 :. determined for a year is a leap year, the date formatted output "2015 | 08 | 24", number of days of a month, determines the number of days between two dates, days after the date of obtaining N
var = {dateutil
isLeapYear: function (year) {
IF (year. 4 == 0 && year% 100% 400% = 0 == 0 || year!) {
return to true;
}
return to false;
},
the formatDate: function (DATE, STR) {
var year = DATE .getFullYear ();
var = month The date.getMonth () +. 1;
var Day = date.getDate ();
IF (month The <10) {
month The = "0" + month The;
}
IF (Day <10) {
Day = "0" Day +;
}
var SS = year + month The STR + + + STR Day
return SS;
},
getDays: function (DATE) {
var year = the Date.getFullYear ();
var = month The date.getMonth () +. 1 ;
Switch (month The) {
Case 2:
IF (dateUtil.isLeapYear (year)) {
return 29;
}
return 28;
BREAK;
Case. 4:
Case. 6:
Case. 9:
Case. 11:
return 30;
BREAK;
default:
return 31 is;
}
},
getDiffDays: function (date1, DATE2) {
// subtract two dates will be a difference between the number of milliseconds
// difference of every second day
var the Math.abs SS = ((DATE2-date1) / 1000);
var = Day Math.floor (SS / 24 / 3600);
var = Math.floor hour (SS / 24% 3600);
var = Math.floor minute (SS / 60% 60);
var = SECOND Math.floor (SS 60%);
return day + "days" + hour + "when" + minute + "min" + second + "seconds";
},
getNDays: function (n-) {
var = new new Odate a Date ();
oDate.setDate (oDate.getDate () + n-);
Odate return;
}};
the DOM and BOM

Guess you like

Origin blog.51cto.com/14479068/2441571