Variables Homework (Show what you know!)

Homework Problem: Understanding JavaScript Variables

Creating Variables

  1. Create three variables as an object variable:
    • A variable called studentName that stores your name as a string.
    • A variable called age that stores your age as a number.
    • A variable called isStudent that stores a boolean value indicating whether you are a student (use true or false).
    1. Create a function variable that displays that your favorite song is playing:

      • A variable called favoriteSong that stores the name of your favorite song.
      • A variable called songArtist that stores the song’s artist.
      • A function called playFavoriteSong that logs a message saying “Now playing [favoriteSong] by [songArtist]”.
  2. Using the variables you created, write a sentence using console.log() to display:
    • Your name.
    • Your age.
    • Whether you are a student.
    • An output that states that your favorite song is now playing

    Example output: My name is [Your Name], I am [Your Age] years old, and it is [true/false] that I am a student. Now playing Champion by Kanye West

%%js

var studentName = "Lucas";
var age = 14;
var isStudent = true;

var favoriteSong = "Mr. Blue Sky";
var songArtist = "Electric Light Orchestra";

// Cool in-line syntax for displaying variable values in text!
function playFavoriteSong(favoriteSong, songArtist) {
    console.log(`Now playing: ${favoriteSong} by ${songArtist}`);
}

// Same here, much more readable than concatenation
console.log(`My name is ${studentName} and I am ${age} years old, and it is ${isStudent} that I am a student.`); 
console.log(playFavoriteSong(favoriteSong, songArtist));

// Function to update student information
function updateStudentInfo(newName, newAge, newIsStudent) {
    studentName = newName;
    age = newAge;
    isStudent = newIsStudent;
    console.log(`Updated Info - Name: ${studentName}, Age: ${age}, Is Student: ${isStudent}`);
}

// Function to change favorite song
function changeFavoriteSong(newSong, newArtist) {
    favoriteSong = newSong;
    songArtist = newArtist;
    console.log(`Favorite song updated to: ${favoriteSong} by ${songArtist}`);
}

// Function to display student info
function displayStudentInfo() {
    console.log(`My name is ${studentName} and I am ${age} years old, and it is ${isStudent} that I am a student.`);
    playFavoriteSong(favoriteSong, songArtist);
}

// Initial display of student info
displayStudentInfo();

// Example usage of update functions
updateStudentInfo("Lucas 2", 15, false);
changeFavoriteSong("Bohemian Rhapsody", "Queen");

// Display updated info
displayStudentInfo();
<IPython.core.display.Javascript object>