# Tasks & Solution of JavaScript Language

#### Output: #### Source Code

console.log(greet)

#### Output: else{

#### Output: #### Source Code

let show=alert("Welcome to JS Land...\r\n Happy Coding");
console.log(show)

#### Output:  #### Source Code

console.log(show+show2)

#### Output: #### Source Code

alert("Hello.. I can run JS through my web browser's console");

## 2. User Input

#### Output: #### Source Code

console.log(print)

#### Output: #### Source Code

let table=prompt("Enter table Number:");
if(table===""){
for(let i=1;i<=10;i++){
console.log("5 x "+ i+ "= "+ (5*i));}}
else{
for(let i=1;i<=10;i++){
console.log(table+" x "+ i+ "= "+ (table*i));}}

#### Output: #### Source Code

let table=prompt("Enter city Name:");
console.log("Welcome to "+ table);
if(table==='karachi'||table==='Karachi'){
console.log("Welcome to city of lights")}
else{
console.log("Welcome to "+ table);}

#### Output: #### Source Code

if(gender==='male'){
else{

#### Output:  #### Source Code

let light=prompt("Enter light color");
if(light==='red'){
else if(light==="yellow"){
else{

#### Output:   #### Source Code

if(curAge<=maxAge){

#### Output:  #### Source Code

var fuel=+prompt("Enter remaining fuel of your car in litres");
var curFuel=0.25;

## 3. Conditional Statements

#### Output: #### Source Code

let num=+prompt("Enter any Number:");
if(num%3===0){
console.log("The given Number is divisible by 3");}
else{
console.log("The given Number is not divisible by 3");}

#### Output: #### Source Code

let num=+prompt("Enter any Number:");
if(num%2===0){
console.log("This is Even Number");}
else{
console.log("This is Odd Number");}

#### Output: #### Source Code

let age=+prompt("Enter age:");
if(age>18){
console.log("Old enough");}
else{
console.log("Too young");}

#### Output: #### Source Code

let num=+prompt("Enter Number:");
switch(num%3){
case 0:
console.log("This Number is divisible by 3");
break;
default:
console.log("This Number is not divisible by 3");}

#### Output: #### Source Code

let num1=+prompt("Enter First Number");
let num2=+prompt("Enter Second Number");
let opt=prompt("Enter Operator");
switch(opt){
case "+":
console.log(`The Addition of \${num1} and \${num2} is \${num1+num2} `);
break;
case "-":
console.log(`The Subtraction of \${num1} and \${num2} is \${num1-num2} `);
break;
case "*":
console.log(`The Multiplication of \${num1} and \${num2} is \${num1*num2} `);
break;
case "/":
console.log(`The Division of \${num1} and \${num2} is \${num1/num2} `);
break;
default:

## 4. Loops

#### Output: #### Source Code

for(let i=1;i<=5;i++){
console.log(i+" Hello World")
}

#### Output: #### Source Code

for(let i=1;i<=10;i++){
console.log(i);
}

#### Output: #### Source Code

let tableNum=+prompt("Enter Table Number");
let times=+prompt("How many times the table would be print?");
for(let i=1;i<=times;i++){
console.log(tableNum+" x "+i+" = "+(tableNum*i));}

#### Output: #### Source Code

let arr= ["Nokia", "Samsung", "Apple", "Sony", "Huawei"];
for(let i=0;i console.log(arr[i])}

## 5. Function

#### HTML Source Code Image #### JavaScript Source Code

var para=document.querySelector
("#paragraph");
function printInfo(){
para.innerHTML= names;}
printInfo();

#### Output: #### HTML Source Code Image #### JavaScript Source Code

var paragraph=document.querySelector
("#paragraph");
function printDate(){
var date=new Date();
paragraph.innerHTML=date;}
printDate();

#### Output: #### HTML Source Code Image #### JavaScript Source Code

var para = document.querySelector
("#paragraph");
function printName() {
var firstName = prompt("Enter First Name");
var lastName = prompt("Enter Last Name");
para.innerHTML="Hello "+firstName+" "+ lastName;}
printName();

#### Output: #### HTML Source Code Image #### JavaScript Source Code

var firstNum = +prompt("Enter first Number");
var secondNum = +prompt("Enter second Number");
var para = document.querySelector
("#paragraph")
function sum(num1, num2) {
console.log("The sum of two numbers are " + (num1 + num2));
para.innerHTML = "The sum of two numbers are " + (num1 + num2);}
sum(firstNum, secondNum);

#### Output: #### HTML Source Code Image #### JavaScript Source Code

var firstNum = +prompt("Enter first Number");
var secondNum = +prompt("Enter second Number");
var opr = prompt("Enter any operator (+,-,*,/)");
var para = document.querySelector
("#paragraph");
function sum(num1, num2, operator) {
if (operator === "+") {
console.log("The sum of two numbers are " + (num1 + num2));
para.innerHTML = "The sum of two numbers are " + (num1 + num2);}
else if (operator === "-") {
console.log("The subtract of two numbers are " + (num1 - num2));
para.innerHTML = "The subtract of two numbers are " + (num1 - num2);}
else if (operator === "*") {
console.log("The multiply of two numbers are " + (num1 * num2));
para.innerHTML = "The multiply of two numbers are " + (num1 * num2);}
else if (operator === "/") {
console.log("The division of two numbers are " + (num1 / num2));
para.innerHTML = "The division of two numbers are " + (num1 / num2);}
else{
sum(firstNum,secondNum,opr);

#### Output: #### HTML Source Code Image #### JavaScript Source Code

var para = document.querySelector
("#paragraph");
var num = +prompt("Enter any number for squaring");
function square(num) {
console.log("The square of " + num +
" is " + (num * num));
para.innerHTML = "The square of " + num + " is " + (num * num);}
square(num);

#### Output: #### HTML Source Code Image #### JavaScript Source Code

var para = document.querySelector
("#paragraph");
var num = +prompt("Enter any number for foctorial");
function fact(num) {
var fact = 1
for (var i = 1; i <= num; i++) {
fact = fact * i;}
console.log("The factorial of " + num + " is " + fact);
para.innerHTML = "The square of " + num + " is " + fact;}
fact(num);

#### Output: #### HTML Source Code Image #### JavaScript Source Code

var para=document.getElementById
("paragraph")
var numStart=+prompt("Enter starting number for display counting");
var numEnd=+prompt("Enter ending number for display counting");
for(var i=numStart;i<=numEnd;i++){
console.log(i); }

#### Output: #### HTML Source Code Image #### JavaScript Source Code

var para=document.getElementById
("paragraph")
var base=+prompt("Enter base of right angled triangle.");
var perp=+prompt("Enter perpendicular of right angled triangle.");
var baseSquare=base*base;
var perpSquare=perp*perp;
var hypo=baseSquare+perpSquare;
var hypothesis=Math.sqrt(hypo);
console.log("The Hypothesis is "+hypothesis);
para.innerHTML="The Hypothesis is "+hypothesis;

#### Output: ## 6. Array

#### Output #### JavaScript Source Code

let studentNames=[];
studentNames.push("Shoaib",'Akhter');
console.log(studentNames)

#### Output #### JavaScript Source Code

let studentNames=[];
studentNames.push("Shoaib",'Akhter');
console.log(studentNames)

#### Output #### JavaScript Source Code

let numbers=[2,3,4];
numbers.unshift(0,1);
console.log(numbers)

#### Output #### JavaScript Source Code

let boolean=[true,false];
console.log(boolean)

#### Output #### JavaScript Source Code

let mixedArray=[0,1,2,"Shoaib","Akhter",true,false];
console.log(mixedArray)

#### Output #### JavaScript Source Code

let network=["Zong","Ufone","Jazz","Warid"];
console.log(network)

#### Output #### JavaScript Source Code

let education=["SSC", "HSC"," BCS"," BS"," BCOM"," MS","M. Phil","PhD"];
console.log(education)

#### Output #### JavaScript Source Code

let education=["Bahria Uni","Fast Uni","Al-Madinah Uni","NED Uni"];
console.log(education.length)
console.log(education)

#### Output #### JavaScript Source Code

let car=["Corola","Passo","Mehran","Civic"];
console.log(car)
console.log("The car at first index is "+car)
console.log("The car at last index is "+car)

## 7. Date & Time

#### HTML Source Code Image #### JavaScript Source Code

var paragraph=document.querySelector
("#paragraph");
var currentDate=new Date();
paragraph.innerHTML= currentDate;

#### Output: #### HTML Source Code Image #### JavaScript Source Code

var paragraph=document.querySelector
("#paragraph");
var monthName=["Januray","February","March","April","May","June","July",
"August","September","Octuber","November",
"December"];
var currentMonth= new Date();
paragraph.innerHTML="Current Month: "+monthName[currentMonth.getMonth()];

#### Output: #### HTML Source Code Image #### JavaScript Source Code

var paragraph=document.querySelector
("#paragraph");
var dayName=['Sunday','Monday','Tuesday','Wednesday','Thursday',
'Friday','Saturday'];
var currentDay= new Date();
paragraph.innerHTML="Today is "+dayName[currentDay.getDay()];

#### Output: #### HTML Source Code Image #### JavaScript Source Code

var paragraph=document.querySelector
("#paragraph");
var currentDate= new Date();
var date=currentDate.getDate();
if(date<=15){
paragraph.innerHTML="First fifteen days of the month";} else{
paragraph.innerHTML="Last fifteen days of the month";}

#### Output: #### HTML Source Code Image #### JavaScript Source Code

var paragraph=document.querySelector
("#paragraph");
var currentDay= new Date();
var day=currentDay.getDay();
if(day==0 || day==6){
paragraph.innerHTML="It's Fun day";}
else{
paragraph.innerHTML="It's not a Fun day";}

#### Output: #### HTML Source Code Image #### JavaScript Source Code

var paragraph=document.querySelector
("#paragraph");
var paragraph1=document.querySelector
("#paragraph1");
var paragraph2=document.querySelector
("#paragraph2");
var currentDate=new Date();
var previousDate=new Date("January 01,1970");
var currentMinutes=currentDate/1000;
var previousMinutes=previousDate/1000;
paragraph.innerHTML="Current Date: "+ currentDate;
var Elapsed=currentDate-previousDate;
var remainingMiliseconds=currentMinutes-previousMinutes;
paragraph1.innerHTML="Elapsed milliseconds since June 4, 2022: "+ Elapsed;
paragraph2.innerHTML="Elapsed minutes since July 4,2022: "+remainingMiliseconds;

#### Output: #### HTML Source Code Image #### JavaScript Source Code

var paragraph=document.querySelector
("#paragraph");
var laterDate=new Date("December 31,2020");
paragraph.innerHTML="Later Date: "+laterDate;

#### Output: ## 7. Number Methods

#### HTML Source Code Image #### JavaScript Source Code

var paragraph1=document.querySelector
("#paragraph1");
var paragraph2=document.querySelector
("#paragraph2");
var paragraph3=document.querySelector
("#paragraph3");
var paragraph4=document.querySelector
("#paragraph4");
var num=prompt("Enter any Number");
paragraph1.innerHTML="The Number is " + num;
paragraph2.innerHTML="The round off value of Number is " +Math.round(num);
paragraph3.innerHTML="The Floor value of Number is " + Math.floor(num);
paragraph4.innerHTML="The Ceil value of Number is " + Math.ceil(num);

#### Output: #### HTML Source Code Image #### JavaScript Source Code

var paragraph1=document.querySelector
("#paragraph1");
var paragraph2=document.querySelector
("#paragraph2");
var paragraph3=document.querySelector
("#paragraph3");
var paragraph4=document.querySelector
("#paragraph4");
var num=prompt("Enter any Negative Number");
paragraph1.innerHTML="The Number is " + num;
paragraph2.innerHTML="The round off value of Number is " +Math.round(num);
paragraph3.innerHTML="The Floor value of Number is " + Math.floor(num);
paragraph4.innerHTML="The Ceil value of Number is " + Math.ceil(num);

#### Output: #### HTML Source Code Image #### JavaScript Source Code

var paragraph1=document.querySelector
("#paragraph1");
var paragraph2=document.querySelector
("#paragraph2");
var paragraph3=document.querySelector
("#paragraph3");
var paragraph4=document.querySelector
("#paragraph4");
var num=prompt("Enter any floating point number");
paragraph1.innerHTML="The Floating Number is "+ num;
paragraph2.innerHTML="The Round off value of Floating Number is "+ Math.round(num);
paragraph3.innerHTML="The Floor value of Floating Number is "+ Math.floor(num);
paragraph4.innerHTML="The Ceil value of Floating Number is "+ Math.ceil(num);

#### Output: #### HTML Source Code Image #### JavaScript Source Code

var paragraph1=document.querySelector
("#paragraph1");
var paragraph2=document.querySelector
("#paragraph2");
var paragraph3=document.querySelector
("#paragraph3");
var paragraph4=document.querySelector
("#paragraph4");
var num=prompt("Enter any Negative floating point number");
paragraph1.innerHTML="The Floating Number is "+ num;
paragraph2.innerHTML="The Round off value of Floating Number is "+ Math.round(num);
paragraph3.innerHTML="The Floor value of Floating Number is "+ Math.floor(num);
paragraph4.innerHTML="The Ceil value of Floating Number is "+ Math.ceil(num);

#### Output: #### HTML Source Code Image #### JavaScript Source Code

var paragraph=document.querySelector
("#paragraph")
var num=prompt("Enter any number for checking absolute");
if(num<0){
paragraph.innerHTML="The absolute value of " + num +" is "+ (num*(-1));
}
else{
paragraph.innerHTML="The absolute value of " + num +" is "+ num;
}

#### Output: #### HTML Source Code Image #### JavaScript Source Code

var paragraph=document.querySelector
("#paragraph");
paragraph.innerHTML="The Random dice value is "+ (Math.ceil(Math.random()*6));

#### Output: #### HTML Source Code Image #### JavaScript Source Code

var paragraph1=document.querySelector
("#paragraph1");
var paragraph=document.querySelector
("#paragraph");
var toss=Math.ceil(Math.random()*2);
paragraph.innerHTML=toss;
if(toss===1){
paragraph1.innerHTML=1;
paragraph.innerHTML="The Random coin value: Tails";}
else{
paragraph1.innerHTML=2;

#### Output: #### HTML Source Code Image #### JavaScript Source Code

var paragraph=document.querySelector
("#paragraph");
paragraph.innerHTML="The Random Number between 1 and 100: "+ Math.ceil(Math.random()*100);

#### Output: #### HTML Source Code Image #### JavaScript Source Code

var paragraph=document.querySelector
("#paragraph");
var num=+prompt("Enter any Number from 1 to 10");
var myNum=5;
if(num===myNum){
paragraph.innerHTML="Congratulation! You won the match";}
else{

#### Output: ## 9. String Methods

#### Output: #### JavaScript Source Code

let fullName=firstName+lastName;

#### Output: #### JavaScript Source Code

let names=prompt("Enter Mobile Phone Model");
alert("The length of Mobile phone model is "+names.length);

#### Output: #### JavaScript Source Code

let indexNumber="Pakistan";
for(let i=0;i if(indexNumber[i]=="n"){
alert("The index number of n is "+i);
document.write("The index number of n is "+i)}}

#### Output: #### JavaScript Source Code

let word="Hello World";
alert("The index of last L is "+word.lastIndexOf("l"));

#### Output: #### JavaScript Source Code

let word="Pakistan";
alert("The third index in Pakistan name is "+word);

## 12. Reduce Method

##### Output: #### Source Code:

let numbers=[1,2,3,4];
let sum=numbers.reduce((accum,item)=>{
return accum+item
},0);
console.log(sum);

##### Output: #### Source Code:

let num=[5,2,4,6,9,1];
let product=num.reduce((accum,item)=>{
return accum*item
},1);
console.log(product)

##### Output: #### Source Code:

let pets=['dog','cat','chicken','dog','chicken','rabit'];
let petscount=pets.reduce((accum,item)=>{
if(accum[item]){
accum[item]++;}
else{
accum[item]=1;}
return accum;
},{})
console.log(petscount);

##### Output: #### Source Code:

let num=[5000,6000,81,10,112,99999, 2030];
let large=num.reduce((accum,item)=>{
if(accum < item ) {
return item;}
else{
return accum}
},1)
console.log(large)

## 13. Classes

##### Output: #### Source Code:

class Animal{
constructor(name,legs,color){
this.name=name;
this.legs=legs;
this.color=color;}
eat(){
console.log(`\${this.name} eats bone`);}}
let anim=new Animal("Dogs",4,"White");
console.log(anim)
anim.eat()

##### Output: #### Source Code:

class Animal{
constructor(name,legs){
this.name=name;
this.legs=legs;}
speed1(name,name1){
console.log(name+` runs `+name1)}}
class Rabit extends Animal{
constructor(name,legs,speed,quality){
super(name,legs);
this.speed=speed;
this.quality=quality;}}
class Turtle extends Animal{
constructor(name,legs,quality,specialPower){
super(name,legs);
this.quality=quality;
this.specialPower=specialPower;}}
let rabit=new Rabit("rabit",4,"fast",'hide');
let turtle=new Turtle("turtle",4,"consistent",'hard shell');
console.log(rabit,turtle)
rabit.speed1("Rabit","Fast");
turtle.speed1("Turtle","Slow")