javascript printing to previous line

I have a bit of code that requires printing underscores but to the line above it, how would i do this? I’m not sure how to print the underscore to the previous line, not much experience with javascript. thanks!

var landscape = function() {
  var result = "";
  var flat = function(size) {
    for (var count = 0; count < size; count++)
      result +=  "_";
  };
  var hill = function(size) {
    result += " /";
    for (var count = 0; count < size; count++)
      result += ""+
                "_";
    result += " \";
  };


  //BUILD SCRIPT
  flat(3)
  hill(4);
  flat(6);
  hill(1);
  flat(1);
  //END SCRIPT
  return result;

it makes this ___ /____ ______ /_ _`enter code here`enter code here`
and i want this
    _____    ___
___/     __/   ____/_

Answer

You can keep track of the two lines separately and then concatenate them just before returning the result.

JS:

function landscape() {
    var resultTop = '';
    var resultBottom = '';

    function hill(size) {
        resultTop += ' ';
        resultBottom += '/';
        for (var i = 0; i < size; i++) {
            resultTop += '_';
            resultBottom += ' ';
        }
        resultTop += ' ';
        resultBottom += '\';
    }

    function flat(size) {
        for (var i = 0; i < size; i++) {
            resultTop += ' ';
            resultBottom += '_';
        }
    }

    flat(3);
    hill(4);
    flat(6);
    hill(1);
    flat(1);

    var result = resultTop + '<br/>' + resultBottom;
    return result;
}

Here’s a fiddle.

Leave a Reply

Your email address will not be published. Required fields are marked *