1

Cannot use value from previous object property function starting(); when trying to combine in output below; getting NaN for timeTaken(); function. Presumably starting isn't reporting within that function, any suggestions?

  starting : function() {
     return this.slow * this.driveTime + " mph ";
  },
  timeTaken : function() { 
     return this.starting / this.driveTime + " seconds ";
  },

document.getElementById("car").innerHTML = car.starting() +  car.timeTaken();

Should I rather try to nest both functions in same one function? I'm open to more optimal methods.


Full object code:

var car = {
  cruise: "35",
  slow: "5",
  stop: "0",
  driveTime: "4",
  starting : function() {
     return this.slow * this.driveTime + " mph ";
  },
  timeTaken : function() { 
       return this.starting / this.driveTime + " seconds ";
  },
 };
4
  • Calling only car.starting() working fine? Commented Mar 12, 2017 at 20:00
  • Yes, .starting() works fine. Commented Mar 12, 2017 at 20:00
  • 2 guesses: 1. Shouldn't you make a function call in return: "return this.starting() / ...." . and the second guess, you append the 'mph' which makes the return value of the starting() to string, and try to divide the string to another string. Commented Mar 12, 2017 at 20:02
  • I thought about that, and omitted extra text and still get NaN error. Commented Mar 12, 2017 at 20:04

3 Answers 3

6

That's because this.starting is a function. Basically what you are asking JavaScript to do is "take the starting function and divide it by this.driveTime". This is undefined.

What I'm guessing you want is something like this:

var car = {
  cruise: 35,
  slow: 5,
  stop: 0,
  driveTime: 4,
  starting: function() {
    return this.slow * this.driveTime;
  },
  timeTaken: function() {
    return this.starting() / this.driveTime;
  },
  toString: function() {
    return this.starting() + " mph " + this.timeTaken() + " seconds ";
  }
};

console.log(car.toString());
console.log(car.starting());
console.log(car.timeTaken());

Note that I changed starting() and timeTaken() to just return numbers (rather than "x mph" or "x seconds"). This allows you to do math on the results from those functions.

I added the toString() function to make it easier to get a string representation of the data that I'm assuming you wanted to print out.

EDIT: As noted by Peter Behr's answer, you should store cruise, slow stop, and driveTime as numbers rather than strings. I have updated my example to show this.


Further Reading

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! so, I have to do the math within another separate function basically?
@Cage, you could think of it like that. The most important thing to look at is what each function is returning. If you return something that is a number, as I am for starting and timeTaken, you can do math on its result. Whereas if you return a string, like the toString function, you can only do string operations with it.
2

Additionally, you're doing math on strings, which isn't a great idea. Make the object properties numbers to begin with.

Comments

0

starting method return a String so divide a string by a number will result as NaN

Comments

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.