Matiny
Matiny

Reputation: 179

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

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);

Upvotes: 2

Views: 111

Answers (4)

guest271314
guest271314

Reputation: 1

Try utilizing String.prototype.split() , Array.prototype.map() , Array.prototype.join()

function squareDigits(num) {
  return String(num).split("")
    .map(function(n) {
      return Math.pow(n, 2);
    }).join("");
}
console.log(squareDigits(123));

Upvotes: 3

Vasil Indzhev
Vasil Indzhev

Reputation: 695

What about this?

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

    //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);
        digits += '' + newnum

    }
    console.log(digits);
}

Upvotes: 1

Tushar
Tushar

Reputation: 87203

  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);

Upvotes: 5

Evan Knowles
Evan Knowles

Reputation: 7511

try process.stdout.write, as in

process.stdout.write(newnum);

Upvotes: -1

Related Questions