Skip to content

Commit 91ff888

Browse files
done
1 parent 4b00882 commit 91ff888

File tree

17 files changed

+1060
-53
lines changed

17 files changed

+1060
-53
lines changed

chapter31 to 34/script.js

Lines changed: 85 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -178,21 +178,96 @@ document.write(
178178
`<h1> Elapsed between the reference date and the beginning of 2015 </h1>`
179179
);
180180
function displayElapsedSeconds() {
181-
// Create a Date object for the beginning of 2015
182-
let referenceDate = new Date(2015, 0, 1); // January is 0-indexed
181+
// Create a Date object for the beginning of 2015
182+
let referenceDate = new Date(2015, 0, 1); // January is 0-indexed
183183

184-
// Get the current date
185-
let currentDate = new Date();
184+
// Get the current date
185+
let currentDate = new Date();
186186

187-
// Calculate the difference in milliseconds
188-
let differenceInMilliseconds = currentDate - referenceDate;
187+
// Calculate the difference in milliseconds
188+
let differenceInMilliseconds = currentDate - referenceDate;
189189

190-
// Convert milliseconds to seconds
191-
let secondsElapsed = Math.floor(differenceInMilliseconds / 1000);
190+
// Convert milliseconds to seconds
191+
let secondsElapsed = Math.floor(differenceInMilliseconds / 1000);
192192

193-
// Display the result in the browser
194-
document.write(`Seconds elapsed since the beginning of 2015: ${secondsElapsed}`)
193+
// Display the result in the browser
194+
document.write(
195+
`Seconds elapsed since the beginning of 2015: ${secondsElapsed}`
196+
);
195197
}
196198

197199
// Call the function to execute it
198200
displayElapsedSeconds();
201+
202+
// Create a Date object for the current date and time.
203+
// Extract the hours, reset the date object an hour ahead and
204+
// finally display the date object in your browser.
205+
document.write(
206+
`<h1> Date Object </h1>`
207+
);
208+
let curDate = new Date();
209+
// console.log(curDate);
210+
let getHours = curDate.getHours();
211+
// console.log(getHours);
212+
curDate.setHours(getHours + 1);
213+
alert(`Update date : ${curDate}`);
214+
document.write(`Update date : ${curDate} <br><br>`);
215+
216+
// Write a program that creates a date object and show the
217+
// date in an alert box that is reset to 100 years back?
218+
document.write(
219+
`<h1> Date 100 Years Back </h1>`
220+
);
221+
let currenDate = new Date();
222+
let hundredYearsAgo = new Date(
223+
currenDate.setFullYear(currenDate.getFullYear() - 100)
224+
);
225+
document.write(`100 years ago : ${hundredYearsAgo} <br><br>`);
226+
alert(`100 years ago : ${hundredYearsAgo}`);
227+
228+
// Write a program to ask the user about his age. Calculate
229+
// and show his birth year in your browser.
230+
document.write(
231+
`<h1> Birth Year </h1>`
232+
);
233+
let userAge = prompt("What is your age?");
234+
document.write(`Your Age is ${userAge} <br>`);
235+
let currentYear = new Date().getFullYear();
236+
console.log(currentYear);
237+
let birthYear = currentYear - userAge;
238+
console.log(birthYear);
239+
document.write(`Your Birth Year is ${birthYear} <br><br>`);
240+
alert(`Your Birth Year is ${birthYear}`);
241+
242+
// Write a program to generate your K-Electric bill in your
243+
// browser. All the amounts should be rounded off to 2
244+
// decimal places. Display the following fields:
245+
// a. Customer Name
246+
// b. Current Month
247+
// c. Number of units
248+
// d. Charges per unit
249+
// e. Net Amount Payable (within Due Date)
250+
// f. Late Payment Surcharge
251+
// g. Gross Amount Payable (after Due Date)
252+
// Where,
253+
// Net Amount Payable (within Due Date) = Number of units * Charges per unit
254+
// & Gross Amount Payable (after Due Date) = Net Amount + Late Payment Surcharge
255+
document.write(
256+
`<h1> K-Electric Bill </h1>`
257+
);
258+
let customerName = prompt("Customer Name");
259+
let currMonth = new Date().toLocaleString("default", { month: "long" });
260+
let unitConsumed = 200;
261+
let chargePerUnit = 50;
262+
let netAmountPayable = unitConsumed * chargePerUnit;
263+
let latePaymentSurcharge = 70;
264+
let grossAmountPayable = netAmountPayable + latePaymentSurcharge;
265+
266+
document.write(`
267+
Customer Name : ${customerName}<br>
268+
Current Month : ${currMonth}<br>
269+
Number of Units : ${unitConsumed}<br>
270+
Charges Per Unit : ${chargePerUnit.toFixed(2)}<br><br>
271+
Net Amount Payable (Within Due Date) : ${netAmountPayable}<br>
272+
Gross Amount Payable (After Due Date) : ${grossAmountPayable.toFixed(2)}<br><br>
273+
`);

chapter35 to 38/script.js

Lines changed: 189 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -13,72 +13,218 @@ currDate();
1313

1414
document.write(`<h1> Greets to User </h1>`);
1515

16-
// function greet (firstName , lastName){
17-
// firstName = prompt("Enter First Name");
18-
// lastName= prompt("Enter Last Name");
16+
function greet(firstName, lastName) {
17+
firstName = prompt("Enter First Name");
18+
lastName = prompt("Enter Last Name");
1919

20-
// document.write(`Good morning ${firstName} ${lastName}`)
21-
// }
22-
// greet();
20+
document.write(`Good morning ${firstName} ${lastName}`);
21+
}
22+
greet();
2323

2424
// Write a function that adds two numbers (input by user)
2525
// and returns the sum of two numbers.
2626
document.write(`<h1> Adds two numbers </h1>`);
27-
// function sum(num1, num2) {
28-
// num1 = Number(prompt("Enter First Number"));
29-
// num2 = Number(prompt("Enter Second Number"));
30-
// document.write(`Sum of two numbers: ${num1 + num2}`);
31-
// }
32-
// sum();
27+
function sum(num1, num2) {
28+
num1 = Number(prompt("Enter First Number"));
29+
num2 = Number(prompt("Enter Second Number"));
30+
document.write(`Sum of two numbers: ${num1 + num2}`);
31+
}
32+
sum();
3333

3434
// Write a function that takes three arguments num1, num2
3535
// & operator & compute the desired operation. Return and
3636
// show the desired result in your browser.
3737

3838
document.write(`<h1> Calculator </h1>`);
3939

40-
// function calculator (numb1, numb2, operator){
41-
// numb1 = Number(prompt("Enter First Number"));
42-
// numb2 = Number(prompt("Enter Second Number"));
43-
// operator = prompt("Enter the operator");
44-
45-
// if(operator=== "+"){
46-
// document.write(`Addition: ${numb1+numb2}`);
47-
// }
48-
// else if(operator=== "-"){
49-
// document.write(`Subtarction: ${numb1-numb2}`);
50-
// }
51-
// else if(operator=== "*"){
52-
// document.write(`Multiplication: ${numb1*numb2}`);
53-
// }
54-
// else if(operator=== "/"){
55-
// document.write(`Division: ${numb1/numb2}`);
56-
// }
57-
// }
58-
// calculator();
40+
function calculator(numb1, numb2, operator) {
41+
numb1 = Number(prompt("Enter First Number"));
42+
numb2 = Number(prompt("Enter Second Number"));
43+
operator = prompt("Enter the operator");
44+
45+
if (operator === "+") {
46+
document.write(`Addition: ${numb1 + numb2}`);
47+
} else if (operator === "-") {
48+
document.write(`Subtarction: ${numb1 - numb2}`);
49+
} else if (operator === "*") {
50+
document.write(`Multiplication: ${numb1 * numb2}`);
51+
} else if (operator === "/") {
52+
document.write(`Division: ${numb1 / numb2}`);
53+
}
54+
}
55+
calculator();
5956

6057
// Write a function that squares its argument.
6158
document.write(`<h1> Squares </h1>`);
62-
// function square(num) {
63-
// num = prompt("Enter a number");
64-
// let square = num ** 2;
65-
// document.write(`Square of ${num} is ${square}`);
66-
// }
67-
// square();
59+
function square(num) {
60+
num = prompt("Enter a number");
61+
let square = num ** 2;
62+
document.write(`Square of ${num} is ${square}`);
63+
}
64+
square();
6865

69-
document.write(`<h1> Factorial of a Number </h1>`);
7066
// Write a function that computes factorial of a number.
67+
document.write(`<h1> Factorial of a Number </h1>`);
68+
69+
function factorial(num) {
70+
// num = prompt("Enter a number");
71+
if (num === 0 || num === 1) {
72+
// document.write(1)
73+
return 1;
74+
} else {
75+
// document.write(num * factorial(num - 1))
76+
return num * factorial(num - 1);
77+
}
78+
}
79+
document.write(factorial(5));
80+
console.log(factorial(5));
7181

7282
// Write a function that take start and end number as inputs
7383
// & display counting in your browser.
7484
document.write(`<h1> Counting </h1>`);
7585
function counting(start, end) {
7686
start = Number(prompt("Enter start number"));
7787
end = Number(prompt("Enter end number"));
78-
for(let i = start; i<= end; i++){
79-
let count ="";
80-
count += i;
81-
document.write(`${count}</br>`);
88+
for (let i = start; i <= end; i++) {
89+
// let count ="";
90+
// count += i;
91+
document.write(`${i}</br>`);
8292
}
8393
}
84-
counting()
94+
counting();
95+
96+
// Write a nested function that computes hypotenuse of a
97+
// right angle triangle.
98+
// Hypotenuse2 = Base2 + Perpendicular2
99+
// Take base and perpendicular as inputs.
100+
// Outer function : calculateHypotenuse()
101+
// Inner function: calculateSquare()
102+
103+
document.write(`<h1> Calculate Hypotenuse </h1>`);
104+
105+
function calculateHypotenuse(base, perpendicular) {
106+
function calcSquare(num) {
107+
return num * num;
108+
}
109+
let baseSquare = calcSquare(base);
110+
let perpendicularSquare = calcSquare(perpendicular);
111+
let hypotenuse = Math.sqrt(baseSquare + perpendicularSquare);
112+
return hypotenuse;
113+
}
114+
document.write(calculateHypotenuse(5, 6));
115+
116+
// Write a function that calculates the area of a rectangle.
117+
// A = width * height
118+
// Pass width and height in following manner:
119+
// i. Arguments as value
120+
// ii. Arguments as variables
121+
122+
document.write(`<h1> Area of a Rectangle (Arguments as value) </h1>`);
123+
124+
function calcAreaOfRectangle(width, height) {
125+
return width * height;
126+
}
127+
document.write(calcAreaOfRectangle(8, 10));
128+
129+
document.write(`<h1> Area of a Rectangle (Arguments as variables) </h1>`);
130+
let width = 8;
131+
let height = 15;
132+
document.write(calcAreaOfRectangle(width, height));
133+
134+
// Write a JavaScript function that checks whether a passed
135+
// string is palindrome or not?
136+
// A palindrome is word, phrase, or sequence that reads the same backward as
137+
// forward, e.g., madam.
138+
document.write(`<h1> String is Palindrome or not? </h1>`);
139+
function isPalindrome(str) {
140+
let reversed = str.split("").reverse().join("");
141+
return str === reversed;
142+
}
143+
document.write(`${isPalindrome("madam")}<br>`);
144+
145+
document.write(`${isPalindrome("eye")} <br>`);
146+
document.write(`${isPalindrome("ear")} <br>`);
147+
document.write(`${isPalindrome("son")} <br>`);
148+
document.write(`${isPalindrome("teeth")} <br>`);
149+
document.write(`${isPalindrome("teeth")} <br>`);
150+
151+
// Write a JavaScript function that accepts a string as a
152+
// parameter and converts the first letter of each word of the
153+
// string in upper case.
154+
// EXAMPLE STRING : 'the quick brown fox'
155+
// EXPECTED OUTPUT : 'The Quick Brown Fox'
156+
157+
document.write(`<h1> Capitalize </h1>`);
158+
function capitalizeWord(words) {
159+
return words
160+
.split(" ")
161+
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
162+
.join(" ");
163+
}
164+
document.write(capitalizeWord(`sadaf shahab <br>`));
165+
document.write(capitalizeWord(`the quick brown fox <br>`));
166+
167+
// Write a JavaScript function that accepts a string as a
168+
// parameter and find the longest word within the string.
169+
// EXAMPLE STRING : 'Web Development Tutorial'
170+
// EXPECTED OUTPUT : 'Development'
171+
document.write(`<h1> Largest Word </h1>`);
172+
173+
function findLargestWord(largeWord) {
174+
let words = largeWord.split(" ");
175+
let longestWord = words.reduce((longest, current) => {
176+
return current.length > longest.length ? current : longest;
177+
}, "");
178+
return longestWord;
179+
}
180+
document.write(findLargestWord("Web Development Tutorial"));
181+
182+
// Write a JavaScript function that accepts two arguments, a
183+
// string and a letter and the function will count the number of
184+
// occurrences of the specified letter within the string.
185+
// Sample arguments : 'JSResourceS.com', 'o'
186+
187+
document.write(`<h1> Count occurrences of a specific letter </h1>`);
188+
189+
function countLetterOccurr(word, letter) {
190+
let count = 0;
191+
for (let i = 0; i < word.length; i++) {
192+
if (word[i] === letter) {
193+
count++;
194+
}
195+
}
196+
return count;
197+
}
198+
document.write(countLetterOccurr("Circumference", "e"));
199+
200+
// 14. The Geometrizer
201+
// Create 2 functions that calculate properties of a circle, using
202+
// the definitions here.
203+
// Create a function called calcCircumference:
204+
// • Pass the radius to the function.
205+
// • Calculate the circumference based on the radius, and output
206+
// "The circumference is NN".
207+
// Create a function called calcArea:
208+
// • Pass the radius to the function.
209+
// • Calculate the area based on the radius, and output "The area
210+
// is NN".
211+
// Circumference of circle = 2πr
212+
// Area of circle
213+
// =
214+
// πr2
215+
216+
document.write(`<h1> Properties of a Circle </h1>`);
217+
218+
document.write(`<h4> Circumference of a Circle </h4>`);
219+
220+
function circumference(radius) {
221+
return `The Circumference is ${2 * Math.PI * radius}`;
222+
}
223+
document.write(`${circumference(7)}`);
224+
225+
document.write(`<h4> Area of a Circle </h4>`);
226+
227+
function area(radius) {
228+
return `The Area is ${Math.PI * radius * radius}`;
229+
}
230+
document.write(`${area(2)}`);

chapter38 to 42/index.html

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<title>Chapter 38 to 42 Task | FUNCTION </title>
7+
<link rel="stylesheet" href="style.css" />
8+
</head>
9+
<body>
10+
<script src="script.js"></script>
11+
</body>
12+
</html>

0 commit comments

Comments
 (0)