console
var globalVariable1 = 0;
function test1() {
var localVariable1 = 2;
document.write(globalVariable1 + "<br/>");
document.write(localVariable1+ "<br/>");
}
document.write(globalVariable2 + "<br/>");
test1();
var globalVariable2 = 1;
function test2() {
var localVariable2 = 3;
}
test2();
document.write(globalVariable1 + "<br/>");
function addSum(a, b)
{
var sum = a + b;
return sum;
}
var n = addSum(1, 2) + 100;
document.write(n+ "<br/>");
function isLeapYear(year)
{
if(year % 4 == 0 )
{
document.write(year +' is a Leap Year'+ "<br/>");
}
else
{
document.write(year +' is not a Leap Year'+ "<br/>");
}
}
isLeapYear(2020);
isLeapYear(2000);
isLeapYear(2001);
writeDocument("//---比较大小------------------------------------------------------------------");
var sy=[23,179,3,78];
function comparer(a)
{
var max=0;
for(var i=0;i<a.length;i++)
{
if(a[i]>=max)
{
max=a[i];
}
}
return max;
}
writeDocument(comparer(sy)+ "<br/>");
writeDocument("//---定义两个函数,它们的功能分别是求出任意五个数中最大最小-------");
function max(sy)
{
var temp=sy[0];
for (var i=0;i<sy.length;i++)
{
if(temp<=sy[i])
{
temp=sy[i];
}
}
return temp;
}
function min(sy)
{
var temp=sy[0];
for (var i=0;i<sy.length;i++)
{
if(temp>=sy[i])
{
temp=sy[i];
}
}
return temp;
}
b=[23,234,8,221,76];
document.write(max(b)+ "<br/>");
document.write(min(b)+ "<br/>");
var welcomeString="Welcome";
document.write(welcomeString.length+ "<br/>");
function writeDocument(variable)
{
document.write(variable + "<br/>");
}
var welcomeString="Welcome";
writeDocument(welcomeString);
writeDocument(welcomeString.length);
writeDocument("//---case------------------------------------------------------------------");
writeDocument(welcomeString.toLocaleLowerCase);
writeDocument(welcomeString.toLocaleUpperCase);
writeDocument("//---chatAt------------------------------------------------------------------");
writeDocument(welcomeString.charAt(2));
writeDocument(welcomeString.charAt(6));
writeDocument("//---substring------------------------------------------------------------------");
writeDocument(welcomeString.substring(3));
writeDocument(welcomeString.substring(0,2));
var sentence="We have fun in our class.Do you have fun in your class?"
writeDocument(sentence);
writeDocument(sentence.replace("class","programming class"));
writeDocument(sentence.replace(/class/g,"programming class"));
writeDocument("//---split------------------------------------------------------------------");
writeDocument(sentence.split(" "));
writeDocument(sentence.split(""));
writeDocument(sentence.split("."));
writeDocument("//---indexOf/lastIndexOf/match/search------------------------------------------------------------------");
writeDocument(sentence.indexOf("class"));
writeDocument(sentence.lastIndexOf("class"));
writeDocument(sentence.match("class"));
writeDocument(sentence.match(/class/g));
writeDocument(sentence.search(/class/g));
function countChar(stringToCount, char)
{
var charCount = 0;
for (var i=0;i<stringToCount.length;i++)
{
if (stringToCount.charAt(i).toLowerCase()==char)
{
charCount++;
}
}
return charCount;
}
var stringToCount = "Can you can a can as a Canner a can I u";
writeDocument("There are " + countChar(stringToCount, "c") + " c in " + stringToCount);
writeDocument("There are " + countChar(stringToCount, "n") + " n in " + stringToCount);
function countNumber(stringtoCount)
{
var numCount = 0;
for (var i=0;i<stringtoCount.length;i++)
{
if (stringtoCount.charAt(i)!=" "&&!isNaN(stringtoCount.charAt(i)))
{
numCount++;
}
}
return numCount;
}
var stringtoCount = "172 Yuzuru Haruv1994";
writeDocument("There are " + countNumber(stringtoCount) + " number in " + stringtoCount);
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<script>
function welcomeMes()
{
alert("生命不息,\n代码不止!");
}
</script>
</head>
<body>
< a href=" ">Welcome</ a>
</body>
</html>