简体   繁体   English

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

[英]JS not displaying properly when opening HTML file

When I try to open the HTML file in a browser, none of my code from my JS file appears.当我尝试在浏览器中打开 HTML 文件时,我的 JS 文件中没有任何代码出现。 I'm still new to JS, so I'm not sure if I'm missing simple syntax, but I've run it through a validator and it seems to be error free.我还是 JS 的新手,所以我不确定我是否缺少简单的语法,但我已经通过验证器运行了它,它似乎没有错误。 I'm not quite sure what could be causing this to happen and would appreciate any help!我不太确定是什么原因导致这种情况发生,希望得到任何帮助!

HTML File: HTML文件:

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

JS File: 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"));

Open the browser console, if you are in Chrome, shift+ctrl+j on Linux and see if you are correctly loading your script in the page or any other error.打开浏览器控制台,如果您使用的是 Chrome,请在 Linux 上使用shift+ctrl+j ,看看您是否在页面中正确加载了您的脚本或任何其他错误。

As I can see you didn't write well parenthesis, replace your function with this:正如我所看到的,你没有写好括号,用这个替换你的函数:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM