Javascript – AppDividend https://appdividend.com Latest Code Tutorials Thu, 26 Mar 2020 18:23:09 +0000 en-US hourly 1 https://appdividend.com/wp-content/uploads/2017/08/cropped-ApDivi-32x32.png Javascript – AppDividend https://appdividend.com 32 32 JavaScript Math sin() Function Example https://appdividend.com/2020/03/26/javascript-math-sin-function-example/ https://appdividend.com/2020/03/26/javascript-math-sin-function-example/#respond Thu, 26 Mar 2020 18:19:37 +0000 http://localhost/wordpress/?p=15933

Javascript Math sin() is an inbuilt function that finds the sine value of a given argument. The Math.sin() method comes handy in programming contexts dealing with any trigonometric expressions. The sin() is a static method of Math, and it can be used without creating an object. JavaScript Math sin() The Math. sin() function in Javascript is used to return the sine […]

The post JavaScript Math sin() Function Example appeared first on AppDividend.

]]>

Javascript Math sin() is an inbuilt function that finds the sine value of a given argument. The Math.sin() method comes handy in programming contexts dealing with any trigonometric expressions. The sin() is a static method of Math, and it can be used without creating an object.

JavaScript Math sin()

The Mathsin() function in Javascript is used to return the sine of the number. The sin() method returns the numeric value between -1 and 1, which represents the sine of the angle given in radians. Javascript sin() is a static method of Math; therefore, it is always used as Math.

Syntax

```Math.sin(x)
```

Parameter(s)

The variable x (in radians) whose sine value is to be determined.

Return Value

The sine value between -1 and 1.

Note

• If the passed value is empty, the sin() method returns NaN.

Compatibility(Version and above)

2. Internet Explorer v3
3. Firefox v1
4. Edge v12
5. Opera v3
6. Safari v1
7. Android webview v1
8. Chrome for Android v18
9. Firefox for Android v4
10. Opera for Android v10.1
11. Safari on iOS v1
12. Samsung Internet v1.0
13. Node.js

JavaScript version: ECMAScript 1

Consider the following examples.

JavaScript Math sin() function example

The following code demonstrates the use of the sin() method.

```// app.js

let a = 0;
let b = 1;
let c = Math.PI;
let d = 2 * Math.PI;
let e = (Math.PI) / 2;

console.log(Math.sin(a));
console.log(Math.sin(b));
console.log(Math.sin(c));
console.log(Math.sin(d));
console.log(Math.sin(e));
```

Output

```node app
0
0.8414709848078965
1.2246467991473532e-16
-2.4492935982947064e-16
1```

Example 2

The following example demonstrates the case where an empty value is passed.

```// app.js

let x;

console.log(Math.sin(x));
console.log(Math.sin());
```

Output

```node app
NaN
NaN
```

Example 3

The following code example demonstrates an application of the Math.sin() method in the simple programming context.

Given the base angle of the triangle and the base side, find all the remaining sides of the triangle.

```// app.js

// Given the base angle of a triangle and the base side,
// find all the remaining sides of the triangle.

let p;
let b;
let h;

let angle;

const rl = r.createInterface({
input: process.stdin,
output: process.stdout
});

const prompt1 = () => {
return new Promise((resolve, reject) => {
rl.question('Base side: ', (answer) => {
resolve();
});
});
};

const prompt2 = () => {
return new Promise((resolve, reject) => {
resolve();
});
});
};

const main = async () => {
await prompt1();
await prompt2();
rl.close();

console.log('The three sides of the triangle are:');
console.log(b); //base side

let angle2 = (Math.PI) / 2 - angle;

h = b / (Math.sin(angle2));
console.log(h); //hypotenuse

p = h * Math.sin(angle);

console.log(p); //third side
}

main();
```

Output

```Test Case 1:
->node example3
Base side: 6
The three sides of the triangle are:
6
6.928204127882605
3.464103410351597

Test Case2:
->node example3
Base side: 30
The three sides of the triangle are:
30
42.42639993882793
29.99999019615471

```

Javascript Math exp()

Javascript Math pow()

Javascript Math tanh()

Javascript Math sinh()

Javascript Math tan()

The post JavaScript Math sin() Function Example appeared first on AppDividend.

]]>
https://appdividend.com/2020/03/26/javascript-math-sin-function-example/feed/ 0
JavaScript Math tan() Function Example https://appdividend.com/2020/03/26/javascript-math-tan-function-example/ https://appdividend.com/2020/03/26/javascript-math-tan-function-example/#respond Thu, 26 Mar 2020 17:59:01 +0000 http://localhost/wordpress/?p=15928

Javascript Math tan() is an inbuilt function that finds the tangent value of a given argument, The Math.tan() method comes handy in Javascript contexts dealing with any trigonometric expressions. JS tan() is a static method of Math, and it can be used without creating an object. JavaScript Math tan() The Math.tan() method returns the numeric value that […]

The post JavaScript Math tan() Function Example appeared first on AppDividend.

]]>

Javascript Math tan() is an inbuilt function that finds the tangent value of a given argument, The Math.tan() method comes handy in Javascript contexts dealing with any trigonometric expressions. JS tan() is a static method of Math, and it can be used without creating an object.

JavaScript Math tan()

The Math.tan() method returns the numeric value that represents a tangent of the angle. Because tan() is the static method of Math, you always use it as Math.tan(), rather than as the method of the Math object you created ( Math is not a constructor).

Syntax

```Math.tan(x)
```

Parameter(s)

The variable x (in radians) whose tangent value is to be determined.

Return Value

The tangent value between negative infinity and positive infinity.

Note

1. If the passed value is empty, this method returns NaN.
2. For odd multiples of pi/2 radians passed as a parameter, this method returns a garbage value as the tan is not defined for these values.

Compatibility(Version and above)

2. Internet Explorer v3
3. Firefox v1
4. Edge v12
5. Opera v3
6. Safari v1
7. Android webview v1
8. Chrome for Android v18
9. Firefox for Android v4
10. Opera for Android v10.1
11. Safari on iOS v1
12. Samsung Internet v1.0
13. Node.js

JavaScript version: ECMAScript 1

Consider the following examples.

Javascript Math.tan()

The following code demonstrates the use of the Math.tan() method.

```// app.js

let a = 0;
let b = 1;
let c = Math.PI;
let d = 2 * Math.PI;
let e = (Math.PI) / 4;

console.log(Math.tan(a));
console.log(Math.tan(b));
console.log(Math.tan(c));
console.log(Math.tan(d));
console.log(Math.tan(e));
```

Output

```node app
0
1.5574077246549023
-1.2246467991473532e-16
-2.4492935982947064e-16
0.9999999999999999```

Example 2

The following example demonstrates the case where an empty value is passed.

```// app.js

let x;

console.log(Math.tan(x));
console.log(Math.tan());
```

Output

```node app
NaN
NaN```

Example 3

The following example demonstrates the case where odd multiples of pi/2 are passed.

```// app.js

let a = (Math.PI) / 2;
let b = 3 * (Math.PI) / 2;
let c = 5 * (Math.PI) / 2;
let d = 7 * (Math.PI) / 2;

console.log(Math.tan(a));
console.log(Math.tan(b));
console.log(Math.tan(c));
console.log(Math.tan(d));
```

Output

```node example3
16331239353195370
5443746451065123
3266247870639074
2333034193313624.5
```

Example 4

The following example demonstrates the application of this method in a simple programming context.

Given the base angle of a triangle and the base side, find all the remaining sides of the triangle.

```// Given the base angle of a triangle and the base side,
// find all the remaining sides of the triangle.

let p;
let b;
let h;

let angle;

const rl = r.createInterface({
input: process.stdin,
output: process.stdout
});

const prompt1 = () => {
return new Promise((resolve, reject) => {
rl.question('Base side: ', (answer) => {
resolve();
});
});
};

const prompt2 = () => {
return new Promise((resolve, reject) => {
resolve();
});
});
};

const main = async () => {
await prompt1();
await prompt2();
rl.close();

console.log('The three sides of the triangle are:');
console.log(b); //base side

let angle2 = (Math.PI) / 2 - angle;

p = b * Math.tan(angle);
console.log(p); //third side

h = Math.sqrt(p * p + b * b);
console.log(h); //hypotenuse
}

main();```

Output

```Test Case 1:
->node example4
Base side: 6
The three sides of the triangle are:
6
3.464103410351597
6.928204127882605

Test Case2:
->node example4
Base side: 30
The three sides of the triangle are:
30
29.99999019615471
42.42639993882793
```

Javascript Math max()

Javascript Math exp()

Javascript Math pow()

Javascript Math tanh()

Javascript Math sinh()

The post JavaScript Math tan() Function Example appeared first on AppDividend.

]]>
https://appdividend.com/2020/03/26/javascript-math-tan-function-example/feed/ 0
Javascript Math sinh() Function Example https://appdividend.com/2020/03/26/javascript-math-sinh-function-example/ https://appdividend.com/2020/03/26/javascript-math-sinh-function-example/#respond Thu, 26 Mar 2020 12:15:02 +0000 http://localhost/wordpress/?p=15921

If you want to find the hyperbolic sine value of a given argument in JavaScript, then use the Math.sinh() method is used. The Math.sinh() method comes handy in programming contexts dealing with any trigonometric expressions. The sinh() is a static method of Math, and it can be used without creating an object. Javascript Math sinh() […]

The post Javascript Math sinh() Function Example appeared first on AppDividend.

]]>

If you want to find the hyperbolic sine value of a given argument in JavaScript, then use the Math.sinh() method is used. The Math.sinh() method comes handy in programming contexts dealing with any trigonometric expressions. The sinh() is a static method of Math, and it can be used without creating an object.

Javascript Math sinh()

Javascript Math.sinh() function returns the hyperbolic sine of a number that can be expressed using the constant e.

Syntax

```Math.sinh(x)
```

Parameter(s)

The variable x, whose hyperbolic sine value is to be determined.

Return Value

A number which is the hyperbolic sine value.

Polyfill

```Math.sinh = Math.sinh || function(x) {
return (Math.exp(x) - Math.exp(-x))/2;
};

OR,

Math.sinh = Math.sinh || function(x) {
var y = Math.exp(x);
return (y-1/y)/2;
};
```

See the following figure.

Note

• If the passed value is not a valid number, the method returns NaN.

Compatibility(Version and above)

2. Firefox v25
3. Edge v12
4. Opera v25
5. Safari v8
6. Android webview v38
7. Chrome for Android v38
8. Firefox for Android v25
9. Opera for Android v25
10. Safari on iOS v8
11. Samsung Internet v3.0
12. Node.js v0.12

Non-compatible with: Internet Explorer

JavaScript version: ECMAScript 6

Consider the following examples.

Javascript Math.sinh() function example

The following code shows the use of the Math.sinh() method.

```// app.js

let a = 1;
let b = -1;
let c = 0;
let d = 4;
let e = -2;
let f = 2;

console.log(Math.sinh(a));
console.log(Math.sinh(b));
console.log(Math.sinh(c));
console.log(Math.sinh(d));
console.log(Math.sinh(e));
console.log(Math.sinh(f));
```

Output

```node app
1.1752011936438014
-1.1752011936438014
0
27.28991719712775
-3.626860407847019
3.626860407847019
```

Example 2

The following code demonstrates the case where values other than valid numbers are passed.

```// app.js

let a = "Hello, world";
let b;

console.log(Math.sinh(a));
console.log(Math.sinh(b));
```

Output

```node app
NaN
NaN```

Example 3

Javascript sinh() method cannot be used with complex arguments as only integer arguments are accepted. See the following code.

```// app.js

// Complex values cannot be passed as arguments as follows
// Since only integer arguments are accepted.

console.log(Math.sinh(2 + i));```

Output

```node app
ReferenceError: i is not defined
```

Example 4

The following code demonstrates the use of Polyfill for the Math.sinh() method.

```// app.js

let a = 1;
let b = -1;
let c = 0;
let d = 4;
let e = -2;
let f = 2;

function polyfill1(x) {
return (Math.exp(x) - Math.exp(-x)) / 2;
}

function polyfill2(x) {
var y = Math.exp(x);
return (y - 1 / y) / 2;
}

console.log(Math.sinh(a));
console.log(Math.sinh(b));
console.log(Math.sinh(c));
console.log(Math.sinh(d));
console.log(Math.sinh(e));
console.log(Math.sinh(f));
console.log();

console.log(polyfill1(a));
console.log(polyfill1(b));
console.log(polyfill1(c));
console.log(polyfill1(d));
console.log(polyfill1(e));
console.log(polyfill1(f));
console.log();

console.log(polyfill2(a));
console.log(polyfill2(b));
console.log(polyfill2(c));
console.log(polyfill2(d));
console.log(polyfill2(e));
console.log(polyfill2(f));```

Output

```node app
1.1752011936438014
-1.1752011936438014
0
27.28991719712775
-3.626860407847019
3.626860407847019

1.1752011936438014
-1.1752011936438014
0
27.28991719712775
-3.626860407847019
3.626860407847019

1.1752011936438014
-1.1752011936438014
0
27.28991719712775
-3.6268604078470186
3.626860407847019```

Javascript Math max()

Javascript Math exp()

Javascript Math pow()

Javascript Math tanh()

Javascript Math sqrt()

The post Javascript Math sinh() Function Example appeared first on AppDividend.

]]>
https://appdividend.com/2020/03/26/javascript-math-sinh-function-example/feed/ 0
JavaScript Math tanh() Function Example https://appdividend.com/2020/03/25/javascript-math-tanh-function-example/ https://appdividend.com/2020/03/25/javascript-math-tanh-function-example/#respond Wed, 25 Mar 2020 09:42:15 +0000 http://localhost/wordpress/?p=15915

If you want to find the hyperbolic tangent value of a given argument in JavaScript, then use the Math.tanh() method is used. Javascript tanh() method comes handy in programming contexts dealing with any trigonometric expressions. The tanh() is a static method of Math, and it can be used without creating an object. JavaScript Math tanh() […]

The post JavaScript Math tanh() Function Example appeared first on AppDividend.

]]>

If you want to find the hyperbolic tangent value of a given argument in JavaScript, then use the Math.tanh() method is used. Javascript tanh() method comes handy in programming contexts dealing with any trigonometric expressions. The tanh() is a static method of Math, and it can be used without creating an object.

JavaScript Math tanh()

Javascript tanh() is a static method of Math, and it can be used without creating an object.

Syntax

```Math.tanh(x)
```

Parameter(s)

The variable x, whose hyperbolic tangent value is to be determined.

Return Value

A number which is the hyperbolic tangent value.

Polyfill

Polyfill is a code that can be used in place of this method for browsers that do not support the Javascript tanh() method.

```Math.tanh = Math.tanh || function (x) {
var a = Math.exp(x), b = Math.exp(-x);
return a == Infinity ? 1 : b == Infinity ? -1 : (a - b) / (a + b);
};```

See the following figure.

Note

1. If the passed value is not a valid number, the method returns NaN.
2. If the passed value is ±Infinity, the method returns ±1.
3. If the passed value is ±0, the method returns the value that is passed.

Compatibility(Version and above)

2. Firefox v25
3. Edge v12
4. Opera v25
5. Safari v8
6. Android webview v38
7. Chrome for Android v38
8. Firefox for Android v25
9. Opera for Android v25
10. Safari on iOS v8
11. Samsung Internet v3.0
12. Node.js v0.12

Non-compatible with: Internet Explorer

JavaScript version: ECMAScript 6

Consider the following examples.

Javascript tanh() function example

See the following code.

```// app.js

let a = 1;
let b = -1;
let c = 10;
let d = 4;
let e = -2;
let f = 2;

console.log(Math.tanh(a));
console.log(Math.tanh(b));
console.log(Math.tanh(c));
console.log(Math.tanh(d));
console.log(Math.tanh(e));
console.log(Math.tanh(f));```

Output

```node example1
0.7615941559557649
-0.7615941559557649
0.9999999958776927
0.999329299739067
-0.9640275800758169
0.9640275800758169
```

Example 2

The following example demonstrates the case where values other than valid numbers are passed.

```// app.js

let a = "Hello, world";
let b;

console.log(Math.tanh(a));
console.log(Math.tanh(b));
```

Output

```node example2
NaN
NaN
```

Example 3

The tanh() method cannot be used with complex arguments as only integer arguments are accepted.

```// app.js

// Complex values cannot be passed as arguments as follows
// Since only integer arguments are accepted.

console.log(Math.tanh(2 + i));
```

Output

```ReferenceError: i is not defined
```

Example 4

The following example demonstrates the situation when an infinite value is passed.

```// app.js

let a = Infinity;
let b = -Infinity;

console.log(Math.tanh(a));
console.log(Math.tanh(b));
```

Output

```node app
1
-1```

Example 5

The following example demonstrates the situation when a positive or negative zero is passed.

```// app.js

let a = 0;
let b = -0;

console.log(Math.tanh(a));
console.log(Math.tanh(b));

```

Output

```node example5
0
-0
```

Example 6

The following example demonstrates the use of polyfill for this method.

```// app.js

let a = 1;
let b = -1;
let c = 0;
let d = 10;
let e = Infinity;
let f = -Infinity;

function polyfill(x) {
let a = Math.exp(x), b = Math.exp(-x);
return a == Infinity ? 1 : b == Infinity ? -1 : (a - b) / (a + b);
}

console.log(Math.tanh(a));
console.log(Math.tanh(b));
console.log(Math.tanh(c));
console.log(Math.tanh(d));
console.log(Math.tanh(e));
console.log(Math.tanh(f));
console.log();

console.log(polyfill(a));
console.log(polyfill(b));
console.log(polyfill(c));
console.log(polyfill(d));
console.log(polyfill(e));
console.log(polyfill(f));```

Output

```node app
0.7615941559557649
-0.7615941559557649
0
0.9999999958776927
1
-1

0.7615941559557649
-0.7615941559557649
0
0.9999999958776926
1
-1```

Javascript Math max()

Javascript Math exp()

Javascript Math pow()

Javascript Math abs()

Javascript Math sqrt()

Javascript Math min()

The post JavaScript Math tanh() Function Example appeared first on AppDividend.

]]>
https://appdividend.com/2020/03/25/javascript-math-tanh-function-example/feed/ 0
JavaScript Math min() Function Example https://appdividend.com/2020/03/25/javascript-math-min-function-example/ https://appdividend.com/2020/03/25/javascript-math-min-function-example/#respond Wed, 25 Mar 2020 08:41:17 +0000 http://localhost/wordpress/?p=15908

If you want to find the minimum out of a set of numbers in JavaScript, the Math.min() method is used. Javascript min() method comes handy in the most basic of programs as well as in programs dealing with advanced algorithms. JavaScript Math.min() method initially compares with positive infinity, and that is why if no argument […]

The post JavaScript Math min() Function Example appeared first on AppDividend.

]]>

If you want to find the minimum out of a set of numbers in JavaScript, the Math.min() method is used. Javascript min() method comes handy in the most basic of programs as well as in programs dealing with advanced algorithms. JavaScript Math.min() method initially compares with positive infinity, and that is why if no argument is passed, this method returns positive infinity.

JavaScript Math min()

Javascript min() is a static method of Math, and it can be used without creating an object. Javascript static function Math.min() returns the lowest-valued number passed into it, or NaN if any parameter isn’t a number and can’t be converted into one.

Syntax

`Math.min(x, y, z, ...)`

Parameter(s)

The numbers out of which the minimum is to be determined.

Return Value

The minimum of all the parameters passed.

See the following method.

Note

1. If no argument is passed, this method returns positive infinity.
2. If any of the arguments cannot be converted into a valid number, this method returns NaN.
3. If the parameter is null, this method treats it as 0.
4. If the parameter is an empty string, this method treats it as 0.
5. If the parameter is an empty array, this method treats it as 0.
6. If one argument is positive zero, and the other argument is negative zero, then min() method returns negative zero as it considers negative zero to be strictly smaller than positive zero.

Compatibility(Version and above)

• Internet Explorer v3
• Firefox v1
• Edge v12
• Opera v3
• Safari v1
• Android webview v1
• Chrome for Android v18
• Firefox for Android v4
• Opera for Android v10.1
• Safari on iOS v1
• Samsung Internet v1.0
• Node.js

JavaScript version: ECMAScript 1

Consider the following examples.

Javascript min() function example

The following example demonstrates the use of the min() method.

```// app.js

console.log(Math.min(1, 2, 3));
console.log(Math.min(-1, 2, 3, -6));
console.log(Math.min(0, 0, -3));
console.log(Math.min(-1, -2));
console.log(Math.min(-5, -2, -3, 1));
```

Output

```node app
1
-6
-3
-2
-5```

Example 2

The following example demonstrates the case no argument is passed.

See the following code.

```// app.js

console.log(Math.min());```

Output

```node app
Infinity
```

Example 3

The following code demonstrates the cases where NaN is returned and cases where it can be avoided.

```// app.js

var a = "JavaScript"; 	                //non-numeric string
var b = [1, 2, 3, 4]; 		             //array with more than one element
var c;					//undefined variable
var d = {};				//empty object

console.log(Math.min(a, 1));
console.log(Math.min(b, 2));
console.log(Math.min(c, 3));
console.log(Math.min(d, 4));

var e = "23";  			           //numeric string
var f = [10]; 			         //array with a single element

console.log(Math.min(e, 5));
console.log(Math.min(f, 1));```

Output

```node app
NaN
NaN
NaN
NaN
5
1
```

Example 4

The following code demonstrates the cases where null, empty string, or an empty object is passed.

```// app.js

let a = null;
let b = "";
let c = [];

console.log(Math.min(a, 1));
console.log(Math.min(b, 2));
console.log(Math.min(c, 3));
```

Output

```node app
0
0
0
```

Example 5

The following example demonstrates the case where the minimum out of positive zero and negative zero is to be determined.

```// app.js

let a = 0;
let b = -0;

console.log(Math.min(a, b));
```

Output

```node app
-0
```

Find a minimum element of an array in Javascript

Javascript min() method is not designed to handle an array with multiple elements being passed as a parameter directly.

The following example demonstrates an array with multiple elements being passed as a parameter directly.

```// app.js

let arr = [1, 2, 3, 4];
console.log(Math.min(arr));
```

Output

```node app
NaN
```

However, the min() method can be used to find the minimum of an array of elements in the following ways.

1. Using the Array.reduce() method.
2. Using the Function.prototype.apply() method.

Using Array.reduce() method

To find the minimum element in an array, the Array.reduce() method can be used to compare all the elements of the Array.

```// app.js

let arr = [1, 2, 3, 4];
let min_element = arr.reduce(function (x, y) {
return Math.min(x, y);
});
```

Consider the following example.

The following example demonstrates the use of the Array.reduce() method with min() to find the minimum element of an array.

```// app.js

let arr = [34, 54, 0, -70, 64];
let min_element = arr.reduce(function (x, y) {
return Math.min(x, y);
});

console.log(min_element);
```

Output

```node example7
-70
```

Using Function.prototype.apply() method

Using Function.prototype.apply(), one can find the minimum element of an array. However, this must be used on arrays having only a few numbers of elements.

See the following code.

```function findMin(arr){
return Math.min.apply(null, arr);
}
```

Consider the following example.

The following example demonstrates the use of the Function.prototype.apply() method with min() to find the minimum element of an array.

```// app.js

function findMin(arr) {
return Math.min.apply(null, arr);
}

let new_arr = [45, 67, -92, 100];

console.log(findMin(new_arr));

```

Output

```node example8
-92
```

The above apply() method can be implemented with the help of the spread operator (…) as well. Again, this must be used on arrays having only a few numbers of elements.

```var min = Math.min(...arr);
```

The following example demonstrates the use of the spread operator with min() to find the minimum element of an array.

```// app.js

let arr=[10,20,-90,6];
console.log(Math.min(...arr));
```

Output

```node example9
-90```

Javascript Math trunc()

Javascript Math max()

Javascript Math exp()

Javascript Math pow()

Javascript Math abs()

Javascript Math sqrt()

The post JavaScript Math min() Function Example appeared first on AppDividend.

]]>
https://appdividend.com/2020/03/25/javascript-math-min-function-example/feed/ 0
JavaScript Math sqrt() Function Example https://appdividend.com/2020/03/25/javascript-math-sqrt-function-example/ https://appdividend.com/2020/03/25/javascript-math-sqrt-function-example/#respond Wed, 25 Mar 2020 06:53:36 +0000 http://localhost/wordpress/?p=15902

JavaScript Math sqrt() is an inbuilt method that is used to find the square root of any number. Although the square roots of any number are positive and negative, respectively, the Math.sqrt() method returns only the positive value. The sqrt() is a static method of Math, and it can be used without creating an object. […]

The post JavaScript Math sqrt() Function Example appeared first on AppDividend.

]]>

JavaScript Math sqrt() is an inbuilt method that is used to find the square root of any number. Although the square roots of any number are positive and negative, respectively, the Math.sqrt() method returns only the positive value. The sqrt() is a static method of Math, and it can be used without creating an object.

JavaScript Math sqrt()

If you want to find a square root of a number in Javascript, then use Math.sqrt() function. Since sqrt() is the static method of Math, it can be used without creating an object.

Syntax

`Math.sqrt(x)`

Parameter(s)

The variable x, whose square root value is to be determined.

Return Value

The non-negative square root value.

See the following method.

Note

1. If the argument is an array with a single element, the method returns the square root of that element.
2. If the argument is an array with more than one element, the method returns NaN.
3. If the passed value is not a valid number, the method returns the NaN.
4. If a negative number is passed, a method returns NaN.
5. If an argument is a negative infinity, then this method returns NaN.
6. If an argument is a positive infinity, then this method returns positive infinity.
7. If an argument is positive zero or negative zero, then this method returns the same value that is passed.

If the passed value is null, the method returns 0.

Compatibility(Version and above):

2. Firefox v1
3. Edge v12
4. Internet Explorer v3
5. Opera v3
6. Safari v1
7. Android webview v1
8. Chrome for Android v18
9. Firefox for Android v4
10. Opera for Android v10.1
11. Safari on iOS v1
12. Samsung Internet v1.0
13. Node.js

JavaScript version: ECMAScript 1

Consider the following examples.

Javascript sqrt() example

The following example demonstrates the use of the sqrt() method.

```// app.js

let a = 4;
let b = 0;
let c = 14;
let d = [9]; // array with single element

console.log(Math.sqrt(a));
console.log(Math.sqrt(b));
console.log(Math.sqrt(c));
console.log(Math.sqrt(d));
```

Output

```node app
2
0
3.7416573867739413
3```

Example 2

The following example demonstrates the cases where NaN is returned.

```// app.js

let a = "Hello, world"; 		//non-numeric string
let b; 					//empty variable
let c = -4;				//negative number
let d = [1, 2, 3, 4];			//array with more than one elements
let e = {};				//empty object

console.log(Math.sqrt(a));
console.log(Math.sqrt(b));
console.log(Math.sqrt(c));
```

Output

```node app
NaN
NaN
NaN
NaN
NaN```

Example 3

The sqrt() method cannot be used with complex arguments as only integer arguments are accepted.

```// Complex values cannot be passed as arguments as follows
// since only integer arguments are accepted.

console.log(Math.sqrt(2 + i));
```

Output

```node app
/Users/krunal/Desktop/code/node-examples/es/app.js:5
console.log(Math.sqrt(2 + i));
^

ReferenceError: i is not defined
at Object.<anonymous> (/Users/krunal/Desktop/code/node-examples/es/app.js:5:27)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
at internal/main/run_main_module.js:17:47```

Example 4

The following example demonstrates the cases when positive infinity or negative infinity is passed as a parameter.

```// app.js

var a = Number.NEGATIVE_INFINITY;
var b = Number.POSITIVE_INFINITY;

console.log(Math.sqrt(a));
console.log(Math.sqrt(b));
```

Output

```node example4
NaN
Infinity
```

Example 5

The following example demonstrates the case of positive or negative zero is passed as a parameter.

```// app.js

let a = 0;
let b = -0;

console.log(Math.sqrt(a));
console.log(Math.sqrt(b));
```

Output

```node example5
0
-0
```

Example 6

The following example demonstrates the cases where zero is returned.

```// app.js

let a = null;
let b = "";	//empty string
let c = [];	//empty array

console.log(Math.sqrt(a));
console.log(Math.sqrt(b));
console.log(Math.sqrt(c));```

Output

```node example6
0
0
0
```

A real-life example of Javascript sqrt() function

Given the two sides of a right-angled triangle, find the hypotenuse.

```// Given two sides of a right-angled triangle,
// find the hypotenuse.

let side_1;
let side_2;

const rl = r.createInterface({
input: process.stdin,
output: process.stdout
});

const prompt1 = () => {
return new Promise((resolve, reject) => {
rl.question('Side 1: ', (answer) => {
resolve();
});
});
};

const prompt2 = () => {
return new Promise((resolve, reject) => {
rl.question('Side 2: ', (answer) => {
resolve();
});
});
};

const main = async () => {
await prompt1();
await prompt2();
rl.close();

let hypotenuse = Math.sqrt(side_1 * side_1 + side_2 * side_2);
console.log("Hypotenuse: " + hypotenuse);
}

main();
```

Output

```Test Case 1:
->node example7
Side 1: 3
Side 2: 4
Hypotenuse: 5

Test Case 2:
->node example7
Side 1: 15
Side 2: 8
Hypotenuse: 17
```

Javascript Math trunc()

Javascript Math max()

Javascript Math exp()

Javascript Math pow()

Javascript Math abs()

The post JavaScript Math sqrt() Function Example appeared first on AppDividend.

]]>
https://appdividend.com/2020/03/25/javascript-math-sqrt-function-example/feed/ 0
JavaScript Math pow() Function Example https://appdividend.com/2020/03/24/javascript-math-pow-function-example/ https://appdividend.com/2020/03/24/javascript-math-pow-function-example/#respond Tue, 24 Mar 2020 18:58:58 +0000 http://localhost/wordpress/?p=15896

JavaScript Math pow() is an inbuilt method that is used to find the value of a number raised to an exponent. Javascript pow() method can come handy in several programs dealing with mathematical expressions. JavaScript Math pow() If you want to find the power of a number in Javascript, then Math.pow() function is used. Since […]

The post JavaScript Math pow() Function Example appeared first on AppDividend.

]]>

JavaScript Math pow() is an inbuilt method that is used to find the value of a number raised to an exponent. Javascript pow() method can come handy in several programs dealing with mathematical expressions.

JavaScript Math pow()

If you want to find the power of a number in Javascript, then Math.pow() function is used. Since the Math.pow() is a static method of Math and therefore it is always used as Math.pow() and not as a method of an object created of Math class.

Syntax

`Math.pow(x, y)`

Numbers x and y, such that

x: The base of the power.

y: The exponent to which the base is raised.

Return Value

The value xy, i.e., x multiplied to itself y times.

See the following figure.

Note

1. If the base is negative and the exponent is integral, the value returned is positive for even exponent values and negative for odd exponent values.
2. If the base is negative and the exponent is fractional, the method will always return NaN.
3. If the exponent is positive or negative zero, the method will always return 1.

Compatibility(Version and above)

2. Internet Explorer v3
3. Edge v12
4. Firefox v1
5. Opera v3
6. Safari v1
7. Android webview v1
8. Chrome for Android v18
9. Firefox for Android v4
10. Opera for Android v10.1
11. Safari on iOS v1
12. Samsung Internet v1.0
13. Node.js

JavaScript version: ECMAScript 1

Consider the following examples:

The following example demonstrates the use of this method with both base and exponent as non-negative integral values.

```// app.js

console.log(Math.pow(2, 2));
console.log(Math.pow(2, 3));
console.log(Math.pow(2, 4));
console.log(Math.pow(0, 2));
console.log(Math.pow(6, 3));
```

Output

```node app
4
8
16
0
216
```

The following example demonstrates the use of this method with the exponent having a fractional value.

```// app.js

console.log(Math.pow(4, 0.5));		  //square root of 4
console.log(Math.pow(27, 1 / 3)); 		 //cube root of 27
console.log(Math.pow(196, 0.5));		//square root of 196
console.log(Math.pow(16, 1 / 4)); 	           //fourth root of 16
```

Output

```node app
2
3
14
2
```

Example 3

The following example demonstrates the use of this method with the exponent having a negative value.

```// app.js

console.log(Math.pow(1/4,-2));
console.log(Math.pow(4,-0.5));
```

Output

```node app
16
0.5
```

Example 4

The following example demonstrates the use of this method with the base having a negative value and the exponent having an integral value. In such a case, the result will have a negative value for odd exponents and a positive value for even exponents.

See the following code.

```// app.js

console.log(Math.pow(-2,2));
console.log(Math.pow(-2,3));
```

Output

```node app
4
-8
```

Example 5

The following example demonstrates the use of this method with the base having a negative value and the exponent having a fractional value. In such a case, this method always returns NaN.

```// app.js

console.log(Math.pow(-4,0.5));
console.log(Math.pow(-27,1/3));
console.log(Math.pow(-4,-0.5));
```

Output

```node app
NaN
NaN
NaN
```

Example 6

The following example demonstrates the use of this method when the exponent is positive or negative zero. In such a case, this method always returns 1.

```// app.js

console.log(Math.pow(2, 0));
console.log(Math.pow(2, -0));
```

Output

```node example6
1
1
```

Example 7

The following example demonstrates the application of this method in a simple programming context.

Given the side of a square, find its area.

```// app.js

// Given the side of a square, find its area.

let side;

const rl = r.createInterface({
input: process.stdin,
output: process.stdout
});

const prompt1 = () => {
return new Promise((resolve, reject) => {
resolve();
});
});
};

const main = async () => {
await prompt1();
rl.close();

var area = Math.pow(side, 2);
console.log("Area: " + area);
}

main();```

Output

```Test Case 1:
->node example7
Side: 2
Area: 4

Test Case2:
->node example7
Side: 4
Area: 16

```

Javascript Math trunc()

Javascript Math max()

Javascript Math exp()

Javascript Math acos()

Javascript Math abs()

The post JavaScript Math pow() Function Example appeared first on AppDividend.

]]>
https://appdividend.com/2020/03/24/javascript-math-pow-function-example/feed/ 0
JavaScript Math trunc() Function Example https://appdividend.com/2020/03/24/javascript-math-trunc-function-example/ https://appdividend.com/2020/03/24/javascript-math-trunc-function-example/#respond Tue, 24 Mar 2020 17:39:20 +0000 http://localhost/wordpress/?p=15888

JavaScript Math trunc() is an inbuilt method that is used to return an integral part of any number by removing the part after the decimal point(including the decimal point). The trunc() method is different from the ceil() or floor() methods as it does not round up the value to the nearest integer rather just remove […]

The post JavaScript Math trunc() Function Example appeared first on AppDividend.

]]>

JavaScript Math trunc() is an inbuilt method that is used to return an integral part of any number by removing the part after the decimal point(including the decimal point). The trunc() method is different from the ceil() or floor() methods as it does not round up the value to the nearest integer rather just remove the part after the decimal point, but can be implemented with the help of these methods (as demonstrated in the polyfill section).

JavaScript Math trunc()

JavaScript, trunc() is a function used to return the integer portion of a number. It truncates the number and removes all fractional digits. Since trunc() is a static method of Math, it can be used without creating an object.

Syntax

```Math.trunc(x)
```

Parameter(s)

The floating-point number whose integral part is to be returned.

Return Value

The integral part of x by removing the part after the decimal point.

Polyfill

Polyfill is code that can be used in place of this method for browsers which do not support this method.

```Math.trunc = function(x){
x = +x;
if(!isFinite(x)) return x;
return (x – x%1) || (x<0 ? -0 : x === 0 ? x : 0);
};
```

OR

```Math.trunc = function(x){
return x<0 ? Math.ceil(x) : Math.floor(x);
};
```

See the following figure.

Note

1. If the passed value is not a valid number, the method returns NaN.
2. If the passed value is null, the method returns 0.
3. For negative values, this method gives the same output as Math.ceil()
4. For positive values, this method gives the same output as Math.floor()

Compatibility(Version and above)

2. Firefox v25
3. Edge v12
4. Opera v25
5. Safari v8
6. Android webview v38
7. Chrome for Android v38
8. Firefox for Android v25
9. Opera for Android v25
10. Safari on iOS v8
11. Samsung Internet v3.0
12. Node.js v0.12

Not compatible with: Internet Explorer

JavaScript version: ECMAScript 6

Consider the following examples:

The following code shows the use of the js trunc() method.

```// app.js

let a = 12.25;
let b = -6.3;
let c = 0.35;
let d = -0.2;
let e = [4.3]; // array with single element

console.log(Math.trunc(a));
console.log(Math.trunc(b));
console.log(Math.trunc(c));
console.log(Math.trunc(d));
console.log(Math.trunc(e));
```

Output

```node example1
12
-6
0
-0
4
```

Javascript trunc(): NaN is returned

The following example shows the cases where NaN is returned.

```// app.js

let a = "Hello, world"; 		// non-numeric string
let b; 					// empty variable
let c = [1, 2, 3, 4];			// array with more than one elements
let d = {};				// empty object

console.log(Math.trunc(a));
console.log(Math.trunc(b));
console.log(Math.trunc(c));
console.log(Math.trunc(d));```

Output

```node app
NaN
NaN
NaN
NaN
```

Javascript trunc(): zero is returned

The following example demonstrates the cases where zero is returned.

```// app.js

let a = null;
let b = "";	// empty string
let c = [];	// empty array

console.log(Math.trunc(a));
console.log(Math.trunc(b));
console.log(Math.trunc(c));
```

Output

```node app
0
0
0
```

Demonstrates the polyfill methods for trunc()

The following example demonstrates the polyfill methods for trunc().

```// app.js

let a = 12.25;
let b = -6.3;
let c = 0.35;
let d = -0.2;
let e = [4.3]; 				// array with single element

function polyfill1(x) {
x = +x;
if (!isFinite(x)) return x;
return (x - x % 1) || (x < 0 ? -0 : x === 0 ? x : 0);
}

function polyfill2(x) {
return x < 0 ? Math.ceil(x) : Math.floor(x);
}

console.log(Math.trunc(a));
console.log(Math.trunc(b));
console.log(Math.trunc(c));
console.log(Math.trunc(d));
console.log(Math.trunc(e));
console.log();

console.log(polyfill1(a));
console.log(polyfill1(b));
console.log(polyfill1(c));
console.log(polyfill1(d));
console.log(polyfill1(e));
console.log();

console.log(polyfill2(a));
console.log(polyfill2(b));
console.log(polyfill2(c));
console.log(polyfill2(d));
console.log(polyfill2(e));
```

Output

```node app
12
-6
0
-0
4

12
-6
0
-0
4

12
-6
0
-0
4```

The following example demonstrates the scenario where the trunc method can be used in a simple programming context.

Consider the following problem:

Let’s say a startup has decided to hire new engineers to build their product. But they have a fixed budget. Given the fixed budget and the salary of an engineer, find out the maximum number of engineers the startup can hire.

```// app.js

let budget;
let salary;

const rl = r.createInterface({
input: process.stdin,
output: process.stdout
});

const prompt1 = () => {
return new Promise((resolve, reject) => {
resolve();
});
});
};

const prompt2 = () => {
return new Promise((resolve, reject) => {
resolve();
});
});
};

const main = async () => {
await prompt1();
await prompt2();
rl.close();

console.log(Math.trunc(budget / salary));
}

main();

```

Output

```Test Case 1:
->node example5
Budget: 200000
Salary: 26000
7

Test Case 2:
->node example5
Budget: 250000
Salary: 100000
2
```

Javascript Math max()

Javascript Math exp()

Javascript Math acos()

Javascript Math abs()

Javascript Math acosh()

The post JavaScript Math trunc() Function Example appeared first on AppDividend.

]]>
https://appdividend.com/2020/03/24/javascript-math-trunc-function-example/feed/ 0
JavaScript Math max() Function | max() in Javascript https://appdividend.com/2020/02/07/javascript-math-max-function-max-in-javascript/ https://appdividend.com/2020/02/07/javascript-math-max-function-max-in-javascript/#respond Fri, 07 Feb 2020 08:59:20 +0000 http://localhost/wordpress/?p=14482

JavaScript Math max() is an inbuilt function that is used to find the maximum out of a set of numbers. The max() method comes handy in the most basic of programs as well as in programs dealing with advanced algorithms. JavaScript Math.max() method initially compares with negative infinity, and that is why if no argument […]

The post JavaScript Math max() Function | max() in Javascript appeared first on AppDividend.

]]>

JavaScript Math max() is an inbuilt function that is used to find the maximum out of a set of numbers. The max() method comes handy in the most basic of programs as well as in programs dealing with advanced algorithms. JavaScript Math.max() method initially compares with negative infinity, and that is why if no argument is passed, this method returns negative infinity.

JavaScript Math max()

Javascript max() function returns the number with the highest value. This function accepts Value1,Value2 Values sent to math.max() function for finding the largest.

Syntax

```Math.max(x, y, z, ...)   // can pass any number of arguments
```

Parameters

The numbers out of which the maximum is to be determined.

Return Value

The maximum of all the parameters passed.

See the following figure.

Note

1. If no argument is passed, this method returns negative infinity.
2. If any of the arguments cannot be converted into a valid number, this method returns NaN.
3. If the parameter is null, the max() method returns 0.
4. If a parameter is an empty string, the max() method returns 0.
5. If the parameter is an empty array, the max() method returns 0.
6. If one argument is positive zero, and the other argument is negative zero, this method returns positive zero as it considers negative zero to be strictly smaller than positive zero.

Compatibility(Version and above)

2. Internet Explorer v3
3. Firefox v1
4. Edge v12
5. Opera
6. Safari v1
7. Android webview v1
8. Chrome for Android v18
9. Firefox for Android v4
10. Opera for Android
11. Safari on iOS v1
12. Samsung Internet v1.0
13. Node.js

JavaScript version: ECMAScript 1

Consider the following examples.

example1.js

The following example demonstrates the use of the js max() method.

```// example1.js

console.log(Math.max(1, 2, 3));
console.log(Math.max(-1, 2, 3, -6));
console.log(Math.max(0, 0, -3));
console.log(Math.max(-1, -2));
console.log(Math.max(-5, -2, -3, 1));
```

Output

```node example1
3
3
0
-1
1
```

example2.js

The following example demonstrates the case no argument is passed.

```//example2.js

console.log(Math.max());
```

Output

```node example2
-Infinity
```

example3.js

The following example demonstrates the cases where NaN is returned and cases where it can be avoided.

```//example3.js

var a = "JavaScript"; 	       // non-numeric string
var b = [1, 2, 3, 4]; 	       // array with more than one element
var c;			       // undefined variable
var d = {};		       // empty object

console.log(Math.max(a, 1));
console.log(Math.max(b, 2));
console.log(Math.max(c, 3));
console.log(Math.max(d, 4));

var e = "23";  			// numeric string
var f = [10]; 			// array with a single element

console.log(Math.max(e, 5));
console.log(Math.max(f, 1));```

Output

```node example3
NaN
NaN
NaN
NaN
23
10
```

example4.js

The following example demonstrates the cases where 0 is returned.

```// example4.js

var a = null;
var b = "";
var c = [];

console.log(Math.max(a, -1));
console.log(Math.max(b, -2));
console.log(Math.max(c, -3));```

Output

```node example4
0
0
0
```

example5.js

The following example demonstrates the case where the maximum out of positive zero and negative zero is to be determined.

```// example5.js

var a = 0;
var b = -0;

console.log(Math.max(a, b));
```

Output

```node example5
0
```

Finding the maximum element of an array in Javascript

The max() method is not designed to handle an array with multiple items being passed as a parameter directly.  Consider the following example:

example6.js

The following example demonstrates an array with various elements being passed as a parameter directly.

```// example6.js

var arr = [1, 2, 3, 4];
console.log(Math.max(arr));
```

Output

```node example6
NaN
```

However, the max() method can be used to find the maximum of an array of elements in the following ways:

1. Using a Javascript array.reduce() method.
2. Using the Function.prototype.apply() method.

Using Array.reduce() method

To find the maximum element in an array, the Array.reduce() method can be used to compare all the elements of the Array.

```var arr = [1, 2, 3, 4];
var max_element = arr.reduce(function (x, y) {
return Math.max(x, y);
});
```

Consider the following example.

example7.js

The following example demonstrates the use of the Array.reduce() method with max() to find the maximum element of an array.

```// example7.js

var arr = [34, 54, 0, -70, 64];
var max_element = arr.reduce(function (x, y) {
return Math.max(x, y);
});

console.log(max_element);```

Output

```node example7
64
```

Using Function.prototype.apply() method

Using Function.prototype.apply(), one can find the maximum element of an array. However, this must be used on arrays having only a few numbers of elements.

```function findMax(arr){
return Math.max.apply(null, arr);
}
```

Consider the following example.

example8.js

The following example demonstrates the use of the Function.prototype.apply() method with max() to find the maximum element of an array.

```// example8.js

function findMax(arr) {
return Math.max.apply(null, arr);
}

var new_arr = [45, 67, -92, 100];

console.log(findMax(new_arr));```

Output

```node example8
100
```

The above apply() method can be implemented with the help of the spread operator (…) as well. Again, this must be used on arrays having only a few numbers of elements.

var max = Math.max(…arr);

Consider the following example.

example9.js

The following example demonstrates the use of the spread operator with max() to find the maximum element of an array.

```// example9.js

var arr=[10, 20, -90, 6];
console.log(Math.max(...arr));
```

Output

```node example9
20```

Javascript Math.floor()

Javascript Math.random()

Javascript Math.cbrt()

Javascript Math.exp()

Javascript Math.log()

The post JavaScript Math max() Function | max() in Javascript appeared first on AppDividend.

]]>
https://appdividend.com/2020/02/07/javascript-math-max-function-max-in-javascript/feed/ 0
Javascript Math floor() Function | floor() in Javascript https://appdividend.com/2020/02/07/javascript-math-floor-function-floor-in-javascript/ https://appdividend.com/2020/02/07/javascript-math-floor-function-floor-in-javascript/#respond Fri, 07 Feb 2020 06:58:01 +0000 http://localhost/wordpress/?p=14469

JavaScript Math floor() is an inbuilt function that is used to find the nearest integer less than the passed value(or equal to, if the passed value is itself an integer). Floor values are often required in various algorithm-building scenarios dealing with mathematical operations and can be directly seen in different mathematical formulae as well. Javascript […]

The post Javascript Math floor() Function | floor() in Javascript appeared first on AppDividend.

]]>

JavaScript Math floor() is an inbuilt function that is used to find the nearest integer less than the passed value(or equal to, if the passed value is itself an integer). Floor values are often required in various algorithm-building scenarios dealing with mathematical operations and can be directly seen in different mathematical formulae as well.

Javascript Math floor()

JavaScript floor() is the function that is used to return the largest integer value that is less than or equal to a number. In other words, the floor() function rounds a number down and returns an integer value. Since floor() is a static method of Math, it can be used without creating an object.

Syntax

`Math.floor(x)`

Parameters

The number whose floor value is to be calculated.

Return Value

For integral values, returns the passed value.

For non-integral values, returns the nearest integer less than the passed value.

See the following figure.

Note

1. If no argument is passed, this method returns negative NaN.
2. If any of the arguments cannot be converted into a valid number, this method returns NaN.
3. If a parameter is null, this method returns 0.
4. If a parameter is an empty string, this method returns 0.
5. If a parameter is an empty array, this method returns 0.
6. This method returns the same value for Math.ceil(x) and -Math.floor(-x).

Compatibility(Version and above)

2. Internet Explorer v3
3. Firefox v1
4. Edge v12
5. Opera
6. Safari v1
7. Android webview v1
8. Chrome for Android v18
9. Firefox for Android v4
10. Opera for Android
11. Safari on iOS v1
12. Samsung Internet v1.0
13. Node.js

JavaScript version: ECMAScript 1

Consider the following examples.

example1.js

The following example demonstrates the use of this method.

```// example1.js

var a = 34.23;
var b = 32;

console.log(Math.floor(a));
console.log(Math.floor(b));
```

Output

```node example1
34
32
```

example2.js

The following example demonstrates the case no argument is passed.

```// example2.js

console.log(Math.floor());
```

Output

```node example2
NaN
```

example3.js

The following example demonstrates the cases where NaN is returned and cases where it can be avoided.

```// example3.js

var a = "JavaScript"; 	   // non-numeric string
var b = [1, 2, 3, 4];      // array with more than one element
var c;		           // undefined variable
var d = {};                // empty object

console.log(Math.floor(a));
console.log(Math.floor(b));
console.log(Math.floor(c));
console.log(Math.floor(d));

var e = "23.5";  		//numeric string
var f = [10.2]; 		//array with a single element

console.log(Math.floor(e));
console.log(Math.floor(f));
```

Output

```node example3
NaN
NaN
NaN
NaN
23
10
```

example4.js

The following example demonstrates the cases where 0 is returned.

```// example4.js

var a = null;
var b = "";
var c = [];

console.log(Math.floor(a));
console.log(Math.floor(b));
console.log(Math.floor(c));
```

Output

```node example4
0
0
0
```

example5.js

The following example demonstrates the case where a negative argument is passed.

```// example5.js

var a = -2.4;
var b = 2.4;

console.log(Math.floor(a));
console.log(Math.floor(b));
```

Output

```node example5
-3
2
```

example6.js

The following example demonstrates that Math.ceil(x) returns same value as -Math.floor(-x).

```// example6.js

var x = 45.6;

console.log(Math.ceil(x));
console.log(-Math.floor(-x));
```

Output

```node example6
46
46
```

example7.js

The following example demonstrates a scenario where the floor method can be used. Consider the following problem.

A startup has decided to hire new engineers to build their product. But they have a fixed budget. Given the fixed budget and the salary of an engineer, find out the maximum number of engineers the startup can hire.

```// example7.js

var budget;
var salary;

const rl = r.createInterface({
input: process.stdin,
output: process.stdout
});

const prompt1 = () => {
return new Promise((resolve, reject) => {
resolve();
});
});
};

const prompt2 = () => {
return new Promise((resolve, reject) => {
resolve();
});
});
};

const main = async () => {
await prompt1();
await prompt2();
rl.close();
console.log(Math.floor(budget / salary));
}

main();```

Output

```Test Case 1:
->node example7
Budget: 200000
Salary: 26000
7

Test Case 2:
->node example7
Budget: 250000
Salary: 100000
2```