H
H
Halcod2017-02-28 21:13:20
JavaScript
Halcod, 2017-02-28 21:13:20

I understand the order of the cycle?

Hello, I'm studying.
I can't understand why in the mountain function, the loop creates four quotes first, then a slash.
After all, the instructions must be followed in order. That is, for me, it should be a quote-slash-quote-slash.
Maybe I missed something about cycles?
ps. Book "Expressive Javascript".

var landscape = function() {
  var result = "";
  var flat = function(size) {
    for (var count = 0; count < size; count++)
      result += "_";
  };
  var mountain = function(size) {
    result += "/";
    for (var count = 0; count < size; count++)
      result += "'";
    result += "\\";
  };
  flat(3);
  mountain(4);
  flat(6);
  mountain(1);
  flat(1);
  return result;
};
console.log(landscape());
// → ___/''''\______/'\_
// почему не так ? ___/'\'\'\_____/'\_

Answer the question

In order to leave comments, you need to log in

2 answer(s)
H
holymotion, 2017-02-28
@holymotion

Because in the mountain function += "\\" must be in a loop, which means you must wrap both the first and second actions in curly braces, then everything will work. And you have strings from "'" concatenated 4 times in a row, and then a slash is added to the end of the string.

var landscape = function() {
  var result = "";
  var flat = function(size) {
    for (var count = 0; count < size; count++)
      result += "_";
  };
  var mountain = function(size) {
    result += "/";
    for (var count = 0; count < size; count++) {
      result += "'";
      result += "\\";
    }
  };
  flat(3);
  mountain(4);
  flat(6);
  mountain(1);
  flat(1);
  return result;
};
console.log(landscape());
// → ___/''''\______/'\_
// почему не так ? ___/'\'\'\_____/'\_

D
di23, 2017-02-28
@di23

And why did you decide that it should be so?? ___/'\'\'\_____/'\_
After all, only this line of the result += "'";
ADF is in the cycle: Roughly, what does mountain do?
Then he puts /the N-th number of quotes, because in the cycle is only adding them and nothing else. then closes it all with this. \
Here I don’t even know how to explain it easier. Read the code.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question