Professional Documents
Culture Documents
Lab: Javascript - 1: Index - HTML Root
Lab: Javascript - 1: Index - HTML Root
1. Create an HTML document, name it index.html, and place it in the root of your project directory.
3. Link the script.js document to the index.html by adding the following tag to the bottom of the
body of the HTML page, just before the </body> tag:
...
<script type="text/javascript" src="src/js/script.js"></script>
</body>
5. Open the HTML document with the live server, open the developer tool, go to the console tab, and
you should be able to see the results running the JS code.
7. Define an array, and name it userPets, which contains two elements "Cat" and "Dog".
12. Output the values of the three newly defined variables to the console.
15. How many days the user balance is sufficient for the 3 pets?
17. Change the everyDaySpendingPerPet value to 2.4, and check how many days the user balance is
sufficient for the 2 pets.
Tasks:
1. Complete the following function that takes a string (e.g., your name) and prints it to the console.
Then, print it letter by letter to the console, as shown in the snapshot:
function nameVertical(name) {
…
}
// Sarah for example
nameVertical(Sarah)
2. Complete the following function that takes a number and returns to which HTTP response status
grouped it belongs.
If the number does not belong to any of these groups, the message "Not a valid code" should be
presented.
Note: the main idea of this task is using the ternary operator (condition ? exIfTrue :
exIfFalse) in a way similar to if else if … else statement.
function code(n) {
return (n < 100) ? "Not a valid code" :
…
}
// for example n = 121
console.log(code(121));
3. Complete the following function that takes two variables and compares them.
The function should print to the console the values of the passed variables.
If the two variables have the same values and they are of the same type, the function should
print to the console: The two variables have the same value and type
If the two numbers have the same values but they are from different types, the function should
print to the console: The two variables have the same value but not the same type followed by
the type of each of them, as follows: the type of var1 is var1 type
Otherwise, the function should print to the console: The two variables do not have the same
value nor the same type
// After completing the function pass different values instead of var1 and var2 to test
your function
4. Write a function to print the Fibonacci Sequence up to a certain number (n). The Fibonacci sequence
is a series of numbers that starts with 0, then, 1, and the next number in the sequence is calculated by
adding the two numbers before it, the program should stop when the sum of these two numbers is
bigger (>) than n.
function fibonacci(n) {
…
}
// After completing the function pass different numbers instead of n and test the
result.
fibonacci(n);
Step 1: try to figure out how many variables you need, then declare them.
Step 3: try to figure out the type of loop you need, and then the condition that will make the loop
stop.
Step 4: try to figure out how you will print the value of the variables.
Do it yourself:
1. Write a function to output the Fibonacci Sequence up to a certain number (n) as an array.
2. Complete the following code so that the for loop works and output the content of the courses array
to the console.
Note: you cannot modify "for (;;)", i.e., for(i = 0, i< courses.length, i++) is not an option. Also, you
cannot use other types of loops. You need to write the code that makes this for loop work.
for (;;) {
//code to be added only here
}