//课后作业,求red出现的位置['red','blue','green','pink','red'],
var arr = ['red','blue','green','pink','red'];
var index = arr.indexOf('red');
var number = 0;
while (index !== -1){
console.log(index);
number++;
index = arr.indexOf('red',index+1);
}
console.log(number);
<script type="text/javascript">
//根据位置返回字符
//1.charAt(index)根据位置返回字符
var str = 'andy';
console.log(str.charAt(3));
//遍历所有的字符
for (var i = 0;i < str.lengh;i++) {
console.log(str.charAt(i));
}
//2.charCodeAt(index)返回相应索引号的字符ASCII值,目的:判断用户按下了哪个键、、
console.log(str.charCodeAt(0));
//3.str[index] H5里面新增的,和第一个效果一样,只不过兼容性不行
console.log(str[0]);
</script>