打开HTML文件时JS无法正常显示

时间:2017-10-19 19:50:05

标签: javascript jquery html

当我尝试在浏览器中打开HTML文件时,我的JS文件中没有出现任何代码。我还是JS的新手,所以我不确定我是否缺少简单的语法,但我通过验证器运行它并且它似乎没有错误。我不太确定是什么导致这种情况发生,并希望得到任何帮助!

HTML文件:

<!doctype html>
<html>
 <head>
 <title>Awesome JavaScript Arrays</title>
 </head>
 <body>
 <script src = "kyle.js"></script>
 </body>
</html>

JS文件:

    function arrayAverage (nums) {
        var sumSoFar = 0;

    nums.forEach(function(value) {
        sumSoFar = (sumSoFar + value);
    }
    );
    return (sumSoFar / nums.length);
    }

    /* 2 */
    function arrayLargest( array ){
        return Math.max.apply( Math, array );

}

/* 3 */
function isEven (array){
    var count1 = 0;
    for (var i = 0; i < array.length; i++) {
        if (array[i] % 2 == 0)
        {
            count1++;
        }
    }

    if (count1 >= 1) {
        return true;
    }
    else{
        return false;
    }
}

/* 4 */
function isAllEven (array){
    var count2 = 0;
    for (var i = 0; i < array.length; i++) {
        if (array[i] % 2 == 0)
        {
            count2++;
        }
    }

    if (count2 == array.length) {
        return true;
    }
    else {
        return false;
    }

}

/* 5 */
function isInString (strings, string){
    var findString = false;
    strings.forEach(function(wordCheck)){
        if (wordCheck == string) {
            findString = true;
        }
        else{
            findString = false;
        }
    }
    return findString;
}

//Testing the functions by writing to the document.
document.writeln("1. A function that accepts an array of numbers as an argument and returns their average. The average of 4, 6, and 20 is: ");
document.write(arrayAverage([4,6,20]));
document.writeln("<br>");
document.writeln("<br>");
document.writeln("2. A function that accepts an array of numbers as an argument and returns the largest number in the array. The largest of 3, 9, and 14 is: ");
document.write(arrayLargest([3,9,14]));
document.writeln("<br>");
document.writeln("<br>");
document.writeln("3. A function that accepts an array of numbers and returns true if it contains at least one even number, false otherwise. Does 5, 5, and 8 contain an even number? ");
document.write(isEven([5,5,8]));
document.writeln("<br>");
document.writeln("<br>");
document.writeln("4. A function that accepts an array of numbers and returns true if every number is even, false otherwise. Is the array of [2, 4, 5, 8] all even? ");
document.write(isAllEven([2,4,5,8]));
document.writeln("<br>");
document.writeln("<br>");
document.writeln("5. A function that accepts two arguments an array of strings and a string and returns true if the string is contained in the array, false otherwise. Is bacon contained in the array [eggs, milk, bacon]? ");
document.write(isInString(["eggs", "milk", "bacon"], "bacon"));

1 个答案:

答案 0 :(得分:0)

如果您在Chrome浏览器中,请打开浏览器控制台,在Linux上shift+ctrl+j,看看您是否正确加载了页面中的脚本或任何其他错误。

我可以看到你没有写好括号,用这个替换你的函数:

function isInString (strings, string){
var findString = false;
strings.forEach(function(wordCheck){
    if (wordCheck == string) {
        findString = true;
    }
    else{
        findString = false;
    }
})
return findString;
}