<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
</body>
<script type="text/javascript">
//求两个数据之间的和
var c = 0;
function add(a,b){
for(var i = a; i <= b; i++){
c = c+i;
}
document.write(c);
}
add(1,100);
</script>
</html>
运行结果
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
</body>
<script type="text/javascript">
//在数组里添加数据的时候去重
//定义一个数组,向后添加一个数,判断后添加的数值与数组中是否重复
var e = prompt("请输入一个数字:");
var array = new Array(1,2,3,4);
add(e);
function add(a){
for(var i=0;i<array.length;i++){
if(array[i] == a){
var b = "数据已重复"
document.write(b);
}
}
if(b == "数据已重复"){
document.write("数组为"+array);
}else{
array.push(a);
document.write("新的数组为"+array );
}
}
</script>
</html>
运行结果
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
</body>
<script type="text/javascript">
//关于字符串方法的练习题====》反转字符串
var aStr="apple"
var i=aStr.length;
i=i-1;
for (var j = i; j >=0; j--)
{
document.write(aStr.charAt(j));
}
</script>
</html>
运行结果
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
</body>
<script type="text/javascript">
//在数组中查找索引位置
function find(arr,num){
var i = arr.length; //得到数组长度
while(i){
if (arr[i] == num){ //判断数组与要查找索引的数字是否相同
document.write(i+1); //如果相同打印位置
return i;
}
i--;
}
}
var a=[1,2,3,4];
find(a,3);
</script>
</html>
运行结果