Out: Jan 10, 2017
Due: Jan 17, 2017
CENG 256: Internet Scripting: Lab 1
For each of the following modify the file Lab1Template.html with your own examples to demostrate the various features of the JavaScript programming language. Place your code between <script id=Answer and </script> under the appropriate numbered comments. Have your code output its results to the console log. Because you are coming up with these examples on your own, you should be making different choices of names, data and functions to use.
Since Questions 5-7 employ a prompt, after you’ve developed them comment them out so that you aren’t bothered by the need to reply as you develop the rest of the answers.
If you are having problems with a particular feature document it, comment it out and move on. By all means ask questions and talk to each other, just don’t copy. If there is something you don’t understand I want to know about it – dcoument your problems. Each block of 5 questions are worth 10 marks. There are 30 marks in the assignment and an opportunity for 6 bonus marks. Be creative.
- Assign numeric values to 3 variables, do a calculation using 2 or 3 operators and 2 functions from the Math library. (2 marks)
- Show the effect of the < and > operators. (1)
- Create a demonstration of 4 different functions on strings. (4 marks)
- Demonstrate the difference between the == and the === operators. (1)
- Create an if statement that uses both relational and logical operators. It should also have an else clause. Use the prompt function call to prompt and suggest a value and test your function so that you test both the then clause and the else clause. (2)
- Write a switch statement with 2 cases and a default handler. Just before the switch, place a prompt that suggests to the user which case should be executed. (2)
- Rewrite your switch statement using if/else. (2)
- Create an array of at least 5 elements and display it in reverse order using a for loop (2)
Create an empty array and use a loop to build an array made up of the same elements in reverse order. (2) - Rewrite #8 using while loops (2)
- Define a dictionary (JavaScript Object Notation/JSON) with 4 entries based on one of the following suggested themes, or pick your own. Use a for/in loop to print out the key/value pairs to the console: (2)
- A list of books where the key is the title
- A list of cars where the key is the owner
- A list of courses where the key is the course code
- A phone book where the key is the phone number
- A list of grocery items where the key is a product code
- A list of animals in the zoo where the key is the animal’s name
- A list of students where the key is the student number
- The script northAmerica.js included by the template file contains country data extracted from the CIA world factbook ( The format is JSON. The script defines the following variables: bd (Bermuda), ca (Canada), gl (Greenland), np (Clipperton Atoll – a small uninhabited island owned by France), mx (Mexico), sb (San Pierre & Miquelon – a chain of 8 inhabited islands in the St. Lawrence river owned by France) and us (United States). In turn each of these variables are stored in the array myArray.
- Using one of these variables, print out 2 interesting items of data to the console other than the data used in Example C. (1)
- Reassign another of these items and print it out. (1)
- Add a new key/value pair using dot notation to one of these variables and print it out using array notation. (1)
- Write a function that takes 3 arguments and returns the calculation you did in Q1 (1)
- Write a function that takes 3 arguments and returns the largest value. Test on both strings and numbers. (2 marks)
- The purpose of this exercise is to practice with loops and arrays. Write a function that takes 2 arrays of equal length as arguments and does one of the following: (2 marks)
- returns a new array adding corresponding values.
- returns a new array multiplying corresponding values.
- returns a new array of truth values showing which values match
- returns a new array of matched pairs, ie: yourFunction([1, 2, 3], [‘Bob’, ‘Ted’, ‘Ivy])
should return: [ [1,’Bob’], [2,’Ted’], [3,’Ivy’]] - Converts the pair into a dictionary, ie: yourFunction([‘Bob’, ‘Ted’, ‘Ivy], [1,2,3])
should return {{‘Bob’ :1} , {‘Ted’: 2}, {‘Ivy’: 3}} - returns a new array consisting of the greater value of the two arrays, ie:
yourFunction ([7,4,9,2], [6,5,10,-1]) returns [7,5,10,2] - Test the map function in Example E. Try modifying it to make sure that you understand it.
Using map, write code that when given an array of strings, returns the length of each string.
ie: [‘Canada’, ‘Mauritania’, ‘US’, ‘Iceland’ ]. map(function() { ….. }) returns
[6, 10, 2 7] (2 marks) - Bonus (up to 6 marks/20% of the total)
Create up to 6 additional demos of JavaScript features that you didn’t use in the above exercises. In each case include a written description of what you are tyring to do. Some suggestions: illustrate the use of regular expressions or write a function that gathers similar information from different countries, show the use of thein or typeof operators, show how to generate random numbers in a specified range or demonstrate reduce. Compare or do calculations with dates. Marks will be awarded based on level of difficulty and copying published examples does not count. If I see the exact same answers from different people I’ll award marks only to the 1st one I see and write a note about plagiarism to the Associate Dean for the rest.
To hand in:
- Printout of your source code. Each item should appear under the appropriate number.
- Description of problems and items that are incomplete.
- Post your web page in your public_html directory under the name N123456789Lab1.html. ‘N’ should be in upper case. Replace the digits with your student #.
Late Assignments:
There is no physical drop box. Handling of late assignments is a problem and a headache for both of us. Try and avoid it. If you do you can ignore the following rather complex late policy. I wish you would.
- 5% for later the same day. This is to encourage people notto skip classes because you are still working on the assignment which will put you further behind.. There’s no difference in penalty between showing up late for class because you were finishing the assignment off and working on it after class.
- email your late assignment as a timestamp and hand in a hardcopy including when it was mailed. You can find me outside of your class hours in my scheduled labs:
- 10% for the next day and every day thereafter.
- I may choose to cap the maximum penalty at 50%/5 days.
- For a suitable and documented reason, usually illness, I may wave the late penalty.
- Notwithstanding, the value of the assignment goes to zero as soon as I return assignments to the class. You may wish to submit the assignment anyway for feedback purposes.
- If you fail to hand in a hardcopy I’ll charge you 2 marks if I have to print it out.