From 1bbffa38cca9bb7a5e446761e33e79ef5915154d Mon Sep 17 00:00:00 2001 From: Iron-56 Date: Mon, 24 Feb 2025 18:32:33 +0530 Subject: [PATCH] Completed solution for issue 1 --- 1-js-basics/1-data-types/SOLUTION.md | 58 +++++++++++++++++++++ 1-js-basics/2-functions-methods/SOLUTION.md | 40 ++++++++++++++ 1-js-basics/3-making-decisions/SOLUTION.MD | 44 ++++++++++++++++ 1-js-basics/4-arrays-loops/SOLUTION.MD | 36 +++++++++++++ 4 files changed, 178 insertions(+) create mode 100644 1-js-basics/1-data-types/SOLUTION.md create mode 100644 1-js-basics/2-functions-methods/SOLUTION.md create mode 100644 1-js-basics/3-making-decisions/SOLUTION.MD create mode 100644 1-js-basics/4-arrays-loops/SOLUTION.MD diff --git a/1-js-basics/1-data-types/SOLUTION.md b/1-js-basics/1-data-types/SOLUTION.md new file mode 100644 index 0000000..021193c --- /dev/null +++ b/1-js-basics/1-data-types/SOLUTION.md @@ -0,0 +1,58 @@ +# Data Types in a Shopping Cart + +## Introduction +A shopping cart contains different data types to effectively manage UI, products, user and network. Here are some of the data types that may be useful for developing a shopping cart: + +## Data Types + +### 1. **String** +**Purpose:** To store data like user name, product name, locations etc... +**Example:** +```javascript +let userName = "Gokul"; +let productName = "Pavillion HP Gaming Laptop" +``` + +### 2. **Array** +**Purpose:** To store different product objects in a cart, listing all products in the document +**Example:** +```javascript +let productList = []; +productList.push(myProduct); +``` + +### 3. **Boolean** +**Purpose:** To store values that represent true or false like the availability of product, user is logged in, free shipping etc... +**Example:** +```javascript +bool isAvailable = false; +if(!isAvailable) +{ + ... +} +``` + +### 4. **Date** +**Purpose:** Date type can be used to display the delivery date, packaging date etc... +**Example:** +```javascript +const delivery = new Date("2025-03-10"); +``` + +### 5. **Object** +**Purpose:** Object can be an instance of a Product class, which may contain information about count, price etc... An array of product objects are added to the cart. +**Example:** +```javascript +let product = new Product("Kurti", 299.9); +``` + +## Challenge + +```javascript +NaN == NaN // false +Number.isNaN(NaN) // true + +## Assignment +``` + +``` diff --git a/1-js-basics/2-functions-methods/SOLUTION.md b/1-js-basics/2-functions-methods/SOLUTION.md new file mode 100644 index 0000000..f1f5dcc --- /dev/null +++ b/1-js-basics/2-functions-methods/SOLUTION.md @@ -0,0 +1,40 @@ +## Challenge +Functions and methods are very similar but they differ in OOP. Functions defined in a class is known as methods. Functions are independent of any class. + + +## Assignment +```javascript +function say() { + console.log("Hello world!"); +} + +say(); +``` + +```javascript +function greet(name = "User") { + console.log("Hello " + name + "!"); +} + +greet("Nandu"); +``` + +```javascript + +function isOdd(n) { + return n & 1; +} + +if (isOdd(5)) { + console.log("5 is an odd number!"); +} else { + console.log("5 is an even number!"); +} +``` + +```javascript +function sum(...numbers) { + return numbers.reduce((acc, num) => acc + num, 0); +} +console.log(sum(5, 6, 2, 3)) +``` \ No newline at end of file diff --git a/1-js-basics/3-making-decisions/SOLUTION.MD b/1-js-basics/3-making-decisions/SOLUTION.MD new file mode 100644 index 0000000..ba4276d --- /dev/null +++ b/1-js-basics/3-making-decisions/SOLUTION.MD @@ -0,0 +1,44 @@ +## Challenge + +```javascript +x = 10 +if (x%5 == 0) { + console.log("Divisible by 5"); +} else { + console.log("Not divisible by 5"); +} +``` + +```javascript +x = 8 +console.log(x%5 == 0 ? "Divisible by 5" : "Not divisible by 5"); +``` + +I prefer using if else statements for understandability and I use ternary operator when the program code does not look good. + +## Assignment + +```javascript +let allStudents = [ + 'A', + 'B-', + 1, + 4, + 5, + 2 +] + +let studentsWhoPass = []; + +allStudents.forEach(v => { + if (typeof(v) == 'string') { + if (v != 'C-') { + studentsWhoPass.push(v) + } + } else { + if (v>3) { + studentsWhoPass.push(v) + } + } +}); +``` \ No newline at end of file diff --git a/1-js-basics/4-arrays-loops/SOLUTION.MD b/1-js-basics/4-arrays-loops/SOLUTION.MD new file mode 100644 index 0000000..e332200 --- /dev/null +++ b/1-js-basics/4-arrays-loops/SOLUTION.MD @@ -0,0 +1,36 @@ +## Challenge ## + +```javascript + +let numbers = [1, 2, 3, 4] + +// Normal for loop + +for (let i = 0; i < numbers.length; i++) { + console.log(numbers[i] * 2); +} + +// for each + +numbers.forEach(num => console.log(num*2)); + +// map + +console.log(numbers.map(num => num*2)); + +// for of + +for(let num in numbers) { + console.log(num*2); +} +``` + +## Assignment ## + +```javascript + +for(let i=1; i<=20; i++) { + if (i%3 == 0) { + console.log(i); + } +}