var result = ""; for (var i = 1; i <= 8; i++) result += "#_".repeat(4).split("")[i & 1 ? "slice" : "reverse"]().join("") + "\n"; alert(result);