
Convert string value inputted in prompt to type number Var size = prompt("Enter a positive integer.")

Outside the scope of Chapter 2, there is a JavaScript String repeat() method. What is the update/final-expression in the inner for loop? However, this idea died a swift death upon the realisation that when a string value’s type is converted to type number, (“10” with 10), any arithmetic operation on this value will be governed by mathematical laws i.e. Using numbers would allow for repetition via arithmetic operations. It passed the mind to replace # and space with 1 and 0, and at the end, replace 1 and 0 with # and space again. Though that still isn’t helpful in repeating string characters horizontally when the number of repeats required is unknown. Keep in mind that the Number and String functions convert a value to type number and type string respectively. Observe string concatenation between string values and number values: //Example 1 I will not be able to repeat characters horizontally using arithmetic multiplication i.e. See the next blog post “Exercise 2.3 Part 2” for this solution.īut recall that no arithmetic operation can be applied to strings i.e. What is needed: Put console.log outside the for loop (so whatever is printed is the final result of repeated string concatenation). console.log("# " + "# ")ĮDIT (after writing Solution Attempt 4 – Final Submission) : You CAN repeat strings horizontally when the number of repeats is unknown. How do you repeat string characters horizontally when the number of repeats required is unknown?

When i is an odd number, the line starts with a space (will rep. – Repeated characters when i is an even number: #_ When i is an even number, the line starts with a # character Increases characters horizontally per line Let size represent the width and height of the square.Let i represent the number of iterations.When you have a program that generates this pattern, define a variable size = 8 and change the program so that it works for any size, outputting a grid of the given width and height. Passing this string to console.log should show something like this: The characters should form a chess board. Write a program that creates a string that represents an 8 x 8 grid, using newline characters to separate lines.Īt each position of the grid there is either a space or a “#” character.
