Midterm Exam 2
Part 1: Trace Code — Variables and Assignments
Write the value of variable a after executing each snippet of code. Each problem is worth one point, for a total of two points.
-
Value of a =
function mystery(x) { let y = x + 1; return y; } let a = 1; a = mystery(a);
-
Value of a =
let b = '5'; let c = '2'; let a = b + c;
Part 2: Trace Code — Statements
What is the result of executing each of the following snippets of code?
- If something is displayed, say specifically what is displayed.
- If a variable ends up with a value, explicitly say what it is.
- If anything else happens, say what happens.
Each problem is worth one point, for a total of three points.
-
Result:
let border = 0; let margin = 10; if (margin/2 > 4) { border = 2; } else { border = 1; }
-
Result:
for (let i = 0; i < 5; i++) { console.log(i); }
-
Result:
let names = ['alice', 'bob', 'carol']; let s = ''; for (let i = 0; i < names.length; i++) { s = s + ' ' + names[i]; } console.log(s);
Part 3: Short Answer
Provide short answers to the following questions (at most one or two sentences each). Each question is worth two points, for a total of ten points.
-
We say that strings, arrays, and
Math
are objects. List the two kinds of things stored in an object that we can use when working with the object. Give an example of each. - What is a function? Why do we need them?
-
You have learned about two ways to attach an event listener
to an HTML element. Give an example of each for attaching
a function named
react()
to an element's click event. - You have learned how to create an array of elements from a string. List at least two new features of strings that we used in such code.
-
List two methods we can use to modify the attributes of an
HTML element. Give an example of each for modifying an
element named
catImage
.
Part 4: Write Code
For each question, write the requested code. Each question is worth five points, for a total of thirty-five points.
You may find some of these functions, methods, and properties useful:
prompt()
andalert()
-
querySelector()
andquerySelectorAll()
-
getAttribute()
andsetAttribute()
innerText
andinnerHTML
Each solution is relatively short. Do not feel that you need to write a lot of code for any of the problems.
-
Write a loop that asks the user for a number smaller than
10, and keeps asking until the response is smaller
than 10. At the end of your code, the variable
choice
should hold the answer. -
Write a script that finds the HTML element with an id of
request
and adds these styles to the element:- changes the item's font style to italic
- changes the item's left margin to 50px
-
Write a script that that modifies every element of an array
named
schools
. Each element in the array is a string. Your script should replace each item 'x' with '<li>x</li>'.
For example, if before your codeschools
contains['Luther', 'Coe', 'Simpson', 'Central', 'Wartburg']
then after running your script it will contain['<li>Luther</li>', '<li>Coe</li>', '<li>Simpson</li>', '<li>Central</li>', '<li>Wartburg</li>']
-
Write a function named
logInvalidBytes(arr)
. When users call this function, they will give it an array of numbers that your function will call arr. The\ function should log all array elements that are larger than 255.
For example:> let bytes = [1, 260, 5, 233, 255, 256, 0]; > logInvalidBytes(bytes);
will write260
and256
to the console. -
Define a function
changeLink(linkNumber, url)
that changes the destination of a link.linkNumber
is the number of the link to change, andurl
is the new destination (a string).
For example, suppose a web page contains 100 links. CallingchangeLink(42, 'http://uni.edu')
should change the 42nd link on the page to point to the UNI homepage.
Hints:-
This should work on any page, so we can't use CSS
selectors like
:nth-child()
. Access all of thea
elements to get the anchor tags. -
The destination of a hyperlink is the value of its
href
attribute.
-
This should work on any page, so we can't use CSS
selectors like
-
Consider the following simplified HTML:
<body> <div id='request'>I want extra credit!</div> </body>
Write a short script that enables the following: When the user clicks on the words "I want extra credit!", the page displays an alert saying "You got it!" Hint: You will need to create a one-line function as part of your code. -
Define a function
createOrderedList(items)
that creates an HTML list element.items
is an array of strings that will be the items in the list.
For example, ifcourses
equals['CS 1100', 'CS 2100']
, then callingcreateOrderedList(courses)
will create and return anol
element with two list items, 'CS 1100' and 'CS 2100'.
Hints:- You will need to add list items to a list element, but you will not add the list element to any other element. The function simply returns it.
That is all!