Assignment 8

Chapter 1

  1. Write a script to greet your website visitor using JS alert box.

  2. Write a script to display following message on your web page:

  3. Write a script to display following message on your web page: (Hint : Use line break)

  4. Write a script to display following messages in sequence:

  5. Generate the following message through browser’s developer console:

Chapter 2

  1. Declare a variable called username.

  2. Declare a variable called myName & assign to it a string that represents your Full Name.

  3. Write script to
    1. Declare a JS variable, titled message.

    2. Assign “Hello World” to variable message

    3. Display the message in alert box

  4. Write a script to save student’s bio data in JS variables and show the data in alert boxes.


  5. Write a script to display the following alert using one JS variable:


  6. Declare a variable called email and assign to it a string that represents your Email Address(e.g. example@example.com). Show the blow mentioned message in an alert box.(Hint: use string concatenation)


  7. Declare a variable called book & give it the value “A smarter way to learn JavaScript”. Display the following message in an alert box:


Chapter 3

  1. Declare a variable called age & assign to it your age. Show your age in an alert box.

  2. Declare & initialize a variable to keep track of how many times a visitor has visited a web page. Show his/her number of visits on your web page. For example: “You have visited this site N times”.

  3. Declare a variable called birthYear & assign to it your birth year. Show the following message in your browser:



  4. A visitor visits an online clothing store www.xyzClothing.com . Write a script to store in variables the following information: a. Visitor’s name b. Product title c. Quantity i.e. how many products a visitor wants to order Show the following message in your browser: “John Doe ordered 5 T-shirt(s) on XYZ Clothing store”. VARIABLE



Chapter 4

  1. Declare 3 variables in one statement

  2. Declare 5 legal & 5 illegal variable names.

  3. Display this in your browser
    1. heading stating “Rules for naming JS variables”
    2. Variable names can only contain ______, ______, ______ and ______. For example $my_1stVariable
    3. Variables must begin with a ______, ______ or _____. For example $name, _name or name
    4. Variable names are case _________
    5. Variable names should not be JS _________


    output:

    Rules for naming JS variables



    Variable names can only contain, numbers, $ and _. For example : $my_1st
    Variable Variable must begin with a letter, $ or _ . For example : $name, _name or name
    Variable name are case sensitive
    Variable names should not be JS keywords

Chapter 5

  1. Write a program that take two numbers & add them in a new variable. Show the result in your browser.

  2. Repeat task1 for subtraction, multiplication, division & modulus.

  3. Do the following using JS Mathematic Expressions
    1. Declare a variable.
    2. Show the value of variable in your browser like “Value after variable declaration is: ??
    3. Initialize the variable with some number.
    4. Show the value of variable in your browser like “Initial value: 5”.
    5. Increment the variable.
    6. Show the value of variable in your browser like “Value after increment is: 6”.
    7. Add 7 to the variable.
    8. Show the value of variable in your browser like “Value

  4. after addition is: 13”.
    1. Decrement the variable.
    2. . Show the value of variable in your browser like “Value after decrement is: 12”.
    3. Show the remainder after dividing the variable’s value by 3.
    4. Output : “The remainder is : 0”.

  5. . Cost of one movie ticket is 600 PKR. Write a script to store ticket price in a variable & calculate the cost of buying 5 tickets to a movie. Example output:

  6. Write a script to display multiplication table of any number in your browser. E.g
  7. The Temperature Converter:It’s hot out! Let’s make a converter based on the steps here.

    1. Store a Celsius temperature into a variable.
    2. Convert it to Fahrenheit & output “NNoC is NNoF”.
    3. Now store a Fahrenheit temperature into a variabl
    4. Convert it to Celsius & output “NNoF is NNoC”.
    Conversion Formulae:
  8. Write a program to implement checkout process of a shopping cart system for an e-commerce website. Store the following in variables

    1. Price of item 1
    2. Price of item 2
    3. Ordered quantity of item 1
    4. Ordered Quantity of item 2
    5. Shipping charges
    Compute the total cost & show the receipt in your browser.

  9. Store total marks & marks obtained by a student in 2 variables. Compute the percentage & show the result in your browser
  10. Assume we have 10 US dollars & 25 Saudi Riyals. Write a script to convert the total currency to Pakistani Rupees. Perform all calculations in a single expression. (Exchange rates : 1 US Dollar = 104.80 Pakistani Rupee and 1 Saudi Riyal = 28 Pakistani Rupee)

  11. . Write a program to initialize a variable with some number and do arithmetic in following sequence:
    1. . Add 5
    2. Multiply by 10
    3. Divide the result by 2 Perform all calculations in a single expression
  12. The Age Calculator: Forgot how old someone is? Calculate it!
    1. Store the current year in a variable.
    2. Store their birth year in a variable.
    3. . Calculate their 2 possible ages based on the stored values.
    4. Output them to the screen like so: “They are either NN or NN years old”.
  13. The Geometrizer: Calculate properties of a circle
    1. Store a radius into a variable.
    1. Calculate the circumference based on the radius, and output “The circumference is NN”.
    (Hint : Circumference of a circle = 2 π r , π = 3.142) Calculate the area based on the radius, and output “The area is NN” (Hint : Area of a circle = π r2, π = 3.142)
  14. The Lifetime Supply Calculator: Ever wonder how much a “lifetime supply” of your favorite snack is? Wonder no more.
    1. Store your favorite snack into a variable
    2. Store your current age into a variable.
    3. Store a maximum age into a variable
    4. . Store an estimated amount per day (as a number).
    5. Calculate how many would you eat total for the rest of your life.
    Output the result to the screen like so: “You will need NNNN to last you until the ripe old age of NN”.

Chapter 6

  1. Write a program to take a number in a variable, do the required arithmetic to display the following result in your browser:

  2. 2. What will be the output in variables a, b & result after execution of the following script: var a=2 , b=1 ; var result=- -a - --b + ++b + b--; Explain the output at each stage: --a; --a - --b; --a - --b + ++b; --a - --b + ++b + b--;
  3. Write a program that takes input a name from user & greet the user.
  4. . Write a program to take input a number from user & display it’s multiplication table on your browser. If user does not enter a new number, multiplication table of 5 should be displayed by default.
  5. Take
    1. Take three subjects name from user and store them in 3 different variables.
    2. Total marks for each subject is 100, store it in another variable.
    3. Take obtained marks for first subject from user and stored it in different variable.
    1. Take obtained marks for remaining 2 subjects from user and store them in variables.
    2. Now calculate total marks and percentage and show the result in browser like this.(Hint: user table)

Chapter 9

  1. 1. Write a program to take “city” name as input from user. If user enters “Karachi”, welcome the user like this: “Welcome to city of lights”

  2. Write a program to take “gender” as input from user. If the user is male, give the message: Good Morning Sir. If the user is female, give the message: Good Morning Ma’am
  3. Write a program to take input color of road traffic signal from the user & show the message according to this table:
  4. Write a program to take input remaining fuel in car (in litres) from user. If the current fuel is less than 0.25litres, show the message “Please refill the fuel in your car”
  5. Run this script, & check whether alert message would be displayed or not. Record the outputs. a. var a = 4; if (++a === 5){ alert("given condition for variable a is true"); }
  6. b. var b = 82; if (b++ === 83){ alert("given condition for variable b is true"); } c. var c = 12; if (c++ === 13){ alert("condition 1 is true"); } if (c === 13){ alert("condition 2 is true"); } if (++c < 14){ alert( "condition 3 is true"); } if(c===1 4){ alert( "condition 4 is true"); } d. var materialCost=2 0000; var laborCost=2 000; var totalCost=m aterialCost + laborCost; if (totalCost===l aborCost + materialCost){ alert( "The cost equals"); } e. if (true){ alert( "True"); } if (false){ alert( "False"); }

    f. if("car" "cat"){ alert( "car is smaller than cat"); }


  7. Guess game: Store a secret number (ranging from 1 to 10) in a variable. Prompt user to guess the secret number.
    1. If user guesses the same number, show “Bingo! Correct answer”
    2. . If the guessed number +1 is the secret number, show “Close enough to the correct answer”.
  8. Write a program to check whether the given number is divisible by 3. Show the message to the user if the number is divisible by 3.
  9. Write a program that checks whether the given input is an even number or an odd number.
  10. Write a program that takes temperature as input and shows a message based on following criteria
    1. T > 40 then “It is too hot outside.”
    2. T > 30 then “The Weather today is Normal.”
    3. T > 20 then “Today’s Weather is cool.”
    4. T > 10 then “OMG! Today’s weather is so Cool.”
  11. Write a program to create a calculator for +,-,*, / & % using if statements. Take the following input:
    1. First number
    2. Second number
    3. Operation (+, -, *, /, %) Compute & show the calculated result to user.

Chapter 12

  1. Write a program that takes a character (number or string) in a variable & checks whether the given input is a number, uppercase letter or lower case letter. (Hint: ASCII codes:- A=65, Z=90, a=97, z=122).

  2. Write a JavaScript program that accept two integers and display the larger. Also show if the two integers are equal.
  3. . Write a program that takes input a number from user & state whether the number is positive, negative or zero.
  4. Write a program that takes a character (i.e. string of length 1) and returns true if it is a vowel, false otherwise
  5. Write a program that
    1. Store correct password in a JS variable.
    2. Asks user to enter his/her password
    3. Validate the two passwords:
    1. Check if user has entered password. If not, then give message “ Please enter your password”
    2. Check if both passwords are same. If they are same, show message “Correct! The password you entered matches the original password”. Show “Incorrect password” otherwise.
  6. This if/else statement does not work. Try to fix it: var greeting; var hour = 13; if (hour < 18) { greeting="Good day" ; else greeting="Good evening" ; }
  7. Write a program that takes time as input from user in 24 hours clock format like: 1900 = 7pm. Implement the following case using if, else & else if statements