Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Storing an object to a variable

I am participating in the full stack javascript techdegree and I am stuck on how to do the following: create a function that calls another function and stores the returned object in a variable. This is what I have so far:

function printQuote(){ var random = getRandomQuote(); var output = "<p class='quote'>"+ random + "</p>"; output += "<p class='source'>"+ random + "</p>"; }

I don't think that I am even close. Please help me

Simon Coates
Simon Coates
28,694 Points

If this is in relation to a challenge, can you provide a URL? it will help people help you.

r h
r h
68,552 Points
function testFunction() {
  function secondFunction () {
    var object = {
       field1: "data1",
       field2: "data2",
    }
    return object;
  }
  var objectReturned = secondFunction();
  return objectReturned;
}

var final = testFunction();

This is calling a function inside a function to return the object returned by the function inside the function; One problem with your code is that you're not returning anything; you need to decide what information you want to return (in your example, most likely output) and return it at the end of the function.

Please format your code

1 Answer

Here's a bare-minimal code fragment that does what's the exercise asks for:

function f1() {
    return { name: 'Hello, World!' };
}

var v = f1();
console.log(v);