0

I want to print the value that the variable quantity holds in HTML

Here's my JavaScript code

class Ingredient {
  constructor (name, dateBought, expiryDate, quantity, expired){
    this.name = name;
    this.dateBought = dateBought;
    this.expiryDate = expiryDate;
    this.quantity = quantity;
    this.expired = false;
  }
  isExpired(){
    this.expired = true;
  }
  needReorder(){
    console.log(this. name + "need to be reordered")
  }
}

let cabbage = ("cabbage", new Date(2003,3,2), 23)

I'm trying to display the quantity in an HTML file like this, so far nothing prints

<script src="inventory.js"> 
  
  var quantity = cabbage.quantity
  
</script>
<script>
    document.write(quantity)
</script>

I know that there isn't anything wrong with my HTML because this code works perfectly fine

<script>
   document.write("write something")
</script>

Thank you in advance :)

3
  • I updated the answer, please check. @RuijieLu Commented Oct 27, 2020 at 3:38
  • Please don't use document.write.... Commented Oct 27, 2020 at 3:40
  • You can not have external source and inline script in the same tag. Commented Oct 27, 2020 at 3:41

2 Answers 2

1
  1. You should use the variable cabbage.quantity in another script, not in the importing script.
<script src="inventory.js">   
</script>

<script>
    var quantity = cabbage.quantity
    document.write(quantity)
</script>
  1. Missing the new Ingredient with 4th argument.
let cabbage = new Ingredient("cabbage", new Date(2003,3,2), 23, 50)
Sign up to request clarification or add additional context in comments.

7 Comments

Can you explain? I tried document.getElementById("demo").innerHTML = quantity; Is this correct?
correct. since script is already imported in html file, you can use the variable as you want like that.
just the important notice is that variable should be used not in the first script (importing script), but in another script.
Thank you it worked. Why is the 4th argument needed? I want to have expired defaulted as false, really confused
if you don't define the 4th argument, it will be undefined.
|
1

You need to access the Ingredient class correctly. In your class declaration, quantity is expected to be the 4th argument, so you should pass that as follows:

new Ingredient("cabbage", new Date(2003,3,2), 23, 10)

Complete code:

class Ingredient {
  constructor (name, dateBought, expiryDate, quantity, expired){
    this.name = name;
    this.dateBought = dateBought;
    this.expiryDate = expiryDate;
    this.quantity = quantity;
    this.expired = false;
  }
  isExpired(){
    this.expired = true;
  }
  needReorder(){
    console.log(this. name + "need to be reordered")
  }
}

let cabbage = new Ingredient("cabbage", new Date(2003,3,2), 23, 10)
document.write(cabbage.quantity)

2 Comments

I can't see the difference
@RuijieLu If you notice carefully, there are differences there, it accesses the class by passing the quantity as the 4th argument.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.