How can I make my output appear all on one line with no spaces?

Matiny

I have a simple Javascript problem that I'm working on, where the point is to...

  1. Take an input, like 123
  2. Separate the input as single digits, then square those single digits, thus getting 149.
  3. Display that "149" (in this case) as an output.

I don't know how to display it as 149. I can only show it as

1

4

9

Sure, I might try adding it to an array then for looping the results... something tells me that this is the slow solution, and that there is a faster one. Here's my code.

function squareDigits(num) {
    //Convert input to string
    num = num + "";
    var newnum;
    var len = num.length;

    //Split into digits, and square that result baby
    for (i = 0; i < len; i++) {
        var digit = num.substr(i, 1);
        newnum = Math.pow(digit, 2);
        console.log(newnum);
    }

}

squareDigits(123);
Tushar
  1. Create empty array outside of the loop
  2. Add squares of the each digit in the array
  3. Join the array after loop finishes

function squareDigits(num) {
  num = '' + num;
  var len = num.length;

  var squares = []; // Define empty array
  //Split into digits, and square that result baby
  for (i = 0; i < len; i++) {
    var digit = num.substr(i, 1);
    squares.push(Math.pow(digit, 2)); // Push the square of the digit at the end of array
  }

  return squares.join(''); // Join the array elements with empty string as glue
}

var squares = squareDigits(123);
console.log(squares);
document.write(squares);


By string concatenation

  1. Declare a empty string before the for loop
  2. Concatenate the square of the digit to the string by first casting the number to string

function squareDigits(num) {
  //Convert input to string
  num = num + "";
  var newnum = ''; // Decalare variable with Empty string
  var len = num.length;

  //Split into digits, and square that result baby
  for (i = 0; i < len; i++) {
    var digit = num.substr(i, 1);
    newnum += '' + Math.pow(digit, 2); // Cast the square to string and then concatenate to the string
  }

  return newnum; // Return the string
}

var squares = squareDigits(123);
document.write(squares);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How can I make my ngx-line-chart responsive?

From Dev

How can I make my app appear in UIActivityViewController for text?

From Dev

How can I make a list of all dataframes that are in my global environment?

From Dev

How can I make one procedure and utilize it in all my view controllers?

From Dev

How can i make my app appear on tablets

From Dev

How do I make existing tabs appear as 4 spaces in vim?

From Dev

How can I make my navbar appear on every page in my rails app?

From Dev

How can I remove the extra spaces between my output?

From Dev

How can I make a prototype function to all my object properties?

From Dev

Can I make Python output exceptions in one line / via logging?

From Dev

How can I display my javascript inside my <p> tags to make it a one line sentence?

From Dev

How can I print all content of an array in one line?

From Dev

How do i get my repeater results to appear line by line

From Dev

How can i make my functions stops executing at a certain line?

From Dev

How can I make my OS appear as if it is running virtualized?

From Dev

How can I make `file` output line break type and encoding for all file types?

From Dev

How do I write a swift array that can make the same UIImage appear for all my dynamic cells?

From Dev

How can I get all of my GMail directories to appear in Thunderbird?

From Dev

How can I make all my processes start with niceness 5

From Dev

How do I make HTML Elements appear on one line?

From Dev

How can I remove the extra spaces between my output?

From Dev

how can I make my numerous button activated functions into one function that handles all numbers?

From Dev

How can i make a button to clear one of my fields in a form?

From Dev

How can I make my JS script calls in Magento appear on one line?

From Dev

How can i make my jQuery code work on all elements

From Dev

How can I make sure my power line network is secure?

From Dev

How can I make my form responsive on all sizes

From Dev

How can I make this PowerShell script into one line

From Dev

How can I make my navbar appear at a certain section?

Related Related

  1. 1

    How can I make my ngx-line-chart responsive?

  2. 2

    How can I make my app appear in UIActivityViewController for text?

  3. 3

    How can I make a list of all dataframes that are in my global environment?

  4. 4

    How can I make one procedure and utilize it in all my view controllers?

  5. 5

    How can i make my app appear on tablets

  6. 6

    How do I make existing tabs appear as 4 spaces in vim?

  7. 7

    How can I make my navbar appear on every page in my rails app?

  8. 8

    How can I remove the extra spaces between my output?

  9. 9

    How can I make a prototype function to all my object properties?

  10. 10

    Can I make Python output exceptions in one line / via logging?

  11. 11

    How can I display my javascript inside my <p> tags to make it a one line sentence?

  12. 12

    How can I print all content of an array in one line?

  13. 13

    How do i get my repeater results to appear line by line

  14. 14

    How can i make my functions stops executing at a certain line?

  15. 15

    How can I make my OS appear as if it is running virtualized?

  16. 16

    How can I make `file` output line break type and encoding for all file types?

  17. 17

    How do I write a swift array that can make the same UIImage appear for all my dynamic cells?

  18. 18

    How can I get all of my GMail directories to appear in Thunderbird?

  19. 19

    How can I make all my processes start with niceness 5

  20. 20

    How do I make HTML Elements appear on one line?

  21. 21

    How can I remove the extra spaces between my output?

  22. 22

    how can I make my numerous button activated functions into one function that handles all numbers?

  23. 23

    How can i make a button to clear one of my fields in a form?

  24. 24

    How can I make my JS script calls in Magento appear on one line?

  25. 25

    How can i make my jQuery code work on all elements

  26. 26

    How can I make sure my power line network is secure?

  27. 27

    How can I make my form responsive on all sizes

  28. 28

    How can I make this PowerShell script into one line

  29. 29

    How can I make my navbar appear at a certain section?

HotTag

Archive