JS Variables Homework
Categories: Homework Breadcrumb: /csse/javascript/fundamentalsThese are the homework problems and popcorn hacks.
- TINKERERS
Presented by the
TINKERERS
<div style="font-weight:bold; text-decoration:underline;">Popcorn Hack 1 🍿😈</div>
Let’s start diving into some of the questions.
Instructions
Below instructions refer to this code cell:
%%html
<html>
<body>
<h2>Popcorn Hack 1 Output</h2>
<div id="output1"></div>
<script>
(() => {
let name = "Adya";
const age = 14;
var city = "San Diego";
const hobby = "painting";
// age = 2; // This would give an error when uncommented because age was a constant and couldn't be redefined.
document.getElementById("output1").innerText = name + " is " + age + " years old and lives in " + city + " and loves " + hobby + ".";
console.log(name + " is " + age + " years old and lives in " + city + " and loves " + hobby + ".")
console.log(age + " is a " + typeof age)
console.log(hobby + " is a " + typeof hobby)
})();
</script>
</body>
</html>
Popcorn Hack 1 Output
Now, do the below with this code.
- Adjust var declarations, names, values, etc. Mess around with it and observe any changes/errors.
- Think and/or discuss with your table: what changes did you notice?
Now let’s make some changes :)
-
Uncomment the line saying
age = 2;and look at your console. What do you notice? -
Add a new variable called
hobbywith the value of “painting” and update the DOM output and console output to say:
“[NAME] is [AGE] years old, lives in [CITY], and loves [HOBBY]” -
There’s a keyword called
typeofin JavaScript. Use this keyword to also display the data types of the variables. Example:typeof "John"gives"string"andtypeof 3.14gives"number"
<div style="font-weight:bold; text-decoration:underline;">Popcorn Hack 2 🍿😈</div>
Follow the below instructions.
- Go to the code cell below this text.
- Using the correct JS variable naming convention, declare a Magic Number variable with the value returned by
input.valueto get a user response. - Convert it to a Number data type using Number(). Example:
let x = Number(x);turns x into a Number. This is because prompt() always returns Strings. - Create variables
doubled,squared, andtripledthat contain the doubled, squared, and tripled values of the magic number. - Display the results in DOM and the console by changing output.innerText and using console.log().
%%html
<html>
<body>
<p>Click the button after entering your magic number!</p>
<input type="number" id="magicInput" placeholder="Enter magic number">
<button id="magicButton">Calculate</button>
<div id="output2">Your results will appear here.</div>
<script>
(() => {
const button = document.getElementById("magicButton");
const input = document.getElementById("magicInput");
const output = document.getElementById("output2");
button.addEventListener("click", () => {
// ^^ DO NOT MODIFY ANY ABOVE CODE ^^
// Set the Magic Number variable to input.value
let magicNumber = input.value;
// Convert the input to a number using Number()
magicNumber = Number(magicNumber);
// Calculate doubled, squared, and half
let doubled = magicNumber * 2;
let squared = magicNumber ** 2;
let tripled = magicNumber * 3;
// Display results in the DOM and console
output.innerText = "Doubled: " + doubled + ", Squared: " + squared + ", Tripled: " + tripled;
console.log("Doubled: " + doubled + ", Squared: " + squared + ", Tripled: " + tripled);
// vv DO NOT MODIFY ANY BELOW CODE vv
});
})();
</script>
</body>
</html>
Click the button after entering your magic number!
Variables Homework (Show what you know!)
Homework Problems: Understanding JavaScript Variables
There is a code block below the image saying “Have Fun!” Write your code in there.
Part A - Creating Variables
-
Create a variable called
nameand store your first name in it. Print it in the console and to DOM. -
Create two variables
ageandcity. Print them in a single sentence like: - “I am 15 years old and I live in New York.” -
Create a variable
isStudent(true/false). Print it.
Part B – Numbers & Strings
-
Create two number variables num1 = 10 and num2 = 5. Print their sum, difference, product, and quotient.
-
Make a variable
favoriteFoodand print: My favorite food is __.”
Part C – Practice Problems
-
Swap the values of two variables: x = 7 and y = 3.
-
Create a variable
fullNameby joining two strings: “FirstName” and “LastName”. -
Convert temperature C = 25 into Fahrenheit using F = (C * 9/5) + 32.
- Create a variable score = 85.
- Print “Pass” if score >= 50, else “Fail”.
-
Write a program that asks for your name and age (use prompt) and prints: “Hello
, you are years old." - Make a project that uses 5 variables to run. It can do anything yuou want, have fun and good luck!
Extra credit (optional): Instead of hard coding the variable for number 9 to 85, make the variable a random number from 1-100.

%%html
<html>
<body>
<h2>Homework Output</h2>
<div id="output"></div>
<script>
document.getElementById("output").innerText = ""; // Clear output
(() => {
// Part A
let name = "Adya";
const age = 14;
let city = "San Diego";
output.innerText = "I am " + age + " and I live in " + city + ".";
console.log("I am " + age + " and I live in " + city + ".");
let isStudent = true;
output.innerText += "\nAm I a student? " + isStudent;
console.log("Am I a student? " + isStudent);
// Part B
let num1 = 10;
let num2 = 5;
let sum = num1 + num2;
output.innerText += "\n\nThe sum of " + num1 + " and " + num2 + " is " + sum + ".";
console.log("The sum of " + num1 + " and " + num2 + " is " + sum + ".");
let difference = num1 - num2;
output.innerText += "\nThe difference when you subtract " + num2 + " from " + num1 + " is " + difference + ".";
console.log("The difference when you subtract " + num2 + " from " + num1 + " is " + difference + ".");
let product = num1 * num2;
output.innerText += "\nThe product of " + num1 + " and " + num2 + " is " + product + ".";
console.log("The product of " + num1 + " and " + num2 + " is " + product + ".");
let quotient = num1 / num2;
output.innerText += "\nThe quotient when you divide " + num1 + " by " + num2 + " is " + quotient + ".";
console.log("The quotient when you divide " + num1 + " by " + num2 + " is " + quotient + ".");
let favoriteFood = "pasta";
output.innerText += "\n\nMy favorite food is " + favoriteFood + ".";
console.log("My favorite food is " + favoriteFood + ".");
// Part C
let x = 7;
let y = 3;
output.innerText += "\n\nInitial values: x = " + x + ", y = " + y + ".";
console.log("Initial values: x = " + x + ", y = " + y + ".");
let temp = x; // Temporary variable to hold x so that both variables can be swapped
x = y;
y = temp;
output.innerText += "\nAfter swapping: x = " + x + ", y = " + y + ".";
console.log("After swapping: x = " + x + ", y = " + y + ".");
let firstName = "Adya ";
let lastName = "Shipekar";
let fullName = firstName + lastName;
output.innerText += "\n\nFull name: " + fullName + ".";
console.log("Full name: " + fullName + ".");
let temperatureC = 25;
let temperatureF = (temperatureC * 9/5) + 32;
output.innerText += "\n\n" + temperatureC + "°C is " + temperatureF + "°F.";
console.log(temperatureC + "°C is " + temperatureF + "°F.");
let score = 85;
if (score >= 50) {
output.innerText += "\n\nPass";
console.log("Pass");
} else {
output.innerText += "\n\nFail";
console.log("Fail");
}
let userName = prompt("Enter you name:", "Adya")
let userAge = prompt("Enter your age:", "14")
if (!userName) userName = "Adya";
if (!userAge) userAge = "14";
output.innerText += "\n\nHello " + userName + ", you are " + userAge + " years old.";
// Question 11:
output.innerText += "\n\nMy five variables: " + fullName + " is " + age + " and loves " + favoriteFood + ". She lives in " + city + " where the temperature is currently " + temperatureF + " degrees Fahrenheit."
// Extra Credit:
let num = Math.floor(Math.random() * 100) + 1;
console.log("The random number is: " + num);
output.innerText += "\n\nThe random number is: " + num;
})();
</script>
</body>
</html>
Homework Output
Submission
You will submit the link to your homework on a web page in the below form.
If you are unable to get your homework accessible from the website, you can upload this Jupyter notebook to the form.
IMPORTANT: If uploading, please name this Jupyter notebook in this format: [FirstName][LastName]_vars_hw.ipynb Example: SamarthHande_hw.ipynb
https://forms.gle/UBDFErZpKpTApWap8
Requirements for homework:
- Parts A, B, and C should be completed. You will get .3 points for each question completed.
- Up to 0.03 extra points will be given to code that demonstrates exceptional creativity and comprehension.