Web Analytics Made Easy -
StatCounter Help with my coding lab assignment! I did most of it. I just have some errors! - CodingForum

Announcement

Collapse
No announcement yet.

Help with my coding lab assignment! I did most of it. I just have some errors!

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Help with my coding lab assignment! I did most of it. I just have some errors!

    Here are the instructor's instructions:
    Prompt the user to enter three characters (this can be done with three prompts in a row)
    Store each character as a variable
    Prompt the user to enter a size
    Store the size as a number
    If the size cannot be converted to a number, prompt the user again for a valid number. Loop this prompt until a valid number is entered.
    Create a nested for loop that iterates through x and y up to the user-entered size
    Generate an interesting pattern (using the x and y values to make decisions) that places the three user-provided characters in a 2-dimensional grid
    Add the ‘<br>’ between each line
    Output the generated pattern to the page in the ‘myParagraph’ element, including a ‘<br>’ first so the piece starts on a new line

    Here is my code below:

    Code:
    <!doctype html>
    <html>
    <head><title>ASCII</title></head>
    <body>
    	<p id="myParagraph">
    </body>
    <script>
    	var question = 'What is the correct fibonacci sequence?';
    	var answers = ['0,2,2,4,8,14,20,40,60,180...','I don\'t know!','Who cares!','0,1,1,2,3,5,8,13,21,34...'];
    	var guess = prompt(question);
    	var score = 0;
    	for (var i = 0; i < answers.length; i++) {
    		if(guess === answers[i]) {
    			score += i;
    			console.log('User guessed answer: ' + i);
    		}
    	}
    	var characterArray = [];
    	for (var i = 0; i < 3; i++) {
    		characterArray[i] = prompt('Enter a character');
    		
    	}
    	console.log(characterArray);
    	
    	var userSize;
    	do {
    		userSize = prompt('Enter a size');
    	} while (!Number(userSize));
    	
    	var design = [];
    	for (var x = 0; x < userSize; x++) {
    		for (var y = 0; y < userSize; y++) {
    			for(var z = 0; z < userSize; z++) {
    			if ((x+y+z)%3 != 0) {
    				design += characterArray[0];
    			} else {
    				design += characterArray[1];
    			} else {
    				design += characterArray[2];
    			}
    		}
    		design += '<br>';
    	}
    		design += '<br>';
    	}
    	document.getElementById('myParagraph').innerHTML += '<br>' + design;
    </script>
    </html>
    Additional information: I know the code placement script should be somewhere else, this is just for learning and experimenting purposes.

  • #2
    I fixed my issue, but how do I make it use all three characters in the design?

    Code:
    <!doctype html>
    <html>
    <head><title>ASCII</title></head>
    <body>
    	<p id="myParagraph">
    </body>
    <script>
    	var question = 'What is the correct fibonacci sequence?';
    	var answers = ['0,2,2,4,8,14,20,40,60,180...','I don\'t know!','Who cares!','0,1,1,2,3,5,8,13,21,34...'];
    	var guess = prompt(question);
    	var score = 0;
    	for (var i = 0; i < answers.length; i++) {
    		if(guess === answers[i]) {
    			score += i;
    			console.log('User guessed answer: ' + i);
    		}
    	}
    	var characterArray = [];
    	for (var i = 0; i < 3; i++) {
    		characterArray[i] = prompt('Enter a character');
    		
    	}
    	console.log(characterArray);
    	
    	var userSize;
    	do {
    		userSize = prompt('Enter a size');
    	} while (!Number(userSize));
    	
    	var design = [];
    	for (var x = 0; x < userSize; x++) {
    		for (var y = 0; y < userSize; y++) {
    			for(var z = 0; z < userSize; z++) {
    			if ((x)%3 || (x)%2 != 0) {
    				design += characterArray[0];
    			} else if ((y)%3 || (y)%2 != 0) {
    				design += characterArray[1];
    			} else if ((z)%3 || (z)%2 != 0){
    				design += characterArray[2];
    			}
    		}
    		design += '<br>';
    		}
    		design += '<br>';
    		}
    	document.getElementById('myParagraph').innerHTML += '<br>' + design;
    </script>
    </html>

    Comment


    • #3
      Presumably you were given this for a JavaScript History class. Do you have a copy of Netscape 4 to test it in (that was the most resent browser to use prompt for that purpose - more recent browsers switched to using forms for input and used the dialogs for debugging until a proper debugger came along to make them completely obsolete.

      Anyway I seem to remember seeing a very similar question asked on the forum here before so presumably someone else has been taking the same history class.
      Stephen
      Learn Modern JavaScript - http://javascriptexample.net/
      Helping others to solve their computer problem at http://www.felgall.com/

      Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

      Comment


      • #4
        That was me. I asked that other time.

        Comment


        • #5
          So is there a particular reason you are taking a JavaScript history class to learn how JavaScript was written 20 years ago rather than taking a class that teaches how it is written now?
          Stephen
          Learn Modern JavaScript - http://javascriptexample.net/
          Helping others to solve their computer problem at http://www.felgall.com/

          Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

          Comment


          • #6
            I am unclear as to how your code runs at all.
            You have the script located after the closing body tag
            which from my understanding, does not make it part of the HTML at all.

            Comment


            • #7
              Just about everything in the code is invalid - that any of it works at all is only due to the forgiving nature of web browsers.
              Stephen
              Learn Modern JavaScript - http://javascriptexample.net/
              Helping others to solve their computer problem at http://www.felgall.com/

              Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

              Comment

              Working...
              X