Tag: Mateen Khadem

  • 2025-02-05

    Basic Output & Interaction

    Console Logging a Message

    Exercise: Display a simple message in the browser console.

    Steps:

    Use the console logging function to send a string message.

    Open your browser’s developer tools to see the output.

    <html>
    <head>
    <title>Exercise</title>
    </head>
    <body>
    <script>
    console.log("Hello! This is a message!");
    </script>
    </body>
    </html>

    Alert Pop-up

    Exercise: Show a pop-up message when the page loads.

    Steps:

    Use the function that triggers a window alert.

    Insert a text message inside the alert function.

    <html>
    <head>
    <title>Exercise</title>
    </head>
    <body>
    <script>
    window.alert("Let's code together!");
    </script>
    </body>
    </html>

    Writing to the Document

    Exercise: Output text on the HTML web page dynamically.

    Steps:

    Choose the document helper that writes to the HTML.

    Output your chosen text on the page.

    <html>
    <head>
    <title>Exercise</title>
    </head>
    <body>
    <script>
    document.write("This is a dynamic text");
    </script>
    </body>
    </html>

    Prompt for Input

    Exercise: Ask the user for their name and store the input.

    Steps:

    Use the prompt function to request input.

    Save the value in a variable.

    <html>
    <head>
    <title>Exercise</title>
    </head>
    <body>
    <script>
    let name = prompt("What is your name?: ");
    </script>
    </body>
    </html>

    Modify Inner HTML of an Element

    Exercise: Change the text of an element with a given ID.

    Steps:

    Access the element using its unique identifier.

    Change the element’s inner text to a new message.

    <html>
    <head>
    <title>Exercise</title>
    </head>
    <body>
    <p id="paragraph">Before Change</p>
    <script>
    function changeParagraph() {
    document.getElementById("paragraph").innerHTML = "After Change";
    }
    </script>
    <button onclick="changeParagraph()">Change!</button>
    </body>
    </html>

    Variables & Declarations

    Declare Variables with Let and Var

    Exercise: Create two variables with let and var using different values.

    Steps:

    Use the let keyword to declare one variable.

    Use the var keyword to declare another variable, assigning a distinct value.

    <html>
    <head>
    <title>Exercise</title>
    </head>
    <body>
    <script>
    let a = "Value1";
    var b = "Value2";
    </script>
    </body>
    </html>

    Usage of Const

    Exercise: Create a constant and assign a string value that will not change.

    Steps:

    Declare a constant using the correct keyword.

    Ensure that its value cannot be changed later in the code.

    <html>
    <head>
    <title>Exercise</title>
    </head>
    <body>
    <script>
    const text = "This is a string";
    </script>
    </body>
    </html>

    Variable Reassignment

    Exercise: Reassign a variable declared with let, then try with const and notice the difference.

    Steps:

    Change the value of a let variable.

    Attempt to change a const variable and observe the error or the behavior in your browser console.

    <html>
    <head>
    <title>Exercise</title>
    </head>
    <body>
    <script>
    let variable = "A variable";
    variable = "This and that";
    const text = "This is a string";
    text = "This won't work!";
    </script>
    </body>
    </html>

    Understanding Variable Scope

    Exercise: Explain the difference in scope between var and let by describing possible outcomes in different blocks.

    Steps:

    Create a variable with var inside a block (e.g., an if statement).

    Create a variable with let inside the same block.

    Describe in words or comments how they behave differently outside the block.

    <html>
    <head>
    <title>Exercise</title>
    </head>
    <body>
    <script>
    a = 0;
    b = 0;
    if (a == b){
    let c = 1;
    var d = 2;
    }
    document.write(d);
    //It seems like var works in a global scope so it works outside the block too!
    </script>
    </body>
    </html>

    Explaining Data Types

    Exercise: List and explain the basic JavaScript data types.

    Steps:

    Write a comment or note that enumerates each type: String, Number, BigInt, Boolean, Undefined, Null, Symbol, and Object.

    Provide a short description of each type.


    Working with Arrays

    Create and Log an Array

    Exercise: Initialize an array with a few string elements and log it to the console.

    Steps:

    Use the array literal format to create an array.

    Output the array to the console using your logging function.

    <html>
    <head>
    <title>Exercise</title>
    </head>
    <body>
    <script>
    let theCities = ["Teheran", "Qom", "Isfahan"];
    console.log(theCities);
    </script>
    </body>
    </html>

    Appending Elements to an Array

    Exercise: Add a new element at the end of an array using the method you know.

    Steps:

    Identify the function that appends an element to an array.

    Use it to add a new string to the array.

    <html>
    <head>
    <title>Exercise</title>
    </head>
    <body>
    <script>
    let theCities = ["Teheran", "Qom", "Isfahan"];
    theCities.push("Tabriz");
    console.log(theCities);
    </script>
    </body>
    </html>

    Removing the Last Element of an Array

    Exercise: Remove the last element from an array.

    Steps:

    Use the appropriate array method to remove the last element.

    Optionally log the modified array to confirm removal.

    <html>
    <head>
    <title>Exercise</title>
    </head>
    <body>
    <script>
    let theCities = ["Teheran", "Qom", "Isfahan"];
    theCities.pop();
    console.log(theCities);
    </script>
    </body>
    </html>

    Working with Array Operations

    Exercise: Create an array with three items, then add one more using push and finally remove one using pop.

    Steps:

    Declare an array with three elements.

    Use the method to add another item.

    Use the removal method to remove the last added item.

    <html>
    <head>
    <title>Exercise</title>
    </head>
    <body>
    <script>
    let theCities = ["Teheran", "Qom", "Isfahan"];
    theCities.push("Mashhad");
    theCities.pop();
    console.log(theCities);
    </script>
    </body>
    </html>

    Swap Two Elements in an Array

    Exercise: Write steps to switch the positions of the first two elements in an array.

    Steps:

    Access the element at the first position.

    Access the element at the second position.

    Swap their positions logically, and then verify the new order by outputting the array.

    <html>
    <head>
    <title>Exercise</title>
    </head>
    <body>
    <script>
    let theCities = ["Teheran", "Qom", "Isfahan"];
    let a = theCities[0];
    let b = theCities[1];
    theCities = [b, a, "Isfahan"];
    console.log(theCities);
    </script>
    </body>
    </html>

    Understanding Arithmetic Operators

    Simple Math Operation

    Exercise: Use addition to combine two numbers.

    Steps:

    Identify two numeric variables or values.

    Use the addition operator to sum them.

    Log the result.

    <html>
    <head>
    <title>Exercise</title>
    </head>
    <body>
    <script>
    let a = 2;
    let b = 3; 
    console.log(a + b);
    </script>
    </body>
    </html>

    Subtraction Operation

    Exercise: Subtract one number from another and display the result.

    Steps:

    Select two numbers.

    Use the subtraction operator between them.

    View the outcome.

    <html>
    <head>
    <title>Exercise</title>
    </head>
    <body>
    <script>
    let a = 2;
    let b = 3; 
    console.log(a - b);
    </script>
    </body>
    </html>

    Multiplication and Division

    Exercise: Multiply two numbers and then divide the product by a given number.

    Steps:

    Multiply two selected numbers.

    Divide the multiplication result by another number.

    Log the final answer.

    <html>
    <head>
    <title>Exercise</title>
    </head>
    <body>
    <script>
    let a = 2;
    let b = 3; 
    let c = a * b;
    let d = c / 2;
    console.log(d);
    </script>
    </body>
    </html>

    Modulus Operation

    Exercise: Find the remainder of a division operation using modulus.

    Steps:

    Choose two numbers where one is not a multiple of the other.

    Apply the modulus operator and then log the remainder.

    <html>
    <head>
    <title>Exercise</title>
    </head>
    <body>
    <script>
    let a = 2;
    let b = 3; 
    let c = a * b;
    let d = c % 2;
    console.log(d);
    </script>
    </body>
    </html>

    Exponentiation Operation

    Exercise: Raise a number to a certain power using exponentiation.

    Steps:

    Select a base number and an exponent.

    Use the exponentiation operator to calculate the power.

    Log/display the result.

    <html>
    <head>
    <title>Exercise</title>
    </head>
    <body>
    <script>
    let a = 2;
    let b = 3; 
    console.log(a ** b);
    </script>
    </body>
    </html>

    Incrementing a Number

    Exercise: Increase a variable’s value by one using the increment operator.

    Steps:

    Start with a numeric variable.

    Increase it by one.

    Output the new number.

    Decrementing a Number

    Exercise: Decrease a variable’s value by one using the decrement operator.

    Steps:

    Use a numeric variable.

    Decrease it by one.

    Validate by logging the value.

    Chaining Arithmetic Operations

    Exercise: Combine addition, multiplication, and division in one expression.

    Steps:

    Decide on three numerical values.

    Form an expression mixing several arithmetic operators.

    Compute and log the final value.

    <html>
    <head>
    <title>Exercise</title>
    </head>
    <body>
    <script>
    let a = 2;
    let b = 4;
    let c = 6;
    let d = a + b * 3 / 6;
    console.log(d);
    </script>
    </body>
    </html>

    Arithmetic with Variables

    Exercise: Declare two number variables and perform several arithmetic operations between them.

    Steps:

    Create two variables with numbers.

    Add, subtract, multiply, and divide them in successive steps.

    Log each result separately.

    Calculate the Area of a Rectangle

    Exercise: Write down the steps to calculate the area from given length and width values.

    Steps:

    Define variables for length and width.

    Multiply them to get the area.

    Output the computed area.

    <html>
    <head>
    <title>Exercise</title>
    </head>
    <body>
    <script>
    let height = 200;
    let width = 100;
    let area = height * width;
    console.log(area);
    </script>
    </body>
    </html>

  • 2025-02-04

    1. Display a Message with console.log()

    Task: Use console.log() to display a simple greeting message.

    Steps to Solve:

    Identify what message you want to show (e.g., “Hello, JavaScript!”).

    Use the console.log() method with your message as the argument.

    Run your code in the browser’s console or an editor to see the output.

    <html>
    <head>
    <title>Exercise</title>
    </head>
    <body>
    <script>
    console.log("Hello, There!");
    </script>
    </body>
    </html>

    2. Alert a Message with window.alert()

    Task: Display an alert box with a custom greeting.

    Steps to Solve:

    Determine the text content for the alert.

    Call window.alert() or simply alert() with your text.

    Test the code in your browser to see the popup.

    <html>
    <head>
    <title>Exercise</title>
    </head>
    <body>
    <script>
    window.alert("This is a message from the people of the earth");
    </script>
    </body>
    </html>

    3. Manipulate HTML Content using document.getElementById()

    Task: Change the text content of an HTML element by its id.

    Steps to Solve:

    Create an HTML element with a unique id attribute.

    Use document.getElementById() to access this element.

    Modify its innerHTML or textContent property.

    Refresh your page to observe the change.

    <html>
    <head>
    <title>Exercise</title>
    </head>
    <body>
    <h1 id="heading1">This is the title before change</h1>
    <script>
    function change() {
    document.getElementById("heading1").innerHTML = "This is the title after change";
    }
    </script>
    <button onclick="change()">Click here to Change!</button>
    </body>
    </html>

    4. Understanding let Variable Declaration

    Task: Declare a variable using let and assign it a value.

    Steps to Solve:

    Choose a variable name that follows valid naming conventions.

    Use the let keyword to declare the variable.

    Assign an initial value.

    Optionally, display its value using console.log().

    <html>
    <head>
    <title>Exercise</title>
    </head>
    <body>
    <h1 id="heading1">This is the title before change</h1>
    <script>
    let a = 15;
    let b = 15;
    console.log(a + b);
    </script>
    </body>
    </html>

    5. Understanding var Variable Declaration

    Task: Declare a variable using var and assign it a value.

    Steps to Solve:

    Use the var keyword to declare a variable.

    Assign a value to the variable.

    Observe how var variables differ in scoping compared to let.

    <html>
    <head>
    <title>Exercise</title>
    </head>
    <body>
    <h1 id="heading1">This is the title before change</h1>
    <script>
    var a = 15;
    var b = 15;
    console.log(a + b);
    </script>
    </body>
    </html>

    6. Using const for Constant Variables

    Task: Declare a constant using const and assign it a value.

    Steps to Solve:

    Use the const keyword to declare a variable that won’t change.

    Try to assign a new value later to see how JavaScript handles it.

    Log the result if needed.

    <html>
    <head>
    <title>Exercise</title>
    </head>
    <body>
    <h1 id="heading1">This is the title before change</h1>
    <script>
    const variable = "value";
    variable = "secondvalue";
    console.log(variable);
    </script>
    </body>
    </html>

    7. Basic Arithmetic Operations (Addition)

    Task: Use basic arithmetic (addition) between two variables.

    Steps to Solve:

    Declare two numeric variables.

    Create a third variable to hold the sum of the first two.

    Use console.log() to display the result.

    Validate your solution by checking basic math.

    <html>
    <head>
    <title>Exercise</title>
    </head>
    <body>
    <h1 id="heading1">This is the title before change</h1>
    <script>
    var firstNumber = 12;
    var secondNumber = 12;
    function sum() {
    let thirdNumber = firstNumber + secondNumber;
    window.alert(thirdNumber);
    }
    </script>
    <button onclick="sum()">Click on me!</button>
    </body>
    </html>

    8. Other Arithmetic Operations: Subtraction, Multiplication, Division, and Modulus

    Task: Perform subtraction, multiplication, division, and modulus operations.

    Steps to Solve:

    Declare two numerical variables.

    Compute subtraction, multiplication, division, and remainder using appropriate operators.

    Log each result.

    Ensure that division by zero is avoided.

    <html>
    <head>
    <title>Exercise</title>
    </head>
    <body>
    <h1 id="heading1">This is the title before change</h1>
    <script>
    var firstNumber = 12;
    var secondNumber = 12;
    var sub = firstNumber - secondNumber;
    var mult = firstNumber * secondNumber;
    var div = firstNumber / secondNumber;
    console.log(div);
    </script>
    </body>
    </html>
  • 2025-02-03

    Exercise 1: Display “Hello, World!” with innerHTML

    Task:
    Create an HTML element (like a div or span) with an id, and using JavaScript, insert the text “Hello, World!” into it with innerHTML.

    Steps to Solve:

    In your HTML file, create an element with a unique id (for example, <div id="output"></div>).

    In your JavaScript, select the element using document.getElementById("output").

    Set the element’s innerHTML to “Hello, World!” (i.e., element.innerHTML = "Hello, World!";).

    <html>
    <head>
    <title>Exercise</title>
    </head>
    <body>
    <script>
    <div id="output"></div>
    document.getElementById("output").innerHTML = "Hello, World!";
    </script>
    </body>
    </html>

    Exercise 2: Write a Message Using document.write()

    Task:
    Use document.write() to display a line of text on the page when it loads.

    Steps to Solve:

    Open your JavaScript file or script tag.

    Call document.write("This is a message from document.write()!");

    Save and open your HTML file in a browser to see the output.

    <html>
    <head>
    <title>Exercise</title>
    </head>
    <body>
    <script>
    document.write("This is a message from document.write()!");
    </script>
    </body>
    </html>

    Exercise 3: Alert a Greeting

    Task:
    Use window.alert() to display a greeting message when the page loads.

    Steps to Solve:

    Create a JavaScript file or add a <script> section in your HTML.

    Use window.alert("Welcome to learning JavaScript!");

    Refresh your page to see the alert popup.

    <html>
    <head>
    <title>Exercise</title>
    </head>
    <body>
    <script>
    window.alert("Welcome to learning JavaScript!");
    </script>
    </body>
    </html>

    Exercise 4: Log a Message to the Browser Console

    Task:
    Use console.log() to print a message to the browser’s console.

    Steps to Solve:

    Open your script file or <script> tag in your HTML.

    Write console.log("Logging a test message"); in your code.

    Open your browser’s developer console (usually by pressing F12 or right-clicking and selecting “Inspect”) to see the message.

    <html>
    <head>
    <title>Exercise</title>
    </head>
    <body>
    <script>
    console.log("Logging a test message");
    </script>
    </body>
    </html>

    Exercise 5: Display a Variable via innerHTML

    Task:
    Declare a variable containing your favorite quote and output it into an HTML element using innerHTML.

    Steps to Solve:

    Declare a variable, for example: var quote = "The only limit is your mind.";

    In your HTML, create an element with an id (e.g., <p id="quoteArea"></p>).

    Use document.getElementById("quoteArea").innerHTML = quote; in JavaScript.

    <html>
    <head>
    <title>Exercise</title>
    </head>
    <body>
    <h2 id="h2"></h2>
    <script>
    var theQuote = "This is a Quote";
    document.getElementById("h2").innerHTML = theQuote;
    </script>
    </body>
    </html>

    Exercise 6: Concatenate Two Strings and Display Using document.write()

    Task:
    Write two different strings (e.g., first name and last name), concatenate them, and then use document.write() to display the full name.

    Steps to Solve:

    Declare two variables:
    var firstName = "John";
    var lastName = "Doe";

    Concatenate the strings (e.g., var fullName = firstName + " " + lastName;).

    Use document.write("Full Name: " + fullName); to display the result.

    <html>
    <head>
    <title>Exercise</title>
    </head>
    <body>
    <h2 id="h2"></h2>
    <script>
    var stringOne = "Mateen";
    var stringTwo = "Khadem";
    document.write(stringOne + " " + stringTwo);
    </script>
    </body>
    </html>

    Exercise 7: Array of Favorite Movies Displayed via innerHTML

    Task:
    Create an array of your favorite movies and display them as a comma-separated list in an HTML element.

    Steps to Solve:

    Declare an array:
    var movies = ["Inception", "The Matrix", "Interstellar"];

    Join the array into a string using movies.join(", ").

    In the HTML, create an element with an id (e.g., <div id="moviesList"></div>).

    Set the element’s innerHTML to the joined string:
    document.getElementById("moviesList").innerHTML = movies.join(", ");


    Exercise 8: Display Array Length in the Console

    Task:
    Create an array and use console.log() to print the length (number of elements) of the array.

    Steps to Solve:

    Declare an array: var colors = ["red", "green", "blue"];

    Determine the length: var length = colors.length;

    Log the length: console.log("Array length is: " + length);

    <html>
    <head>
    <title>Exercise</title>
    </head>
    <body>
    <script>
    var ser = ["Ratched", "The Silo", "Severance"];
    var length = ser.length;
    console.log("The Array is " + length + " Series Long!");
    </script>
    </body>
    </html>

    Exercise 9: Change HTML Content on Click

    Task:
    Create a button in HTML that, when clicked, will change the content of a paragraph using innerHTML.

    Steps to Solve:

    In HTML, create a button (<button onclick="changeText()">Click Me</button>) and a paragraph (<p id="para">Original Text</p>).

    In your script, write the function:function changeText() { document.getElementById("para").innerHTML = "Text has been changed!"; }

    Test by clicking the button.

    <html>
    <head>
    <title>Exercise</title>
    </head>
    <body>
    <p id="paragraph">This is Before Change</p>
    <script>
    function change(){
    document.getElementById("paragraph").innerHTML = "This is After Change";
    }
    </script>
    <button onclick="change()">Change!</button>
    </body>
    </html>

    Exercise 10: Alert Array Elements

    Task:
    Create an array of three fruits and use window.alert() to display the first fruit in the array.

    Steps to Solve:

    Declare an array: var fruits = ["Apple", "Banana", "Cherry"];

    Select the first element (fruits[0]).

    Use window.alert("The first fruit is " + fruits[0]);.

    <html>
    <head>
    <title>Exercise</title>
    </head>
    <body>
    <script>
    var fruits = ["Orange", "Apple", "Banana"];
    window.alert(fruits[0]);
    </script>
    </body>
    </html>

    Exercise 11: Check if a Variable is Undefined

    Task:
    Declare a variable without assigning a value and use console.log() to print out a message saying whether the variable is undefined or not.

    Steps to Solve:

    Declare a variable: var myVar;

    Use an if-statement to check if it is undefined:if (typeof myVar === "undefined") { console.log("myVar is undefined"); } else { console.log("myVar is defined"); }

    Run it and check the console.

    <html>
    <head>
    <title>Exercise</title>
    </head>
    <body>
    <script>
    var variable;
    if (typeof variable === "undefined"){
    console.log("variable is undefined");
    } else {
    console.log("variable is defined");
    }
    </script>
    </body>
    </html>

    Exercise 12: Simple Calculator with Prompt Input

    Task:
    Prompt the user to enter two numbers, add them together, and display the result via an alert.

    Steps to Solve:

    Use prompt() to get two numbers:var num1 = parseFloat(prompt("Enter first number:")); var num2 = parseFloat(prompt("Enter second number:"));

    Calculate the sum: var sum = num1 + num2;

    Then use window.alert("The sum is " + sum);.


    Exercise 13: Swap the Content of Two HTML Elements

    Task:
    Use two HTML elements (like two paragraphs), and write a function that swaps their innerHTML values when a button is clicked.

    Steps to Solve:

    Create two paragraphs with different ids (e.g., <p id="first">First</p> and <p id="second">Second</p>) and a button with an onclick event (e.g., <button onclick="swapContent()">Swap</button>).

    In JavaScript, write a function:function swapContent() { var first = document.getElementById("first").innerHTML; var second = document.getElementById("second").innerHTML; document.getElementById("first").innerHTML = second; document.getElementById("second").innerHTML = first; }

    Test by clicking the button.

    <html>
    <head>
    <title>Exercise</title>
    </head>
    <body>
    <p id="p1">This is The First Paragraph</p>
    <p id="p2">This is The Second Paragraph</p>
    <script>
    function swapContent() {
    var first = document.getElementById("p1").innerHTML;
    var second = document.getElementById("p2").innerHTML;
    document.getElementById("p1").innerHTML = second;
    document.getElementById("p2").innerHTML = first;
    }
    </script>
    <button onclick="swapContent()">Swap</button>
    
    </body>
    </html>

    Exercise 14: Prompt User Name and Display a Personalized Message

    Task:
    Prompt the user to enter their name, then display a welcome congratulatory message on the page using innerHTML.

    Steps to Solve:

    Use prompt() to get the user’s name:
    var userName = prompt("Please enter your name:");

    Construct a message, for example: var message = "Welcome, " + userName + "!";

    Use innerHTML to display:
    document.getElementById("welcomeMessage").innerHTML = message;

    In HTML, add an element: <div id="welcomeMessage"></div>.

    <html>
    <head>
    <title>Exercise</title>
    </head>
    <body>
    <h1 id="welcome"></h1>
    <script>
    var name = prompt("What is your name: ");
    document.getElementById("welcome").innerHTML = "Welcome to your own website " + name;
    </script>
    </body>
    </html>

    Exercise 15: Change Text Color Using JavaScript

    Task:
    Create a button that when pressed will change the color of the text in an HTML element using JavaScript.

    Steps to Solve:

    In your HTML file, add an element with text (e.g., <p id="colorText">Change my color!</p>) and a button (<button onclick="changeColor()">Change Color</button>).

    In your JavaScript, write a function:function changeColor() { document.getElementById("colorText").style.color = "blue"; }

    Click the button to see the text color update.


    Exercise 16: Create and Use Multiple Variables in an Alert

    Task:
    Declare three variables (name, age, and city), then combine them into a sentence and display the sentence using an alert.

    Steps to Solve:

    Declare the variables, for example:var name = "Bob"; var age = 30; var city = "New York";

    Construct a sentence:var message = name + " is " + age + " years old and lives in " + city + ".";

    Use window.alert(message); to display it.

    Test by opening your web page.

    <html>
    <head>
    <title>Exercise</title>
    </head>
    <body>
    <script>
    var name = prompt("What is your name? ");
    var age = prompt("How old are you? ");
    var city = prompt("Where are you from? ");
    window.alert("So you are " + name + ", you are " + age + " and from " + city);
    </script>
    </body>
    </html>