looking for some help with- JSON.stringify

I have a student trying to use a server script to create a list of ingredients. She would like to have the ingredients listed above the instructions but for the life of us we have not been able to figure out how to get a line brake to work.

Can anyone show us how to get the greetingString to display with line brakes?

Thank you

code below

// get ingredients from service, name is entered in the app var flour = request.get("flour"); var baking_soda = request.get("baking_soda"); var salt = request.get("salt"); var lemon_zest = request.get("lemon_zest"); var unsalted_butter = request.get("unsalted_butter"); var sugar = request.get("sugar"); var egg = request.get("egg"); var vanilla = request.get("vanilla"); var lemon_juice = request.get("lemon_juice");

// put together the greeting string var greetingString = "2 cups of flour, 1/2 teaspoon of baking soda, 1/2 teaspoon of salt, 1 table spoon of lemon zest, 1 stick of butter, 1 cup of sugar, 1 egg, 1 teaspoon of vanilla extract. Preheat oven to 350 degrees. In a medium bowl, whisk together " + flour + ", " + baking_soda +", " + salt +", " + lemon_zest+ "In a large bowl, using an electric mixer, beat butter and granulated sugar until light and fluffy. Add " + egg + ", " + vanilla +", " + lemon_juice + "and beat until combined. With mixer on low, beat in flour mixture. Drop dough by heaping tablespoons, 1 inch apart, onto two baking sheets. Bake until edges are golden, 15 to 20 minutes. Let cool 2 minutes on sheets, then transfer cookies to a wire rack to cool completely. Spread cookies with lemon glaze;)"

// format as JSON and return the response response.success(JSON.stringify({ greeting: greetingString

}), "application/json");

Answers

  • Hello -

    KPR Text objects allow you to display multi-line text strings. You can use '\n' characters in the string to force hard line breaks:

    var string = "hello" + "\n" + "world";
    

    A Text object would display this string as "hello" on the first line and "world" on a second line. Does this answer your question?

    Regards, Brian

  • When I added the \n to the line it treated it a text and just added it. this was part of the line of code:

    code: var greetingString = "2 cups of flour" + "\n" + "1/2 teaspoon of baking soda.. Results were: {"greeting":"2 cups of flour\n1/2 teaspoon of baking soda

  • Hello -

    The greetingString result looks correct. The line break is included in the "greeting" property.

    I'm afraid I don't follow what specifically you are trying to do. To display multi-line text in a Kinoma app, you'd use a Text container:

    var greetingString = "hello\nworld";
    var item = {greeting: greetingString};
    application.add(new Text({ left: 0, right: 0, top: 0, bottom: 0, string: "'item.greeting'"}));
    

    JSON.stringify() doesn't display text - it is used to convert a JavaScript value into a string:

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

    Regards, Brian

Sign In or Register to comment.