//第一次课案例
document.write('<br/>1.全局变量与局部变量--------------------------------<br/>');
//全局变量与局部变量
var globalVariable1 = 0;
function test1()
{
document.write(globalVariable1+ "<br/>");
var localVariable1 = 2;
//打印globalVariable1
document.write(globalVariable1 + "<br/>");
//打印localVariable1
document.write(localVariable1 + "<br/>");
}
//打印localVariable1
//document.write(localVariable1);
//打印globalVariable2
document.write(globalVariable2 + "<br/>");
test1();
var globalVariable2 = 1;
function test2()
{
var localVariable2 = 3;
//打印localVariable1
//document.write(localVariable1 + "<br/>");
}
test2();
//打印localVariable2
//document.write(localVariable2 + "<br/>");
//打印globalVariable1
document.write(globalVariable1+ "</br>");
//////////////////////////////////////////////////////////
//第二次课案例
function writeDocument(variable) {
document.write(variable + "<br/>");
}
writeDocument("//---------------charAt---------------------------------");
var welcomeString="Welcome to our class.";
writeDocument(welcomeString.charAt(2));
writeDocument(welcomeString.charAt(6));
writeDocument("//---------------substring---------------------------------");
writeDocument(welcomeString.substring(2));
writeDocument(welcomeString.substring(8,2));
writeDocument("//---------------replace---------------------------------");
var sentence="We have fun in our class.Do you have fun in your class?";
writeDocument(sentence);
writeDocument(sentence.replace("class","programing class"));
writeDocument(sentence.replace(/class/g,"programing class"));
writeDocument("//---------------split---------------------------------");
writeDocument(sentence.split(" "));
writeDocument(welcomeString.split(""));
writeDocument(sentence.split("."));
writeDocument("//---------------indexOf/laseIndexOf/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(stringValue, char) {
//var charCount = 0;
//for(var i=0;i<stringValue.length;++i){
//if(stringValue)
//}
//return charCount;
//}
//var stringToCount = "Can you can a can as a Canner can can a can";
//writeDocument("There are " + countChar(stringToCount, "c") + " c in " + stringToCount);
/////////////////数组的案例
writeDocument("数组创建----------------------------------");
var arr=["HTML","css","JavaScript","jQuery","Vue.js"];
writeDocument(arr);
writeDocument("数组下标取值------------------------------");
writeDocument(arr[2]);
writeDocument("数组赋值---------------------------------");
arr[4]="React.js";
writeDocument(arr);
arr[5]="Vue.js";
writeDocument(arr);
arr[8]="Angular.js";
writeDocument(arr);
writeDocument("//---case(大小写转换)----------------------------");
writeDocument(welcomeString.toLowerCase());
writeDocument(welcomeString.toUpperCase());
writeDocument("//---charAt(获取指定位置字符)----------------------------");
writeDocument(welcomeString.charAt(2));
writeDocument(welcomeString.charAt(6));
writeDocument("//---substring(截取)----------------------------");
writeDocument(welcomeString.substring(3));
writeDocument(welcomeString.substring(0,2));
writeDocument("//---replace(替换)----------------------------");
var setence = "we have fun in our class. Do you have fun in your class?";
writeDocument(setence);
writeDocument(setence.replace("class","programming class"));
writeDocument(setence.replace(/class/g,"programming class"));
writeDocument("//---split(分割)----------------------------");
writeDocument(setence.split(" "));
writeDocument(welcomeString.split(""));
writeDocument(setence.split("."));
writeDocument("//---indexOf/lastindexOf(查找);match/search(统计字符)----------------------------");
writeDocument(setence.indexOf("class"));
writeDocument(setence.lastIndexOf("class"));
writeDocument(setence.match("class"));
writeDocument(setence.match(/class/g));
writeDocument(setence.search(/class/g));
writeDocument("//---题目:不区分大小写,找出句子中的c----------------------------");
function countChar(stringValue, char) {
var charCount=0;
for(var i=0;i<stringValue.length;++i){
if(stringValue.charAt(i).toLowerCase()==char){
++charCount;
}
}
return charCount;
}
var stringValue="Can you can a can as a Canner can can a can";
writeDocument("There are " + countChar(stringValue,"c")+" c in sentence.");
writeDocument("//任意字符串,统计里面有多少个数字。----------------------------");
function countNumber(str) {
var num = 0;
for (var i = 0; i < str.length; i++) {
var char = str.charAt(i);
if (char != " " && !isNaN(char)) {
num++;
}
}
return num;
}
document.write(countNumber("I have 2 eggs and 26 apples")+"<br/>");
console