Javascript some basic problem disscussion

Here are some basic problem of javascript,that i disscussed.
1. Largest Number
To find the largest element, the first two elements of array are checked and the largest of these two elements are placed in arr[0] the first and third elements are checked and largest of these two elements is placed in arr[0] .
this process continues until the first and last elements are checked.
I share a simple example:
const array1 = [1, 3, 2];

console.log(Math.max(…array1));
Zero or more numbers among which the largest value will be selected and returned.

2. Remove Duplicate item from an array
many method are use to remove duplicate item from an array
and i disscuss some of them:
I have a very simple JavaScript array that may or may not contain duplicates.
var names = [“x”,”y”,”x”,”y”,”z”,”p”,”q”];
now i need to remove the duplicate item
Remove duplicates from an array using indexOf() and filter() methods
names.indexOf(‘B’);

let name = names .filter((c, index) => {
return names .indexOf(para) === index;
});

console.log(name);

3.JavaScript Program to Reverse a String

Example:

function reverseString(str) {

let newString = “”;
for (let i = str.length — 1; i >= 0; i — ) {
newString += str[i];
}
return newString;
}

const string = prompt(‘Enter a string: ‘);

const result = reverseString(string);
console.log(result);
In the above program, the user is prompted to enter a string. That string is passed to the reverseString() function.

Inside the reverseString() function,

An empty newString variable is created.
The for loop is used to iterate over the strings. the first iteration, str.length — 1 gives the position of the last element. That element is added to the newString variable.
This process continues for all the string elements.
The value of i decreases in each iteration and continues until it becomes 0.

4.Factorial
the factorial of n can be found by finding the factorial of a number one less than n
Factorial of number is the product of all positive descending integers. Factorial of n is denoted by n!.
For example:

4! = 4 * 3 * 2 * 1 = 24

5! = 5 * 4 * 3 * 2 * 1 = 120
n!=n*n*n

5.Fibonacci Series Program in JavaScript
Fn = Fn-1 + Fn-2
Where F0=1 and F1=1. The Fibonacci numbers are the numbers that are 0, 1, 1, 2, 3, 5, 8, 13
the sequence Fn of Fibonacci numbers is defined

Fn = Fn-1 + Fn-2 where F0 = 0 and F1 = 1
As the first Fibonacci number is 0 and the second is 1 and the nth Fibonacci number is summation of n-1 and n-2.
So,the nth Fibonacci series is
fib(1)=0
fib(2)=1
fib(3)=fib(2)+fib(1)
fib(4)=fib(3)+fib(2)
….
fib(n)=fib(n-1)+fib(n-2)

6.Api
API: API stands for application programming interface. … In an internet-connected world, web and mobile applications are designed for humans to use.
GET is better for the data which does not require any security or having images or word documents.

What is POST Request Method?
POST is the HTTP method that is designed to send loads of data to a server from a specified resource.
Most common HTML forms on the web operate using this request method. It usually transmits relatively small loads of data to a receiver.
This method allows data to be sent as a package in a separate communication with the processing script.
This means that data sent through the POST method will not be visible in the URL, as parameters are not sent along with the URI.

7. setTimeout and setInterval
There are two methods for it:

setTimeout allows us to run a function once after the interval of time.
setInterval allows us to run a function repeatedly, starting after the interval of time, then repeating continuously at that interval.
function sayHi() {
alert(‘Hello’);
}

setTimeout(sayHi, 1000);
8. undefined vs null
In JavaScript, undefined is a type, where null an object. It means a variable declared,
but no value has been assigned a value. Whereas, null in JavaScript is an assignment value.
Null and Undefined are both data types in JavaScript.
Undefined is a variable that has been declared but not assigned a value.
Null as an assignment value. So you can assign the value null to any variable.
9.Global variable, Global Scope.
JavaScript Global Variable
A JavaScript global variable is declared outside the function or declared with window object. It can be accessed from any function.
example:
var value=50;//global variable
function a(){
alert(value);
}
function b(){
alert(value);
}
Javascript GLobal Scope
JavaScript Function Scope
In JavaScript there are two types of scope:

Local scope
Global scope

Global Scope
In a script, the outermost scope is the global scope:
// Global Scope

const name = “Monique”;

function sayHi() {
console.log(`Hi ${name}`);
}

sayHi();

10. double equal vs triple equal
Double Equals ( == ) checks for value equality only, that means it only check the value are same or true.
Triple Equals ( === ) checks for value equality and also check the types of value the value. all are true so the output result will true.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Mahedi Hassan Sharif

Mahedi Hassan Sharif

Hi! I am Mahedi Hassan Sharif. I am a Web Application Developer.