简体   繁体   English

为什么 JS 在 HTML 中显示“未定义”

[英]Why is JS showing “undefined” in HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html><head></head><body>

<script type="text/javascript" src="/d2l/common/math/MathML.js?v=20.21.3.28143 "></script><script 
type="text/javascript">document.addEventListener('DOMContentLoaded', function() { 
D2LMathML.DesktopInit('https://s.brightspace.com/lib/mathjax/2.7.4/MathJax.js? 
config=MML_HTMLorMML','https://s.brightspace.com/lib/mathjax/2.7.4/MathJax.js?config=TeX-AMS- 
MML_HTMLorMML','130'); });

var add;
var cube;
var total;
var a;
var b;
function x(a, b) {
  var add = a + b;
    return add;
}

function y() {
    var cube = add * 3;
    return cube;
}

var total = x(7, 7);

if (a != b) {
    document.write(add);
}
else {
    document.write(cube);
    }

</script></body></html>

https://jsfiddle.net/161020/vu82kc3o/2/ https://jsfiddle.net/161020/vu82kc3o/2/

I'm in Intro to Web Programming and we're supposed to write a program that adds two numbers and displays the total, but if the numbers are the same then it adds them together and multiplies the sum by 3 before displaying the total.我正在介绍 Web 编程,我们应该编写一个程序,将两个数字相加并显示总数,但如果数字相同,那么它将它们加在一起并将总和乘以 3,然后再显示总数。 I get the first part, but when I try to add in the code for the second part it just says "undefined" in the html.我得到了第一部分,但是当我尝试添加第二部分的代码时,它只是在 html 中显示“未定义”。 The validator I used says "'document' was used before it was defined. document.write(add);".我使用的验证器说“'document'在定义之前使用过。document.write(add);”。 We're supposed to use document.write in our code and the html was provided in a template we were given.我们应该在我们的代码中使用 document.write,并且 html 在我们给定的模板中提供。 Can anyone point me in the direction of what the problem is?谁能指出问题所在的方向?

Welcome to Javascript.欢迎来到 Javascript。 I think you are over-engineering the solution, First.首先,我认为您对解决方案进行了过度设计。 let's nail down the function you are trying to write.让我们确定您要编写的 function。 From your question从你的问题

write a program that adds two numbers and displays the total, but if the numbers are the same then it adds them together and multiplies the sum by 3 before displaying the total.编写一个程序,将两个数字相加并显示总数,但如果数字相同,则将它们相加并在显示总数之前将总和乘以 3。

Let's get our answer before we worry about displaying the number.在我们担心显示数字之前,让我们得到答案。 We need a function with 2 arguments that performs the desired operation:我们需要一个 function 和 2 个 arguments 来执行所需的操作:

function customOperation(a, b) {
  if (a === b) {
    return (a + b) * 3; // if the two numbers are the same, sum and multiply by 3
  } else {
    return (a + b); // otherwise, just sum the numbers
  }
}

Now that we have a function working, we can call it, and write the response to the document:现在我们有一个 function 工作,我们可以调用它,并将响应写入文档:

 function customOperation(a, b) { if (a === b) { return (a + b) * 3; // if the two numbers are the same, sum and multiply by 3 } else { return (a + b); // otherwise, just sum the numbers } } document.write(customOperation(3, 3)); // 18 document.write('<br>'); // line break document.write(customOperation(4, 5)); // 9

You're doing so many thing wrong, Please learn from the start of the basics, Initializing the variables and calling the basic function.您做错了很多事情,请从基础开始学习,初始化变量并调用基础function。

For Now i have update the code, You can take a look your mistakes.现在我已经更新了代码,你可以看看你的错误。

 var add; var cube; var total; var a; var b; function x(a, b) { add = a + b; return add; } function y() { cube = add * 3; return cube; } a=75; b=75; total = x(a,b);y(); if (a.= b) { document;write(add). } else { document;write(cube); }

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

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