ELCT 5830 Network and Web Programming - Exercises 01
Lecture 01 JavaScript Basics
Q1.
Let X be a variable that stores a positive integer in which its last digit
is not zero. Write a segment of JS code to reverse its digits.
e.g. if X = 13579; X should become 97531.
1 | function reverseDigit(x) { |
Q2.
Write a segment of JS code to make a DEEP copy of array “fruits” and sort the elements in the cloned arrayby “id” in ascending order.
1 | const fruits = [ |
1 | function deepCopy(obj) { |
Q3.
Implement a function named “length” that takes one parameter and perform the followings:
- If the parameter is an array, return its length (# of elements)
- If the parameter is a string, return its length (# of characters)
- If the parameter is a number that is neither NaN nor Infinity, return the
number of digits in its integer portion. (e.g., for -456.99, the integer
portion is -456, so return 3) - Otherwise, return 0
1 | function length(x) { |
Q4.
Implement a function that takes one parameter and returns true if the parameter is an object containing the following three properties: ‘foo’, ‘bar’, and ‘foo-bar’. Otherwise, the function returns false.
Please note that the value of the properties can be undefined.
1 | function checkProperty(obj) { |