简体   繁体   中英

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/

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. 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. The validator I used says "'document' was used before it was defined. document.write(add);". We're supposed to use document.write in our code and the html was provided in a template we were given. Can anyone point me in the direction of what the problem is?

Welcome to Javascript. I think you are over-engineering the solution, First. let's nail down the function you are trying to write. 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.

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 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 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.

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); }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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