Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Arrays Multidimensional Arrays Build a Quiz Challenge

BUILDING A TWO DIMENSIONAL ARRAY QUIZ - How to Carriage Return

On the "Build-a-quiz-challenge", which I am taking to heart and which to put in my portfolio: I cannot find a way to "carriage return" the data.

SetUP:

  1. The question is answered here, 10 such, in Array_#1

a through g. The answers for a given question.

The FOR loop "i" is to run through the questions, carriage return, then FOR loop "j" is to run through the answers.

The Output to the HTML page should be:

1. a. b. c.

etc up to g for each of the remaining 2-9 questions.

All I am seeing is the "g" to question 10.

I think what I need is beyond the scope of what I have been taught so far.

================================================================================= I have been asked to past the code. I warn that it is very verbose.

SUBJECT MATTER: Questions and Answers are pertaining to a board game entitled "Axis & Allies: 1940" which depicts a 1940s era global map and a player manages his money to purchase military assets in an attempt to conquer the opposing player.

/* THE FOLLOWING IS THE QUIZ REQUIREMENT #1 → → → 1. Create a multidimensional array to hold quiz questions and answers



PROGRAM BY MICHAEL P. DUNAGAN, DATED JULY 27TH, 2020

A QUIZ THAT ASKS 10-QUESTIONS AND 7-ANSWERS PER QUESTIONS

LOCAL VARIABLE "i" RUNS THE QUESTIONS IN A "FOR" LOOP LOCAL VARIABLE "j" IS HOPED TO RUN THE ANSWERS IN A "FOR" LOOP INSIDE THE "i" FOR LOOP

I am attempting to solve this issue without having to watch the solution video.


***********************************************************/

const Axis_Allies =

['1. An Axis player of players may exactly just win with no excess room to spare by the following way(s)?',

'2. When playing the "Asian" Campaign, in other words, not the Global Campaign, which of the following is true regarding entry in to Russian Territories to include Mongolia? ',

'3. The Italian phrase: "Mare Nostrum", for "Out Sea", is a political situation where Italy may gain IPCs in National Objective Points if she achieves the following certain feats?',

'4. For ANZAC, control of which combinations of territories is ANAC entitled to 5 IPCs in \ National Objectives?',

'5. For which territories on the European Map must Great Britain hold in order to receive IPCs in \ National Objectivives?',

'6. `Which "combined arms" may allow a the more inferior attacking piece to be promoted one attacking value number higher regarding qualify for a hit with a dice roll?',

'7. When playing against Sean, which is the most prudent purchase for the Russian Player?',

'8. When may a scrambled Fighter retreat from an ongoing battle?',

'9. Which of the following are listed as National Objectives for the Soviet Union?',

'10. Which of the following situations is allowed according to the rules?' ];

const answers_1 =

[ 'a. Controlling at least six victory cities in either European or Asian map board', 'b. Controlling at least eight victory cities in the Asian map board', 'c. Controlling at least eight victory cities on the European map board', 'd. Fahgehtabout it! The Allies always win!', 'e. Controlling at least six victory cities on the Asian map board', 'f. The answer appears more than once in a through e above.', 'g. The answer does not appear in a through e above.'];

const answers_2 =

[ 'a. Entry through Russian or Mongolian territories is prohibited by Japanese units.',
  'b. Entry through Russian or Mongolian territories is prohibited by ANY units.',
  'c  Entry is allowed to the Japanese units but the price to pay is that "all Mongolian territories that are still strict neutral, or pro-Allies, or have joined the Allies as a result of a failed Japanese attack", are placed under control of the Allied player at the end of Japan\'s conduct combat phase.',
  'd. Entry into Russia or Mongolia is an automatic forfeit by the Japan player.',
  'e. Entry into Mongolia is allowed for the Chinese units.',
  'f. The answer appears more than once in a through e above.',
  'g. The answer does not appear in a through e above.' ];

const answers_3 =

 [ 'a. Not having Great Britain bomb and torpedo it on turn #1 so that there are Italian Units left remaining.' ,
  'b. 2 IPCs per territory if Italy controls Iraq, Peria, and/or Northwest Persia.' ,
  'c. 3 IPCs if Italy controls the Caucasus.' ,
  'd. 5 IPCs if there are no Allied ships in the Mediterranean.' ,
  'e. 5 IPCs if Germany controls the territories Gibraltar, Southern France, and Egypt.' ,
  'f. The answer appears more than once in a through e above.' ,
  'g. The answer does not appear in a through e above.' ];

const answers_4 =

[ 'a. Malaya.',
 'b. Quangtung' ,
 'c. Sea Zone 37.' ,
 'c. Dutch New Guinea, New Guinea, New Britain, and Salomon Islands.' ,
 'd. India.',
 'e. Sumatra.' ,
 'f. The answer appears more than once in a through e above.',
 'g. The answer does not appear in a through e above.' ];

const answers_5 = [ 'a. Every original territory it controls at the start of the game.' , 'b. Gibraltar.' , 'c. It is not possible for Great Britain to obtain National Objectives on the European board.' , 'd. Tunisia.' , 'e. Alexandria.' , 'f. The answer appears more than once in a through e above.' , 'g. The answer does not appear in a through e above.' ];

const answers_6 =

[ 'a. A Battleship and a Cruiser.' ,
  'b. An Armor and a Fighter.' ,
  'c. An Infantry and an Armor.' ,
  'd. An Infantry and an Artillery.' ,
  'e. A Destroyer and a Strategic Bomber.',
  'f. The answer appears more than once in a through e above.',
  'g. The answer does not appear in a through e above.' ];

const answers_7 =

[ 'a. A Battleship and a Cruiser!  By no way in hell is he going to get control of the Caucus Sea Zone!' ,
  'b. Two Strategic Bombers.  Gotta carpet bomb Berlin.' ,
  'c. Save all the IPCs and with a Frugal-Stingy Award from the website I used to play A & A 1942' ,
  'd. If Germany puts no money into the water, Twelve Infantry and One Artillery. May adjust from here.',
  'e. If Germany puts more than a submarine in the water, one Armor and one Mechanical Infantry for each 6 IPCs \beyond 6 IPCs fractions rounded up put in Germany puts in the water; i.e., you are playing Mike.' ,
  'f. The answer appears more than once in a through e above.' ,
  'g. The answer does not appear in a through e above.' ];

const answers_8 =

[ 'a. After the first round.' ,
  'b. Fighters are not allowed to scramble!',
  'c. Fighters are prohibited from withdrawing from a battle to which they scrambled; Fighters only survive if they have not been hit when the battle ceases.' ,
  'd. After all other aircraft has withdrawn' ,
  'e. Fighters are prohibited from withdrawing from a battle to which they scrambled; Fighters only survive if they have not been hit when the battle ceases.' 
  'f. The answer appears more than once in a through e above.' ,
  'g. The answer does not appear in a through e above.' ];

const answers_9 =

[ 'a. Control of all Germany Territories.' ,
  'b. Control of any German Territory.' ,
  'c. Control of Archangel and Sea Zone 125.' ,
  'd. Control of Caucus.' ,
  'e. Control of Novgorod.' ,
  'f. The answer appears more than once in a through e above.' ,
  'g. The answer does not appear in a through e above.' ];

const answers_10 =

[ 'a. An air-base is put-out of action after six hits.' ,
  'b. When a naval-base is put out of action, ships emanating from said base lose only the get the   "extra range" the sea-base rules give to ships.' ,
  'c. An air-base which is put-out of action is prohibits plane from landing there.',
  'd. Air and Naval bases can take up to ten hits before the eleventh or more hits do not matter.',
  'f. The answer appears more than once in a through e above.',
  'g. The answer does not appear in a through e above.' ]; 



for (let i=0; i < Axis_Allies.length; i++)  {
    document.getElementById('questions').innerHTML = `${Axis_Allies[i]}`;

      for (let j=1; j <= 6; j++)  {
          document.getElementById('answers').innerHTML = `<br><p>${answers_[i]}</p>`;
      }
}


// I need a way to 1) carriage return, and then 2) to pause and wait for a response from the quiz taker

On the HTML file, I added the id's for questions and answers.

Hi. I'd be glad to help. Would you mind posting your code so I can take a look?

1 Answer

Steven Parker
Steven Parker
231,248 Points

A normal assignment (=) replaces the contents of the target. To add onto existing content, you need a concatenating assignment (+=) instead.

And you don't need the "<br>" because a paragraph ("<p>") is a block element with margins.

But the biggest issue is that you haven't defined an array named "answers_" but have several separate arrays with names like "answers_1". It's possible to access globals by name using the "window" object:

          let answers_ = window["answers_" + i];  // "i" for the question, but "j" for the answer
          document.getElementById('answers').innerHTML += `<p>${answers_[j]}</p>`;

However, this is a rather obscure trick and a more conventional approach might be to use a 2-dimensional array for the answers. The title of this course stage indicates that is probably what is expected.

Thank you three times again Mr. Parker.

Ya—taking a break attempt to get my car to pass Northeaster Illinois Emmissions Standards, I was thinking that maybe I should print the "possible answers" along with the question: i.e., put them in the first array.

Other parts of the quiz directs me to get the answer, see if it is correct, and the appropriate noticing of such to the user. That is beyond the scope of this forum posting however.

=============================

What is in my mind, and if I could use Visual Basic for Application I would know the syntax to "get 'er dune".

What I want, if I were to write Stage Directions, is:

  1. Print the question, then say, a three second delay, slowly list the possible choices.
  2. Grab the letter choice made by the user
  3. Notice the user of his error or correct choosing.

1 is where I am stuck. I will attempt you suggestions after the sun goes down. I have to get my license plates before this month expires, and I cannot get my plates until I see where the leak is in my car's emissions system.

MPD

Steven Parker
Steven Parker
231,248 Points

Animation effects (delays, slow printing) are a bit outside the scope of this course. But you might look into 3rd-party libraries for doing such things.

The very useful "jQuery" library has several animation functions that could do this.