JavaScript Basics Overview
JavaScript Basics Overview
1. Fundamentals of JavaScript:
Variables and Data Types: let, const, var, string, number, boolean, null, undefined, object, array.
Operators: Arithmetic (+, -, *, /), Comparison (==, ===, !=, !==, >, <), Logical (&&, ||, !).
Conditionals: if, else if, else, switch statement.
Loops:
for, while, do...while.
Advanced Loop: for...of (for arrays), for...in (for objects).
1. Fundamentals of JavaScript
Variables: Used to store data that can be used later in the program. JavaScript has three ways to
declare variables:
`let`: Block-scoped variable, allows reassignment.
`const`: Block-scoped constant, cannot be reassigned.
`var`: Function-scoped variable, allows reassignment.
Data Types: JavaScript has different data types to store various kinds of information.
Primitive Types:
`string`: Represents textual data. Example: `"Hello, World!"`
`number`: Represents numbers, both integers and floats. Example: `42`, `3.14`
`boolean`: Represents logical values. `true` or `false`.
`null`: Represents an intentional absence of any value.
`undefined`: Represents a variable that has been declared but has not been assigned a
value.
Non-Primitive Types:
`object`: Stores collections of key-value pairs. Example: `{ name: "John", age: 30 }`
`array`: Stores ordered lists of items. Example: `[1, 2, 3]`
Operators
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 1/12
`===` (Strict Equality): Checks if values and types are equal. Example: `5 === '5'` (false).
`!=` (Inequality): Checks if values are not equal. Example: `5 != '6'` (true).
`!==` (Strict Inequality): Checks if values and types are not equal. Example: `5 !== '5'`
(true).
`>` (Greater Than): `5 > 3` results in `true`.
`<` (Less Than): `5 < 3` results in `false`.
Conditionals
javascript
if (x > 10) {
console.log("x is greater than 10");
}
javascript
if (x > 10) {
console.log("x is greater than 10");
} else if (x === 10) {
console.log("x is exactly 10");
}
javascript
if (x > 10) {
console.log("x is greater than 10");
} else {
console.log("x is less than or equal to 10");
}
switch statement: Evaluates an expression and executes code based on the matching `case`.
javascript
let day = 3;
switch (day) {
case 1:
console.log("Monday");
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 2/12
break;
case 2:
console.log("Tuesday");
break;
default:
console.log("Other day");
}
Loops
javascript
javascript
let i = 0;
while (i < 5) {
console.log(i);
i++;
}
do...while loop: Executes the block once before checking the condition.
javascript
let i = 0;
do {
console.log(i);
i++;
} while (i < 5);
Advanced Loop
javascript
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 3/12
javascript
This covers the core aspects of JavaScript fundamentals, perfect for building a strong foundation in
programming!
now make some questions for my improvement in JS coding and problem solving skills
Here are some questions and challenges to help improve your JavaScript coding and problem-solving
skills. They range from basic to intermediate, covering variables, data types, loops, conditionals, and
more.
Question 1: Create a variable using `let` to store your name and another using `const` to store
your birth year. Change the value of the name variable but try changing the value of the birth
year. What happens?
Question 2: Write a program that stores three different data types (a string, a number, and a
boolean) in variables and prints their types using `typeof`.
2. Operators
Question 3: Write a program that takes two numbers as input and prints their sum, difference,
product, and quotient.
Question 4: Compare two variables using both `==` and `===`. Explain the difference between
the two comparisons through code examples.
3. Conditionals
Question 5: Write a program that checks if a number is positive, negative, or zero using
`if...else`.
Question 6: Create a `switch` statement that takes a day of the week as input (e.g., 1 for
Monday) and prints out the corresponding day name.
Question 7: Write a program that takes a user's age as input and outputs whether they are a
minor, an adult, or a senior citizen using `if`, `else if`, and `else`.
4. Loops
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 4/12
Question 10: Create a `do...while` loop that asks the user for a password until they enter the
correct one.
5. Advanced Loop
Question 11: Write a `for...of` loop that iterates over an array of colors and prints each color.
Question 12: Write a `for...in` loop that iterates over the properties of an object and prints
both the property names and their values.
Question 13: Given an array of numbers, use a `for...of` loop to find the sum of all the
elements.
Question 14: Write a program that stores five numbers in an array. Find and print the maximum
number using a loop.
Question 15: Create an object representing a car (with properties like `make`, `model`, `year`).
Write a function that takes an object as an argument and prints out all its properties.
Question 16: Write a function that takes an array of numbers as input and returns a new array
with each number squared.
7. Functions
Question 17: Write a function called `isEven` that takes a number as input and returns `true` if
it is even and `false` if it is odd.
Question 18: Write a function `calculateArea` that takes the radius of a circle as a parameter
and returns the area of the circle.
Question 19: Create a function `greet` that takes a name as a parameter and prints a
personalized greeting.
8. Practice Problems
Question 20: Write a program that checks if a given word is a palindrome (reads the same
forwards and backwards).
Question 21: Create a function that finds the factorial of a given number using a `for` loop.
Question 22: Write a program that reverses a given array without using the `reverse()` method.
Question 23: Write a function that takes an array of numbers and returns the second largest
number.
Question 24: Create a function that takes an array and removes all duplicates, returning a new
array with only unique values.
Question 25: Write a program that takes a number as input and prints a triangle pattern using
nested `for` loops. For example, if the input is `5`:
markdown
*
**
***
****
*****
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 5/12
These questions will help you to practice coding and improve your problem-solving skills in JavaScript.
Try to solve each question, and if you get stuck, focus on breaking down the problem into smaller
parts. Happy coding!
Let's work through all the questions with solutions and explanations:
Question 1: Create a variable using `let` to store your name and another using `const` to store
your birth year. Change the value of the name variable but try changing the value of the birth
year. What happens?
javascript
Question 2: Write a program that stores three different data types (a string, a number, and a
boolean) in variables and prints their types using `typeof`.
javascript
2. Operators
Question 3: Write a program that takes two numbers as input and prints their sum, difference,
product, and quotient.
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 6/12
javascript
let a = 8;
let b = 4;
console.log("Sum:", a + b); // 12
console.log("Difference:", a - b); // 4
console.log("Product:", a * b); // 32
console.log("Quotient:", a / b); // 2
Question 4: Compare two variables using both `==` and `===`. Explain the difference between
the two comparisons through code examples.
javascript
let x = 5;
let y = "5";
Explanation: `==` compares only the values, so `5` and `"5"` are considered equal. `===`
compares both values and types, so `5` (number) is not equal to `"5"` (string).
3. Conditionals
Question 5: Write a program that checks if a number is positive, negative, or zero using
`if...else`.
javascript
if (num > 0) {
console.log("Positive");
} else if (num < 0) {
console.log("Negative");
} else {
console.log("Zero");
}
Question 6: Create a `switch` statement that takes a day of the week as input (e.g., 1 for
Monday) and prints out the corresponding day name.
javascript
let day = 2;
switch (day) {
case 1:
console.log("Monday");
break;
case 2:
console.log("Tuesday");
break;
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 7/12
case 3:
console.log("Wednesday");
break;
case 4:
console.log("Thursday");
break;
case 5:
console.log("Friday");
break;
case 6:
console.log("Saturday");
break;
case 7:
console.log("Sunday");
break;
default:
console.log("Invalid day");
}
Question 7: Write a program that takes a user's age as input and outputs whether they are a
minor, an adult, or a senior citizen using `if`, `else if`, and `else`.
javascript
4. Loops
javascript
Question 9: Write a `while` loop that prints the even numbers between 1 and 20.
javascript
let i = 2;
while (i <= 20) {
console.log(i);
i += 2;
}
Question 10: Create a `do...while` loop that asks the user for a password until they enter the
correct one.
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 8/12
javascript
let password;
const correctPassword = "1234";
do {
password = prompt("Enter the password:");
} while (password !== correctPassword);
console.log("Access granted.");
5. Advanced Loop
Question 11: Write a `for...of` loop that iterates over an array of colors and prints each color.
javascript
Question 12: Write a `for...in` loop that iterates over the properties of an object and prints
both the property names and their values.
javascript
Question 13: Given an array of numbers, use a `for...of` loop to find the sum of all the
elements.
javascript
Question 14: Write a program that stores five numbers in an array. Find and print the maximum
number using a loop.
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 9/12
javascript
Question 15: Create an object representing a car (with properties like `make`, `model`, `year`).
Write a function that takes an object as an argument and prints out all its properties.
javascript
const car = {
make: "Toyota",
model: "Corolla",
year: 2020
};
function printCarDetails(car) {
for (let key in car) {
console.log(key + ": " + car[key]);
}
}
printCarDetails(car);
Question 16: Write a function that takes an array of numbers as input and returns a new array
with each number squared.
javascript
function squareArray(arr) {
const squared = [];
for (let num of arr) {
squared.push(num * num);
}
return squared;
}
7. Functions
Question 17: Write a function called `isEven` that takes a number as input and returns `true` if
it is even and `false` if it is odd.
javascript
function isEven(num) {
return num % 2 === 0;
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 10/12
}
console.log(isEven(4)); // true
console.log(isEven(5)); // false
Question 18: Write a function `calculateArea` that takes the radius of a circle as a parameter
and returns the area of the circle.
javascript
function calculateArea(radius) {
return Math.PI * radius * radius;
}
Question 19: Create a function `greet` that takes a name as a parameter and prints a
personalized greeting.
javascript
function greet(name) {
console.log("Hello, " + name + "!");
}
8. Practice Problems
Question 20: Write a program that checks if a given word is a palindrome (reads the same
forwards and backwards).
javascript
function isPalindrome(word) {
const reversed = word.split("").reverse().join("");
return word === reversed;
}
console.log(isPalindrome("racecar")); // true
console.log(isPalindrome("hello")); // false
Question 21: Create a function that finds the factorial of a given number using a `for` loop.
javascript
function factorial(n) {
let result = 1;
for (let i = 1; i <= n; i++) {
result *= i;
}
return result;
}
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 11/12
console.log(f
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 12/12