Objectives:
Practicing using alert, console.log() and different loops to solve problems
Resources
http://www.w3schools.com/js/js_loop_while.asp
http://www.w3schools.com/js/js_loop_for.asp
Overview
The purpose of today’s assignment is to create a JavaScript to practice writing information to the console. This lab will also allow you to practice using while loops and for loops and conditions.
Create a new HTML file called a4.html. Add the usual tags to make a complete HTML file. In the head of this file add the following line of code:
In the same directory, create a new JavaScript file called a4.js
Inside a4.js complete the following exercises:
Write the JavaScript to prompt the user to enter a string and a letter. Then, using a loop, you should determine how many times the letter appears in the string (both uppercase and lower case.Ex. If the user enters the string “Mischief managed!” and the letter “m” your console window should contain the following:The letter m appears 2 times in Mischief managed!
Write the JavaScript that prompts the user to continuously enter numbers until a negative number is entered. Once a negative number has been entered, you should then print the average of all the entered numbers to the console. If the user does not enter any non-negative numbers (i.e., they only enter one number and it’s negative), you should alert the user with an error message.You must use a do while loop to solve this problem.Ex.If the user enters the numbers 7, 3, 5, 2, -1 you should print“The average is 4.25”Ex. If the user just enters -2 and no other numbers you should print“Error: No positive numbers entered”
Write the JavaScript to prompt the user to enter an integer. Then you need to create the nested for loop that prints to the console the pattern shown below, where the number of rows and columns in the pattern is equal to the number entered by the user. You may assume the user will only enter numbers between 0 and 99. If you’re feeling adventurous, do a check to ensure the user only enters a number between 0 and 99.Ex. If the user enters the number 5 you need to print the following pattern to the console:....1...2...3..
.4...
5....Whereas if the user had entered 3, you would print this:..1
.2.3..
Test that your program works by opening the HTML file in the browser and checking the output in the console.
Submissions
Submit to D2L ONE zip/archive file containing the TWO files from this lab
If you do not zip your files, you will receiveZEROfor this lab (http://www.wikihow.com/Zip-Files-Together)
GradingMarks will be assigned as follows:
1 mark for correctly creating the HTML file and linking to JS
4 marks for proper indentation and good code quality
4 marks for correctly implementing exercise A
5 marks for correctly implementing exercise B – 0 if missing the do while
6 marks for correctly implementing exercise C
Already registered? Login
Not Account? Sign up
Enter your email address to reset your password
Back to Login? Click here