0

This is my code for callback function in javascript:

callback(
        undefined,

        body.current.weather_descriptions[0] +
          "." +
          "It is currently " +
          body.current.temperature +
          " degrees. It feels like " +
          body.current.feelslike +
          " degrees. There is " +
          body.current.precip +
          "% chance of rain."
      );

My desired output is:

Varanasi, Uttar Pradesh, India

Partly cloudy.
It is currently 26 degrees. 
It feels like 26 degrees.
There is 0% chance of rain.

But my output coming is:

Varanasi, Uttar Pradesh, India

Partly cloudy.It is currently 26 degrees. It feels like 26 degrees. There is 0% chance of rain.

I tried many things but not getting desired output, can you help ?

old greg
  • 799
  • 8
  • 19
  • Does this answer your question? [How do I create a new line in Javascript?](https://stackoverflow.com/questions/5758161/how-do-i-create-a-new-line-in-javascript) – Gautham Dec 12 '20 at 14:03
  • Does this answer your question? [Creating multiline strings in JavaScript](https://stackoverflow.com/questions/805107/creating-multiline-strings-in-javascript) – FZs Dec 12 '20 at 16:23

4 Answers4

1

Ok to literally throw back at you the solved version of what you gave us..

callback(
    undefined,

    body.current.weather_descriptions[0] +
      ".\n" +
      "It is currently " +
      body.current.temperature +
      " degrees.\nIt feels like " +
      body.current.feelslike +
      " degrees.\nThere is " +
      body.current.precip +
      "% chance of rain."
  );
The Bomb Squad
  • 4,192
  • 1
  • 9
  • 17
0

On mobile, so excuse the lack of formatting.

Add in “\n\r” each place you want to add a new line. So something like

“It is currently: “ + your.call.data + “\n\rdegrees outside”.

MCA
  • 98
  • 6
0

If you need to display this in a browser, use "<br>" tag to places where you need the new lines:

callback(
        undefined,

        body.current.weather_descriptions[0] +"<br>"
          "." +
          "It is currently " +
          body.current.temperature +
          " degrees." +"<br>" "It feels like " +
          body.current.feelslike 
          " degrees." +"<br>"+ "There is " +
          body.current.precip +
          "% chance of rain."
      );

If you need to display this in a console, use "\n" to places where you need the new lines:

callback(
        undefined,

        body.current.weather_descriptions[0] +"\n"
          "." +
          "It is currently " +
          body.current.temperature +
          " degrees." +"\n" "It feels like " +
          body.current.feelslike 
          " degrees." +"\n"+ "There is " +
          body.current.precip +
          "% chance of rain."
      );
Harshana
  • 5,151
  • 1
  • 17
  • 27
0

You can insert literal newline character in a string with \n. For example:

const myString = "Hello,\nThat's a nice Tnettenba.";
console.log(myString);
Hello,
That's a nice Tnettenba.

Or, you can use backticks to create a string literal with newlines. For example:

const anotherString = `Hello,
That's a nice Tnettenba.`;

Will produce the same output as above.

Using strings delineated with backticks is helpful in this situation, as any whitespace (newlines, indents) are preserved, which is often desirable when formatting text for display. Eg.

const myTable = `
  | ID  | Name            |
  -------------------------
  | 001 | Hello, World!   |
  | 002 | Brave New World |
`;

Will output:


  | ID  | Name            |
  -------------------------
  | 001 | Hello, World!   |
  | 002 | Brave New World |

Note the leading spaces.

old greg
  • 799
  • 8
  • 19
  • It is inside the callback function and these values are returned by the function, so can't do this but I tried this too and it's not working. – Gaurav Singh Dec 12 '20 at 14:06
  • I'm not sure I follow, sorry. Could you share what you have attempted? You should be able to assign your string to a variable/constant before calling the callback, and passing in a reference to your string. – old greg Dec 12 '20 at 14:08
  • (error, { body } = {}) This is the callback function, it is getting stored as a body. – Gaurav Singh Dec 12 '20 at 14:18
  • 1
    I don't follow, sorry. You may need to share the whole implementation. – old greg Dec 12 '20 at 14:20
  • https://github.com/gauravmnnit/weather-website it is the github of whole project src/utils/forecast – Gaurav Singh Dec 12 '20 at 14:28
  • Instead of putting the string literal in the call to the callback function `callback(undefined, "my string")`, define the string constant first using backticks `const myString = \`foo\`;`, then pass a reference to it in to the callback `callback(undefined, myString)`. – old greg Dec 12 '20 at 14:41