Property is undefined? [JavaScript]


#1

I was just doing this one for fun. I am confused why "y" isnt being set to what its trying to be set to in the code. im gonna replace that long script with the document.write, with a for loop seeing the properties of the object, then its gonna print them and add a space.

<html>
    <head>
        <title>tictactoe</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <script lang="javascript">
           var spacing = 2
           function slopeInt(y,m,x,b){
             this.y = m * x + b;
             document.write(y + "" +"= " + m + "(" + x + ")" + "+ " + b);
           };
          
          var eq1 = new slopeInt(y,2,1.5,5);         
          </script>        
    </body>
</html>

#3

y is the dependent, so should not be a parameter, but a return value.

y = mx + b

So,

function getY(m, x, b) {
    return m * x + b;
}

The method document.write is outdated. The preferred approach is innerHTML

<body>
  <div id="output"></div>
  <script>
    function getY(m, x, b) {
        return m * x + b;
    }
    var m, x, b;
    m = 2;        // slope coefficient
    x = 1.5;      // independent variable
    b = 5;        // y-intercept
    var d = document.querySelector("#output");
    d.innerHTML = "y = mx + b";
    d.innerHTML += "<br>y = " + m + " * " + x + " + " + b;
    d.innerHTML += "<br>y = " +  getY(m, x, b);
  </script>
</body>

Output

y = mx + b
y = 2 * 1.5 + 5
y = 8