0

Write the JavaScript to prompt the user to enter an odd number. First, check that the user has entered a valid odd number. Then you need to print to the console a cross made of the same number of * characters as the number the user entered.

So far I have my code as such

I've edited my code to include the proper way

let num = prompt("Enter an odd number");
let star = "*";
let vertical = "";
let horizontal = star.repeat(num);

while (num%2 == 0) {
    console.log("User did not enter an odd number");
    num = prompt("Enter an odd number");
}
console.log("User's number is " +num);
let pos = +(Math.ceil(num/2));
// I need to divide the input number by 2 and round up
for (var h = 0; h < pos; h++){
    vertical += (" ")

}

for (var v = 0; v < pos; v++){
    vertical += vertical + "\n *"
}
vertical += "*"
console.log (vertical) 

I can get vertical to \n the proper amount of times but how would I go about making sure I don't over repeat the *?

2
  • 1
    Why not just say in the beginning let vertical = "" and then after the loop write vertical += "*"? Commented Oct 8, 2017 at 3:20
  • Yup, you are absolutely right. That was a very simple solution thank you for bringing it up. Now any chance you would have an idea as to how I could repeat that vertically for num or pos amount of time? I tried using \n to repeat new line it but it doesn't quite work Commented Oct 8, 2017 at 3:27

2 Answers 2

1

You're on the right track. There are three steps that you need to do here. You need to create a cross like this (assuming number is 13):

   *
   *
   *
*******
   *
   *
   *

You need to create Math.floor(13/4) (comes out to 3) rows of a single star, with each star after Math.floor(13/4) spaces. Then you need a row of Math.ceil(13/2) stars starting at position 0. Lastly, you need the same number of stars as you did before that row, so you can just do the same thing again.

Using your code, I would do something like this:

let num = prompt("Enter an odd number");

while (num%2 == 0) {
    console.log("User did not enter an odd number");
    num = prompt("Enter an odd number");
}
console.log("User's number is " +num);

let star = " ";
let pos = Math.floor(num/4);
let vertical = star.repeat(pos) + "*";

for (var i = 0; i < pos; i++) {
    console.log(vertical);
}

console.log("*".repeat(Math.ceil(num/2)));

for (var i = 0; i < pos; i++) {
    console.log(vertical);
}
Sign up to request clarification or add additional context in comments.

2 Comments

That's definitely closer to what I need. Although, I changed the math parts a little to be math.floor(num/2) and just .repeat(num) because I want the full amount the user input as * ; is there a reason console isn't showing the complete new line with the *? it just shows the number (6) of how many times it repeats instead of showing all the *
That's just how certain consoles work. You might be able to change the settings on your browser. It does it to preserve space. Another possibility is to convert the entire cross into one large string that has multiple new line characters in it
1

Initially you are assigning

let vertical = "*";

change it to

let vertical = "";

Try this

let num = prompt("Enter an odd number");
let star = "*";
let vertical = "";
let horizontal = star.repeat(num)

while (num%2 == 0) {
console.log("User did not enter an odd number");
num = prompt("Enter an odd number");
}
console.log("User's number is " +num);
let pos = +(Math.ceil(num/2));
// I need to divide the input number by 2 and round up
for (var h = 0; h < pos; h++){
vertical += (" ")

}
console.log(vertical)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.