简体   繁体   English

console.log 中没有 output。 初学者问题

[英]No output in console.log. Beginner Question

I'm reading Head 1st JavaScript book and try to learn the language as much as i can.我正在阅读 Head 1st JavaScript 这本书,并尽可能多地学习这门语言。 Wanted to to all the problems from the book.想解决书中的所有问题。 After i did so for one of the problems, and wrote the code as i thought, and checked the solution, i changed it to reflect the solution in the book even thou it worked for me.在我针对其中一个问题这样做后,按照我的想法编写了代码,并检查了解决方案,我对其进行了更改以反映书中的解决方案,即使它对我有用。 The thing is, when i want to "print" in the console now, nothing shows up and idk why... i don't see nor have a problem... Any idea why the console.log will not output anything?问题是,当我现在想在控制台中“打印”时,什么也没有显示,我知道为什么……我没有看到也没有问题……知道为什么 console.log 不会 output 任何东西吗? Thanks!谢谢!

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script>
        function Coffee(roast, ounces) {
            this.roast = roast;
            this.ounces = ounces;
            this.getSize = function() {
            if (this.ounces === 8) {
                return "small";
            } else if (this.ounces === 12) {
                return "medium";
            } else (this.ounces === 16); {
                return "large";
            }
        };
            this.toString = function() {
                return "You have ordered a " + this.getSize() + " " + this.roast + " coffee.";
            };
        }
        var csmall = new Coffee ("House Blend",  "8");
        var cmedium = new Coffee ("House Blend", "12");
        var clarge = new Coffee ("Dark Roast", "16");
        var coffees = [csmall, cmedium, clarge];

        for (var i = 0; i < coffees.length; i++) {
            coffees[i].toString();
        }
    </script>
</head>
<body>
    
</body>
</html>

This is the way i wrote the code and worked.这就是我编写代码和工作的方式。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script>
        function Coffee(roast, size, ounces) {
            this.roast = roast;
            this.size = size;
            this.ounces = ounces;
            this.getSize = function() {
            if (this.ounces === 8) {
                console.log("You have ordered a " + this.size + " " + this.roast + " coffee.");
            } else if (this.ounces === 12) {
                console.log("You have ordered a " + this.size + " " + this.roast + " coffee.");
            } else (this.ounces === 16); {
                console.log("You have ordered a " + this.size + " " + this.roast + " coffee.");
            }
            };
        }
        var csmall = new Coffee ("House Blend", "small",  "8");
        var cmedium = new Coffee ("House Blend", "medium", "12");
        var clarge = new Coffee ("Dark Roast", "large", "16");
        var coffees = [csmall, cmedium, clarge];

        for (var i = 0; i < coffees.length; i++) {
            coffees[i].getSize();
        }
    </script>
</head>
<body>
    
</body>
</html>

In above code console log has not been used so this is why you haven't seen any thing in the console在上面的代码中,控制台日志没有被使用,所以这就是为什么你没有在控制台中看到任何东西的原因

for (var i = 0; i < coffees.length; i++) {
        coffees[i].toString();
        console.log(String(coffees[i])); // <-- add here
    }

Still it would give incorrect result becuase it used === in the function which means the type of the value supplied will also match with type in the function它仍然会给出不正确的结果,因为它在 function 中使用了 === 这意味着提供的值的类型也将与 function 中的类型匹配

It used integers in function but supplied value in string see the code below它在 function 中使用了整数,但在字符串中提供了值,请参见下面的代码

if (this.ounces === 8) { // used === to match type as well here it is integer
            return "small";
        } else if (this.ounces === 12) {
            return "medium";
        } else (this.ounces === 16); {
            return "large";
        }
    };
        this.toString = function() {
            return "You have ordered a " + this.getSize() + " " + this.roast + " coffee.";
        };
    }
    var csmall = new Coffee ("House Blend",  "8"); // passing it as string "8" it should be just 8
    var cmedium = new Coffee ("House Blend", "12");
    var clarge = new Coffee ("Dark Roast", "16");

Correct Code should be like this正确的代码应该是这样的

<script>
    function Coffee(roast, ounces) {
        this.roast = roast;
        this.ounces = ounces;
        this.getSize = function() {
        if (this.ounces === 8) {
            return "small";
        } else if (this.ounces === 12) {
            return "medium";
        } else (this.ounces === 16); {
            return "large";
        }
    };
        this.toString = function() {
            return "You have ordered a " + this.getSize() + " " + this.roast + " coffee.";
        };
    }
    var csmall = new Coffee ("House Blend",  8);
    var cmedium = new Coffee ("House Blend", 12);
    var clarge = new Coffee ("Dark Roast", 16);
    var coffees = [csmall, cmedium, clarge];

    for (var i = 0; i < coffees.length; i++) {
        coffees[i].toString();
        console.log(String(coffees[i]));
    }
</script>

But in the secon code which is done by you...但是在你完成的第二个代码中......

I beleive the real problem was to use logic and convert ounces into size by getsize function where as you directly used sizes in your solution我相信真正的问题是使用逻辑并通过 getsize function 将盎司转换为尺寸,因为您在解决方案中直接使用了尺寸

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

相关问题 如果在console.log 中调用了相应的函数,如何打印标签。 现在它不打印标签和结果? - How can I print label if the corresponding function is called in the console.log. Now its not printing the label and result? 尽管在console.log中显示了它的存在性,但仍然无法访问对象数据成员。 尝试的访问返回未定义 - Unable to access objects datamember, despite showing its exsistence in console.log. Attempted access returns undefined NestJS:无法在 console.log 中获得嵌套的 object。 错误:类型 Dto 上不存在属性 x - NestJS: Cannot get nested object in console.log. Error: Property x does not exist on type Dto Javascript 与 console.log 混淆 - 初学者 Q - Javascript confusion with console.log - Beginner Q 我正在尝试向我的反应组件发出 axiosGET 请求,我在 console.log 上获取对象。 但是当我尝试渲染它时,我得到一个“未定义” - i'm trying to make a axiosGET request to my react component, i get the object on the console.log. But when i try to render it i get a "is not defined" 输出console.log到html - Output console.log to html 在javascript中输出Console.log - Console.log output in javascript 有关Chrome和Firefox中的Console.log的问题 - Question regarding Console.log in Chrome and Firefox JS:输出控制台日志的内容 - JS: Output the content of the console log iWebInspector中的Console.log输出 - Console.log output in iWebInspector
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM