0% found this document useful (0 votes)
5 views5 pages

JavaScript Math Functions and Techniques

The document covers various JavaScript concepts including the Math object functions like abs(), ceil(), floor(), round(), and random(). It also explains methods for swapping variables, finding the maximum of two or three values, summing an array, finding the largest element in an array, creating a Fibonacci series using loops and recursion, and handling unexpected input. Additionally, it provides examples of selecting random numbers without repetition.

Uploaded by

Kawsar Ahmed
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views5 pages

JavaScript Math Functions and Techniques

The document covers various JavaScript concepts including the Math object functions like abs(), ceil(), floor(), round(), and random(). It also explains methods for swapping variables, finding the maximum of two or three values, summing an array, finding the largest element in an array, creating a Fibonacci series using loops and recursion, and handling unexpected input. Additionally, it provides examples of selecting random numbers without repetition.

Uploaded by

Kawsar Ahmed
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

20-1 Module Introduction, Math and Random number

●​ Research on ‘JavaScript Math’


●​ ‘[Link](number)’ function returns the absolute value of a number (For example, the
absolute value of -5 is the same as the absolute value of 5).
●​ The ceil() method rounds a number UPWARDS to the nearest integer and returns the
result.
const myNumber = 6.0395;
const output = [Link](myNumber);

Result ‘output = 7;’

●​ The floor() method rounds a number DOWNWARDS to the nearest integer and returns
the result.
const myNumber = 9.999;
const output = [Link](myNumber);

Result ‘output = 9;’

●​ The round() method rounds a number to the nearest integer.


Note: 2.49 will be rounded down (2), and 2.5 will be rounded up (3).

●​ [Link]() returns a random number between 0 (inclusive), and 1 (exclusive):

20-2 Swap variable, swap without temp, destructing

●​ First method:
var first = 5;
var second = 7;
[Link](first, second);

// first approach
var temp = first;
first = second;
second = temp;
[Link](first, second);

●​ Destructing method for swapping variable:


[first, second] = [second, first];
[Link](first, second);
20-3 Find max of two values, find max of three values

●​ To comment multiple lines of code as a block comment, press alt+shift+A together.


●​ Function for finding the largest number:​

●​ function findLargest(first, second) {


●​ if (first > second) {
●​ return first;
●​ }
●​ else {
●​ return second;
●​ }
●​ }
●​
●​ const larger = findLargest(350, 500);
●​ [Link]('largest is ', larger);

●​ The max() method returns the number with the highest value.​

var max = [Link](400, 300, 500);


[Link]('largest is ', max);
output: 500;

Tip: The min() method returns the number with the lowest value.

20-4 Sum of all numbers in an array

●​ const numbers = [44, 23, 534, 32, 54, 5, 88];


●​ function arrayTotal (numbers) {
●​ let sum = 0;
●​ for (let i = 0; i < [Link]; i++) {
●​ const element = numbers[i];
●​ sum = sum + element;
●​ }
●​ return sum;
●​ }
●​ [Link](sum);
20-5 Find the largest element of an array

●​ function largestElement(numbers) {
●​ let largest = numbers[0];
●​ for (let i = 0; i < [Link]; i++) {
●​ const element = numbers[i];
●​ if (element > largest) {
●​ largest = element;
●​ }
●​ }
●​ return largest;
●​ }
●​
●​ const ages = [12, 43, 23, 32, 75, 43, 54];
●​ const oldest = largestElement(ages);
●​ const oldest2 = largestElement([-3, -4, -17]);
●​ [Link](oldest, oldest2);

●​ Homework: Find the lowest element of an array.

20-6 Create a Fibonacci Series using a for loop

●​ Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89


●​ Code is below:​

●​ const fibo = [0, 1];


●​ for (let i = 2; i <= 10; i++) {
●​ // nth = (n - 1)th + (n - 2)th
●​ // ith = (i - 1)th + (i - 2)th
●​ fibo[i] = fibo[i-1] + fibo[i-2];
●​ }
●​ [Link](fibo);
20-7 Handle unexpected input using simple return

●​ Simple error handling code:​

function fibonacciSeries(num) {
if (typeof num != 'number') {
return 'Please give a number';
}

if(num < 2) {
return 'Please enter a positive number greater than 2';
}
const fibo = [0, 1];
for (let i = 2; i <= num; i++) {
fibo[i] = fibo[i-1] + fibo[i-2];
}
return fibo;
}

const fiboSeries = fibonacciSeries(23);


[Link](fiboSeries);

●​ Research on “Error Throw” in Javascript.

20-8 (advanced) Fibonacci Element and series Recursive way

●​ Could not understand. Need to watch this video more.

20-9 Module summary and Create Fibonacci series in a recursive way

●​ [Link]()
●​ [Link]()
●​ [Link]()
●​ [Link]()
●​ Code for selecting 5 numbers (range between 100):​

●​ for (let i = 0; i < 5; i++) {


●​ const random = [Link]() * 100;
●​ const selected = [Link](random);
●​ [Link](selected);
●​ }

●​ To avoid selecting same number for multiple time, try this code:​

●​ const selected = [];


●​ for (let i = 0; i < 10; i++) {
●​ const random = [Link]() * 100;
●​ const picked = [Link](random);
●​ if ([Link](picked) == -1) {
●​ [Link](picked);
●​ }
●​
●​ else {
●​ [Link](selected, picked);
●​ }
●​ }
●​
●​ [Link](selected);

You might also like