30Days30JavaScript Challenge by Hitesh Sir
Table of contents
- Day 1: Variables and Data Types by
- DAY 2: Operators
- DAY3: JavaScript Control Structures Practice
- JavaScript Day 4 Loops by Hitesh Sir
Day 1: Variables and Data Types by
Activity 1: Variable Declaration
Task 1: Declaring with var
var num = 11;
console.log("Variable declared using var: " + num);
Uses
var
to declare a variableAssigns a number value
Demonstrates basic console logging
Task 2: Declaring with let
let name1 = "Ankit";
console.log("Variable declared using let: " + name1);
Uses
let
for variable declarationAssigns a string value
Shows difference in declaration between
var
andlet
Activity 2: Constant Declaration
Task 3: Declaring with const
const human = true;
console.log("Variable declared using const: " + human);
Introduces
const
for constant valuesAssigns a boolean value
Demonstrates that
const
is used for values that shouldn't change
Activity 3: Data Types
Task 4: Various Data Types
var age = 10;
var twitter = "ankitmishraexe";
var placed = false;
const details_of_person = {
name_of_person: "Ankit Mishra",
age: 22,
college: "CGC",
};
const arr = ["ankit", "mishra", "is", 22, "years", "old", true];
Demonstrates different data types: number, string, boolean, object, array
Uses
typeof
operator to check data typesShows how to create and use arrays and objects
Activity 4: Reassigning Variables
Task 5: Reassigning let
Variables
let number1 = 22;
console.log("number1 is: " + number1);
number1 = 23;
console.log("number1 is: " + number1);
Shows that variables declared with
let
can be reassignedDemonstrates value changing over time
Activity 5: Understanding const
Task 6: Attempting to Reassign const
const number2 = 22;
number2 = 23; This would cause an error
Illustrates that
const
variables cannot be reassignedCode is commented out to prevent runtime errors
Feature Requests
1. Variable Types Console Log
var company = "Google";
let year = 2024;
var permanent = true;
const login_credentials = {
uername: "Ankitthegoat",
team_id: 23553,
location: "Hyderabad",
passKey: 43873892,
};
for (key in login_credentials) {
var value = login_credentials[key];
console.log(key, value, typeof value);
}
const completeDetail = [company, year, permanent];
completeDetail.forEach((e) => {
console.log("Value: " + e + " typeOfValue: " + typeof e);
});
Declares variables of different types
Uses object to store multiple related values
Demonstrates iterating over object properties
Shows use of arrays and the
forEach
methodIllustrates how to log both value and type of variables
2. Reassignment Demo
var password = 123498623;
console.log("Password: " + password);
password = 1234423;
console.log("Password reassigned : " + password);
const username = "@AnkitMishraexe";
console.log("Username of Twitter is : " + username);
username = "@Ankit.exe"; // This line will cause an error
console.log("Username of Twitter is changed : " + username);
Demonstrates reassignment of
var
variablesAttempts to reassign a
const
variable, which will cause an errorIllustrates the difference between
var
/let
andconst
in terms of reassignment
Key Concepts
Variable Declaration: Using
var
,let
, andconst
to declare variables.Data Types: Understanding different data types in JavaScript (number, string, boolean, object, array).
Type Checking: Using the
typeof
operator to determine variable types.Objects: Creating and accessing object properties.
Arrays: Creating and manipulating arrays.
Loops: Using
for...in
loops andforEach
method for iteration.Reassignment: Understanding which variables can be reassigned (
var
,let
) and which cannot (const
).
Output
DAY 2: Operators
Welcome to Day 2 of our JavaScript learning journey from Hitesh sir! Today, we're diving into the world of operators. Operators are essential in programming as they allow us to perform operations on variables and values. Let's explore the different types of operators in JavaScript.
1. Arithmetic Operators
Arithmetic operators are used for mathematical calculations. Here are the basic ones:
Addition (+)
Subtraction (-)
Multiplication (*)
Division (/)
Remainder (%)
Example:
function add(a, b) {
console.log("Sum of the given numbers is:", a + b);
}
add(100, 827);
2. Assignment Operators
Assignment operators are used to assign values to variables. The most common ones are:
+= (add and assign)
-= (subtract and assign)
Example:
var num = 5875;
num += 25;
console.log("Sum after using operator is:", num);
3. Comparison Operators
Comparison operators are used to compare two values. They include:
(greater than)
< (less than)
\= (greater than or equal to)
<= (less than or equal to)
\== (equal to, checks value only)
\=== (strict equal to, checks value and type)
Example:
function compareNumbers(a, b) {
if (a > b) console.log(`${a} is greater than ${b}`);
else if (a < b) console.log(`${a} is smaller than ${b}`);
else console.log(`${a} is equal to ${b}`);
}
compareNumbers(875, 76);
4. Logical Operators
Logical operators are used to combine multiple conditions:
&& (AND)
|| (OR)
! (NOT)
Example:
function andOperator(a, b) {
if (a + b > 20 && a > b) {
console.log(`Sum of ${a} and ${b} is greater than 20 and ${a} is greater than ${b}`);
}
}
andOperator(789, 123);
5. Ternary Operator
The ternary operator is a shorthand way of writing an if-else statement:
function checkNumber(a) {
a >= 0 ? (a == 0 ? console.log(`${a} is zero`) : console.log(`${a} is positive`)) : console.log(`${a} is negative`);
}
checkNumber(8480);
Output
DAY3: JavaScript Control Structures Practice
This repository contains JavaScript functions demonstrating various control structures and conditional logic. These exercises are designed to help beginners understand and practice if-else statements, switch cases, ternary operators, and more complex conditional checks.
If-Else Statements
1. Check Number Sign
This function checks if a number is positive, negative, or zero.
function checkNumber(a) {
if (a > 0) console.log(`${a} is a positive number`);
if (a < 0) console.log(`${a} is a negative number`);
if (a == 0) console.log(`${a} is zero`);
}
Usage:
checkNumber(87218); // Output: 87218 is a positive number
checkNumber(-218); // Output: -218 is a negative number
checkNumber(0); // Output: 0 is zero
2. Check Voting Eligibility
This function determines if a person is eligible to vote based on their age.
function eligibleToVote(age) {
if (age >= 18)
console.log("Hurrey!! Now you can vote and make the democracy Strong");
else
console.log(
`Ooopsss!!! you have to wait ${18 - age} years to be eligible to VOTE!!`
);
}
Usage:
eligibleToVote(37); // Output: Hurrey!! Now you can vote and make the democracy Strong
eligibleToVote(3); // Output: Ooopsss!!! you have to wait 15 years to be eligible to VOTE!!
Nested If-Else Statements
3. Find Largest Among Three Numbers
This function finds the largest number among three given numbers using nested if-else statements.
function largestAmongThree(a, b, c) {
if (a > b) {
if (a > c) console.log(`${a} is the Largest Among ${a} and ${b} and ${c}`);
else console.log(`${c} is the Largest Among ${a} and ${b} and ${c}`);
} else if (b > c)
console.log(`${b} is the Largest Among ${a} and ${b} and ${c}`);
else console.log(`${c} is the Largest Among ${a} and ${b} and ${c}`);
}
Switch Case
4. Determine Day of the Week
This function uses a switch case to determine the day of the week based on a number (1-7).
function WhichDay(a) {
switch (a) {
case 1:
console.log("Today is MONDAY");
break;
case 2:
console.log("Today is TUESDAY");
break;
// ... (other cases)
case 7:
console.log("Today is SUNDAY");
break;
default:
console.log("OOOPPSSSS!!!!! Enter number between 1 to 7");
}
}
Usage:
WhichDay(7); // Output: Today is SUNDAY
WhichDay(875); // Output: OOOPPSSSS!!!!! Enter number between 1 to 7
5. Assign Grade Based on Score
This function uses a switch case with ranges to assign a grade based on a score.
function assignToFun(score) {
switch (true) {
case score >= 80 && score <= 100:
console.log("A");
break;
case score >= 70 && score < 80:
console.log("B");
break;
// ... (other cases)
default:
console.log("F");
}
}
Usage:
assignToFun(23); // Output: F
assignToFun(73); // Output: B
assignToFun(93); // Output: A
Conditional (Ternary) Operator
6. Check if Number is Even or Odd
This function uses the ternary operator to check if a number is even or odd.
function EvenOdd(a) {
var res = a % 2 == 0 ? "even" : "odd";
console.log(res);
}
Usage:
EvenOdd(8578); // Output: even
Combining Conditions
7. Check Leap Year
This function checks if a year is a leap year using multiple conditions.
function checkLeapYear(year) {
if (year % 4 == 0 && year % 100 != 0) {
if (year % 400 == 0) console.log(`${year} is a leap year`);
} else console.log(`${year} is not a leap year`);
}
Usage:
checkLeapYear(7126); // Output: 7126 is not a leap year
Output
JavaScript Day 4 Loops by Hitesh Sir
For Loop
Task 1: Print numbers from 1 to 10
for (var i = 1; i <= 10; i++) {
console.log(i);
}
This code uses a `for` loop to print numbers from 1 to 10. The loop initializes `i` to 1, continues as long as `i` is less than or equal to 10, and increments `i` by 1 in each iteration.
### Task 2: Print multiplication table of 5
```javascript
for (var i = 1; i <= 10; i++) {
console.log(i * 5);
}
This code prints the multiplication table of 5 up to 10. It uses a for
loop similar to Task 1, but multiplies each number by 5 before printing.
While Loop
Task 3: Calculate sum of numbers from 1 to 10
var sum = 0;
var sum_num = 1;
while (sum_num <= 10) {
sum += sum_num;
sum_num++;
}
console.log(sum, "this is sum");
This code uses a while
loop to calculate the sum of numbers from 1 to 10. It initializes sum
to 0 and sum_num
to 1, then adds sum_num
to sum
in each iteration until sum_num
exceeds 10.
Task 4: Print numbers from 10 to 1
var i = 10;
while (i > 0) {
console.log(i);
i--;
}
This code prints numbers from 10 to 1 using a while
loop. It initializes i
to 10 and decrements it in each iteration until it reaches 0.
Do...While Loop
Task 5: Print numbers from 1 to 5
var number3_5 = 1;
do {
console.log(number3_5);
number3_5++;
} while (number3_5 <= 5);
This code uses a do...while
loop to print numbers from 1 to 5. The loop body is executed at least once before the condition is checked.
Task 6: Calculate factorial of a number
function factorial(num) {
var fact = 1;
var i = 1;
do {
fact *= i;
i++;
} while (i <= num);
console.log(fact);
}
factorial(5);
This function calculates the factorial of a given number using a do...while
loop. It multiplies numbers from 1 to the given number and returns the result.
Nested Loops
Task 7: Print a pattern
for (var i = 1; i <= 5; i++) {
var str = "";
for (var j = 1; j <= i; j++) {
str += "* ";
}
console.log(str);
}
This code uses nested for
loops to print a triangular pattern of asterisks. The outer loop controls the number of rows, while the inner loop prints the asterisks for each row.
Loop Control Statements
Task 8: Skip number 5 using continue
for (var i = 1; i <= 10; i++) {
if (i === 5) continue;
console.log(i);
}
This code prints numbers from 1 to 10, but skips the number 5 using the continue
statement. When i
is 5, the continue
statement skips the rest of the loop body and moves to the next iteration.
Task 9: Stop at number 7 using break
for (var i = 1; i <= 10; i++) {
if (i === 7) break;
console.log(i);
}
This code prints numbers from 1 to 10, but stops the loop when the number is 7 using the break
statement. When i
is 7, the break
statement terminates the loop entirely.