-4

enter image description here

I am trying to create a wedding banquet table planner app for an assignment, but I am getting a NaN error for my "table" variable but not for my "guests" variable within my return statement. The picture is what I'm getting on my website, and below is script code. I am using HTML language with JavaScript. Thanks for any help!

function distribGuests() {

  // Read the table value inputed by the user
     var text = document.getElementById('tablesInputBox').value;

  // Convert what the user typed from text into a number.
     var t = (text);

  // Read the guests value inputed by the user
     var text = document.getElementById('guestsInputBox').value;

  // Convert what the user typed from text into a number.
     var g = (text);

  // Formula for variable "a", the rounded down value of guests
     var a = Math.floor (g / t);

  // Formula for variable "b", the rounded up value of guests
     var b = Math.round (g / t); 

  // Formula for variable "x", the end result for the first set of table distributions
     var x = (g - b * t) / (a - b);

  // Formula for variable "y", the end result for the second set of table distrubutions
     var y = (t - x); 

  // Use string concatenation to produce statement for end result of wedding table distribution
     var message = "There will be "+ x +" tables with "+ b +" guests and "+ y +" tables with "+ a +" guests";

  // Display the message in the div that has the outputDiv
     document.getElementById('outputMessage').innerHTML = message;
}
Nick Parsons
  • 45,728
  • 6
  • 46
  • 64

1 Answers1

0

Parentheses don't convert a string to a number.

  // Convert what the user typed from text into a number.
     var t = (text);

Should be:

  // Convert what the user typed from text into a number.
     var t = +text;

You can also use parseInt(text), but +text is effectively the same for your use case (it's called type coercion).

(For an analysis of the differences between the two methods, see this question)

Josh Beam
  • 19,292
  • 3
  • 45
  • 68