2

I am learning JavaScript and I am trying to print this particular sequence of hashes:

#
##
###

I wrote this piece of code to produce the sequence:

for(let i = 0; i < 3; i++){
    for(let j = 0; j <= i; j++){
        print('#');
    }
    print('\n');
}

Note: I am using rhino to execute the program on my Ubuntu terminal.

rhino <filename>.js              //Command used to execute JavaScript file.

The output I obtain:

#


#
#


#
#
#

I obtain the correct output with this program:

var starString = "";
for(let i = 0; i < 3; i++){
    for(let j = 0; j <= i; j++){
        starString += '#';
    }
    print(starString);
    starString = "";
}

My question is this: Is there a print statement which I can use that does not add a newline at the end of the statement?

V.I.L
  • 19
  • 4

2 Answers2

0

No, not directly using print(). However you can make your own function using System.out.print() that prints without the new lines:

function printNOn(arg) {
     java.lang.System.out.print(arg)
}

result:

MBP:rhino1_7R4 mark$ java -jar js.jar
Rhino 1.7 release 4 2012 06 18
js> function printNOn(arg) {
     java.lang.System.out.print(arg)
} 
js> for(let i = 0; i < 3; i++){
  >     for(let j = 0; j <= i; j++){
  >         printNOn("#")
  >     }
  >     printNOn('\n')
  > }
#
##
###
js>
Mark
  • 90,562
  • 7
  • 108
  • 148
0
console.log('Node js - hello world');
for(var counter = 0; counter< 5 ;counter++){
    // console.log("----------------");
    var pound = "#";
    for(var localcounter = 0; localcounter < counter; localcounter++ ){

      //console.log("localcounter is ", localcounter);
       //console.log("counter is ", counter);
      //console.log(pound);
      pound += "#";
      //console.log(pound);
    }
    console.log(pound);
    //console.log("----------------");

    //
    pound = "";
    pound = null;
}