Skip to content

AlizayAyesha/JavaScript-assignment-2

Repository files navigation

js-assigment-1


Note: Check Rules end of document

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

index.html

<title>Js sample</title> <script src=" index.js"></script>
    </body>
</head>

index.js

alert("Welcome To Js Land...\n Happy Coding!")

image


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

index.html

<title>Js sample</title> <script src=" index.js"></script>
    </body>
</head>

index.js

var a = "Alizay"

var b =" 21 years old"

var c = "Certified Mobile Application Development"

alert(a) alert(b) alert(c)

image image image


3. Declare a variable called email and assign to it a string that represents your Email Address(e.g. [email protected]). Show the below mentioned message in an alert box.(Hint: use string concatenation)

index.html

<title>Js sample</title> <script src=" index.js"></script>
    </body>
</head>

index.js

var email = "My Email address is [email protected]" alert(email) console.log(email) image


4. Write a script to display this in browser through JS

index.html

<title>Js sample</title> <script src=" index.js"></script> Hello !
How are you?
Yah! I can write HTML content through JavaScript ![image](https://github.com/AlizayAyesha/js-assigment-1/assets/68489612/08e245ab-346f-41ac-b934-f15ddd00a662)

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

index.html

<title>Js sample</title> <script src=" index.js"></script>
    </body>
</head>

inex.js

alert("I am 21 years old")

image

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

index.html

<title>Js sample</title> <script src=" index.js"></script> My Birth Year is 2003
data type of my decleared variable is number

image


7. Display this in your browser a. A heading stating “Rules for naming JS variables” b. Variable names can only contain ______, ______, c. ______ and ______. d. For example $my_1stVariable. e. Variables must begin with a ______, ______ or f. _____. For example $name, _name or name g. Variable names are case _________ h. Variable names should not be JS _________

index.html

<title>Js sample</title> <script src=" index.js"></script>

Rules For Naming variables in JavaScript

1- Variable names can only contain a letter, an underscore ( _ ) or a dollar sign ( $ ) and Variable names cannot contain spaces.
For example : $my_1stVariable.
2- Variables must begin with a a letter or an underscore character (_) A variable name cannot start with a digit. A variable name can only contain alpha-numeric characters and underscores ( a-z, A-Z , 0-9 , and _ ) Variable names are case-sensitive (age, Age and AGE are three different variables)
For example : $name, _name or name
3- Variable names are case sensitive.
For example, the while keywords must be typed “while”, not “While” or “WHILE”
4- Variable names should not be JS keyword
For example it cannot be the same as reserved keywords such as if or const it can only process camelCase e.g (e.g. sellingPrice and costPrice rather than x and y )

    </body>
</head>

image


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

index.html

<title>Js sample</title> <script src=" index.js"></script> Sum of a and b is 4

index.js

var a = 1 var b = 3

console.log(a + b )

image


9. Repeat task 8 for subtraction, multiplication, division & modulus.

index.html

<title>Js sample</title> <script src=" index.js"></script> a = 1
b = 2
c = sum of a + b is 3
d = sum of a - b is -1
e = sum of a * b is 2
f = sum of a % b is 1
g = sum of a / b is 0.5
h = sum of a ** b is 1
    </b>
    </body>
</head>

index.js

var a = 1 var b = 2 var c = a + b //add result 3 var d = a - b //sub result 1 var e = a * b //mul result 2 var f = a % b //module result 1 var g = a / b //division result 0.5 var h = a ** b //exponentiation result 1

console.log(c) console.log(d) console.log(e) console.log(f) console.log(g) console.log(h)

image


10. Do the following using JS Mathematic Expressions a. Declare a variable. b. Show the value of variable in your browser like “Value after variable declaration is: ??”. c. Initialize the variable with some number. d. Show the value of variable in your browser like “Initial value: 5”. e. Increment the variable. f. Show the value of variable in your browser like “Value after increment is: 6”. g. Add 7 to the variable. h. Show the value of variable in your browser like “Value after addition is: 13”. i. Decrement the variable. j. Show the value of variable in your browser like “Value after decrement is: 12”. k. Show the remainder after dividing the variable’s value by 3. l. Output : “The remainder is : 0”.

index.html

<title>Js sample</title> <script src=" index.js"></script> Value after variable declaration is undefined
Initializing the variable with some number.
Initial value : 5
Increment the variable.
Value after increment is: 6
Add 7 to the variable.
Value after addition is :13
Decrement the variable.
Value after decrement is: 12
Remainder
Output : “The remainder is : 0”.
    </body>
</head>

index.js

var value1 console.log(value1) var value2 = 5 console.log(value2) var value3 = value2++ console.log(value2) var value4 = value2 + 7 console.log(value4) var value5 = --value4 console.log(value5) var value6 = value5 / 3 console.log(value6)

image


**11. The Temperature Converter: It’s hot out! Let’s make a converter based on the steps here. a. Store a Celsius temperature into a variable. b. Convert it to Fahrenheit & output “N oC is N oF”. c. Now store a Fahrenheit temperature into a variable. d. Convert it to Celsius & output “N oF is N oC”.

Conversion Formulae**

index.html

<title>Js sample</title> <script src=" index.js"></script> 30°C is 86°F
      86°F is 30°C
    </body>
</head>

index.js

var celsius = 30; var fahrenheit = (celsius * 9/5) + 32; console.log(celsius + "°C is " + fahrenheit + "°F");

var fahrenheit2 = 86; var celsius2 = (fahrenheit2 - 32) * 5/9; console.log(fahrenheit2 + "°F is " + celsius2 + "°C");

image


12. 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 = 155 Pakistani Rupee and 1 Saudi Riyal = 41 Pakistani Rupee)

index.html

<title>Js sample</title> <script src=" index.js"></script> Currency in PKR


Total Currency in PKR : 2,575

index.js

var us_dollar = 10 console.log(us_dollar) var saudi_riyal = 25 console.log(saudi_riyal)

var usd_to_pkr = 155 console.log(usd_to_pkr) var sar_to_pkr = 41 console.log(sar_to_pkr)

var totalPkr = (us_dollar * usd_to_pkr) + (saudi_riyal * sar_to_pkr) console.log(totalPkr)

image


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

index.html

<title>Js sample</title> <script src=" index.js"></script> Result:
The value of 'a' is 10
----------------------

The value of '++a' is : 11
Now the of 'a' is : 11


The value of 'a++' is : 11
Now the value of 'a' is : 12


The value of '--a' is : 11
Now the value of 'a' is :11


The value of 'a--' is : 11
Now the value of 'a' is : 10
    </body>
</head>

index.js

var a = 10 console.log(a)

var b = ++a console.log(b)

var a = 11 console.log(a)

var c = a++ console.log(c)

var a = 12 console.log(a)

var d = --a; console.log(d)

var a = 11 console.log(a)

var e = a-- console.log(e)

var a = 10 console.log(a)

image


14. 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--;

index.html

<title>Js sample</title> <script src=" index.js"></script> a = 2
b = 1
result = 3

index.js

var a = 2, b = 1; console.log(a) var result = --a - --b + ++b + b--; console.log(result)

image