function and callbacks, class, object(oops)

 functions


Q: what is a function?
Ans:  A function in a JavaScript is a set of statement that performs a task or calculate a value It should take some input and return an output where there is some obvious relationship between the input and the output.


Function Syntax:
function findSum(n) {
let ans = 0;
for (let i = 0; i<n; i++) {
ans = ans +i
}
return ans;
}
let ans = findSum(100)
console.log(ans);


Example
function sum (a,b){
return  a+ b;
}



1 Without using Function.
let n = 100;
let ans = 0;
for (let i=1; i<n; i++) {
ans = ans + i
}
console.log(ans);
--------------
let n2 = 1000;
let ans2 = 0;
for (let i=1; i<n; i++) {
ans2 = ans2 + i
}
console.log(ans2);


2 With Function type 

function findSum(n) {
let ans = 0;
for (let i = 0; i<n; i++) {
ans = ans +i
}
return ans;
}
let ans = findSum(100)
console.log(ans);

let ans = findSum(1000)
console.log(ans);


Callback Functions

you can call one function inside another function?


Example Without callback function code:
//find the square of the input
function square(n) {
return n*n
}

// find the sum of the square of the inputs.
function sumOfSquares(a,b) {
const val1 = square(a);
const val2 = square (b);

return val1+val2;
}
console.log(sumOfSquares(1,3));


Example without callback function code:

//find the square of the input
function square(n) {
return n*n
}

function cube (n) {
return n*n*n
}


// find the sum of the square of the inputs.
function sumOfSquares(a,b) {
const val1 = square(a);
const val2 = square (b);

return val1+val2;
}
console.log(sumOfSquares(1,3));

//
function sumOfCube(a,b) {
const val1 = cube(a);
const val2 = cube(b);

return val1+ val2;
}


console.log(sumOfCube(1,2));



Exaple code with callbacks function:


function sauare(a) {
return (a*a)
}

function cube(a) {
return (a*a)
}


function sumOfSomthing(a, b, callback) {
const val1 = callback(a);
const val2 = callback(b);
rerutn val1+ val2;
}
sumOfSomething(a, b, square)


---------------------

code::

function square(n) {
  return n * n;
}

function cube(n) {
  return n * n * n;
}

function sumOfSomething(a, b, callback) {
  const val1 = callback(a);
  const val2 = callback(b);
  return val1 + val2;
}

const ans = sumOfSomething(3, 2, square);
console.log(ans);


Anonymous functions

A function that doesn't have name.

function sumOfSomething(a, b, callback) {
  const val1 = callback(a);
  const val2 = callback(b);
  return val1 + val2;
}
sumOfSomething(a,b, function (a) {
return a *a
})
 
 
Absolutely! Let's break down the differences between classes, objects, and functions in JavaScript.

**Classes**

* **Blueprints:** Classes are templates or blueprints for creating objects. They define a set of properties (data) and methods (behaviors) that objects made from that class will possess.
* **Object-Oriented Programming (OOP):** Classes are a core concept in object-oriented programming, a way to structure code that models real-world concepts or logical units.
* **Example:**

```javascript
class Car {
  constructor(make, model, year) { // Constructor to initialize properties
    this.make = make;
    this.model = model;
    this.year = year;
  }

  accelerate() { // Method representing  a behavior
    console.log(`${this.make} ${this.model} is accelerating!`);
  }
}
```

**Objects**

* **Instances:** Objects are actual instances created from classes. They inherit the properties and methods defined in the class.
* **Real-world representation:** Objects often represent things or concepts—a car, a person, a bank account, etc.
* **Example:**

```javascript
const myCar = new Car("Toyota", "Camry", 2022);  // Creating an object
myCar.accelerate(); // Calling the accelerate method on the object
```

**Functions**

* **Self-contained code blocks:** Functions encapsulate a specific task or logic. They can take inputs (parameters) and return outputs.
* **Reusability:** Functions are designed to be reusable, so you can write a function once and call it multiple times.
* **Example:**

```javascript
function greet(name) {
  console.log(`Hello, ${name}!`);
}

greet("Alice");
greet("Bob");
```

**Key Points**

* **Prototypal Inheritance:** JavaScript uses an inheritance system where objects inherit properties and methods from other objects (their prototypes). There's a bit of nuance here since it doesn't use traditional "classes" like some other languages.
* **Functions as First-Class Citizens:**  Functions in JavaScript can be treated like objects: assigned to variables, passed as arguments, and returned from other functions.

**Let me know if you'd like a deeper dive into any of these concepts or more illustrative examples!**
 

Comments

Popular posts from this blog

CyberSecurity

VERTICAL SCALING 💋

prisma using in project idx 13.3