Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

This PR is the solution to the 'js basics' #1 #15

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions 1-js-basics/1-data-types/excercise.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// let myVariable;
// myVariable has now been declared using the let keyword. It currently doesn't have a value.

let myVariable = 123;
// The above is called an explicit initialization when a variable is declared and is assigned a value at the same time.

const PI = 3;
// PI = 4; // not allowed
// Constants must be initialized, or an error will occur when running code.
// The reference of a constant cannot be changed once initialized, or an error will occur when running code.

let myString1 = "Hello";
let myString2 = "World";

myString1 + myString2 + "!"; //HelloWorld!
myString1 + " " + myString2 + "!"; //Hello World!
myString1 + ", " + myString2 + "!"; //Hello, World!
// Template literals are another way to format strings, except instead of quotes, the backtick is used. Anything that is not plain text must be placed inside placeholders ${ }. This includes any variables that may be strings.

// Challenge

// 1. Case Sensitivity
let age = 1;
let Age = 2;
console.log(age == Age); // false
// JavaScript is case-sensitive, meaning age and Age are treated as entirely different variables.

// 2. Type Coercion
console.log(1 == '1'); // true
console.log(1 === '1'); // false
// == performs type coercion, converting '1' (string) to 1 (number) before comparing.
// === is a strict equality operator and doesn't perform type coercion, so it compares both value and type.

// 3. numm vs. undefined
console.log(null == undefined); // true
console.log(null === undefined); // false

// null and undefined are considered loosely equal (==) because they both represent "absence of value".
// They are not strictly equal (===) because null is an object type and undefined is a primitive type.

// 4. NaN is Not Equal to Itself
console.log(NaN == NaN); // false
console.log(NaN === NaN); // false
// NaN (Not-a-Number) is unique in that it is not equal to anything, including itself. Use Number.isNaN(value) to check for NaN.

// 5. Typeof Difference
console.log(typeof null); // "object"
console.log(typeof NaN); // "number"
// typeof null returns "object" because of a historical bug in JavaScript that was never fixed for backward compatibility.
// typeof NaN returns "number" because NaN is technically a special value of the number type.
51 changes: 51 additions & 0 deletions 1-js-basics/1-data-types/solution1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Solution for 1: data types

The question is quite simple. We need to mention the data types that we would need to complete a shopping experience.

The experience can be divided into 5 different parts:

## User Login detials
| Name (Property) | Data Type | Reason |
|------------------|-----------|----------------------------------------------------------------------------------------|
| `id` | String | - Unique identifier for the user. <br> - String is used as it will be a combination of letters and numbers. |
| `name` | String | - Name of Customer. <br> - String is used to store names. |
| `email` | String | - Authentication. <br> - Combination of letters, numbers, and special characters. |
| `isLoggedIn` | Boolean | - Check the status of a session. <br> - A user can be logged in or not, thus Boolean. |

---

## Product Information
| Name (Property) | Data Type | Reason |
|------------------|-----------|----------------------------------------------------------------------------------------|
| `id` | String | - Unique identifier for each product. <br> - String is used as it will be a combination of letters and numbers. |
| `name` | String | - Name of Product. <br> - String is used to store names. |
| `price` | Double | - Represents the cost of the product. <br> - Number is used to perform final cost calculations. |
| `quantity` | Integer | - Number of products available. |
| `isAvailable` | Boolean | - Checks if a product is available or not. <br> - A product can be available or not, thus Boolean. |

---

## Shopping Cart
| Name (Property) | Data Type | Reason |
|------------------|-----------|----------------------------------------------------------------------------------------|
| `items` | Array | - Holds all the products added to the cart. |
| `totalPrice` | Double | - Sum of prices will be a double. |
| `discount` | Double | - Stores discount percentage or amount. |

---

## Payment Details
| Name (Property) | Data Type | Reason |
|------------------|-----------|----------------------------------------------------------------------------------------|
| `cardNumber` | String | - Allows payment processing systems to validate it. <br> - It will be a combination of numbers and dashes. |
| `totalPaid` | Double | - Reflects the final amount after discounts. |

---

## Order Summary
| Name (Property) | Data Type | Reason |
|------------------|-----------|----------------------------------------------------------------------------------------|
| `orderId` | String | - Unique identifier for each order. |
| `items` | Array | - Lists purchased products. |
| `totalPrice` | Double | - Reflects the amount charged. |
| `deliveryDate` | String | - Displays the estimated delivery date. |
44 changes: 44 additions & 0 deletions 1-js-basics/2-functions-methods/exercise2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// a function is a block of code we can execute on demand. This is perfect for scenarios where we need to perform the same task multiple times

// Syntax
function nameOfFunction() { // function definition
// function definition/body
}


// Whenever we want to call (or invoke) our function, we use the name of the function followed by ()

// If we want to make it a little more flexible, like allowing someone to specify the name of the person to greet, we can add a parameter. A parameter (also sometimes called an argument), is additional information sent to a function
// Syntax
function name(param, param2, param3) {

}

function displayGreeting(name) {
const message = `Hello, ${name}!`;
console.log(message);
}
displayGreeting('Prarthana');
// displays "Hello, Prarthana!" when run
// If someone doesn't want to customize it, we provide a default value instead. To provide a default value to a parameter, we set it much in the same way we set a value for a variable

function displayGreeting2(name, salutation='Hello') {
console.log(`${salutation}, ${name}`);
}
displayGreeting2('Prarthana', 'Hi');
// displays "Hi, Prarthana"

// A return value is returned by the function, and can be stored in a variable just the same as we could store a literal value such as a string or number.



// It uses a special indicator of =>, which looks like an arrow - thus the name! By using =>, we are able to skip the function keyword
// Syntax
setTimeout(() => {
console.log('3 seconds has elapsed');
}, 3000);

// Challenge

// Functions are standalone blocks of code that perform tasks, while methods are functions associated with an object and can access and modify the object's data.
// Functions are independent and can be called on their own, whereas methods are tied to objects and operate on the object's attributes.
49 changes: 49 additions & 0 deletions 1-js-basics/2-functions-methods/solution2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Solution for 2: Data Types

This assignment will familiarise you with the use of functions in different situations.

## Function that returns something:

```
function addNumbers(a, b) {
return a + b;
}

// Example usage:
const result = addNumbers(1, 2);
console.log(`Sum: ${result}`); // Output: Sum: 3


```
## Function that doesn't return anything

```
def greet_user(name):
function greetUser(name) {
console.log(`Hello, ${name}!`);
}

// Example usage:
greetUser("Prarthana"); // Output: Hello, Prarthana!


```
## Function with no Parameters

```
function printMessage() {
console.log("Dream Team Task: 04");
}
printMessage();

```
## Function with mix parameters

```
function introducePerson(name, age = 20, city = "Hyd") {
return `My name is ${name}, I am ${age} years old, and I live in ${city}.`;
}
const intro1 = introducePerson("Prarthana");
console.log(intro1);

```