Numbers



Course Link

1. Generate Random Fractions with JavaScript

JavaScript has a Math.random() function that generates a random decimal number between 0 (inclusive) and not quite up to 1 (exclusive). Thus Math.random() can return a 0 but never quite return a 1

function randomFraction() {
  return Math.random();
}

2. Generate Random Whole Numbers

It’s great that we can generate random decimal numbers, but it’s even more useful if we use it to generate random whole numbers.

  • Use Math.random() to generate a random decimal.
  • Multiply that random decimal by 20.
  • Use another function, Math.floor() to round the number down to its nearest whole number.
  • Remember that Math.random() can never quite return a 1 and, because we’re rounding down, it’s impossible to actually get 20. This technique will give us a whole number between 0 and 19.
Math.floor(Math.random() * 20);

3. Generate Random Whole Numbers within a Range

function randomRange(myMin, myMax) {
  // Only change code below this line
  return Math.floor(Math.random() * (myMax - myMin + 1)) + myMin
  // Only change code above this line
}

4. Use the parseInt Function

The parseInt() function parses a string and returns an integer

function convertToInteger(str) {
  return parseInt(str)
}
# test
convertToInteger("56");

5. Use the parseInt Function with a Radix

Use parseInt() in the convertToInteger function so it converts a binary number to an integer and returns it

# parseInt(string, radix);
function convertToInteger(str) {
  return parseInt(str,2)
}

convertToInteger("10011");

6. Use the Conditional (Ternary) Operator 😃

  • The conditional operator, also called the ternary operator, can be used as a one line if-else expression.
  • The syntax is: condition ? statement-if-true : statement-if-false;
  • Example
 function findGreater(a, b) {
  if(a > b) {
    return "a is greater";
  }
  else {
    return "b is greater";
  }
}

can be re-written using the conditional operator

 function findGreater(a,b){
     return a >b ? "a is greater" : "b is greater";
 }

7. Use Multiple Conditional (Ternary) Operators

Use multiple conditional operators in the checkSign function to check if a number is positive, negative or zero. The function should return “positive”, “negative” or “zero”.

function checkSign(num) {
  return (num==0)? "zero"
  : (num>0) ? "positive"
  : "negative"
}

checkSign(10);

8. Use Recursion to Create a Countdown

The function should use recursion to return an array containing the integers n through 1 based on the n parameter. If the function is called with a number less than 1, the function should return an empty array. For example, calling this function with n = 5 should return the array [5, 4, 3, 2, 1]

function countdown(n) {
  if (n < 1) {
    return [];
  } else {
    const arr = countdown(n - 1); //index
    arr.unshift(n); // unshift
    return arr;
  }
}

9. Use Recursion to Create a Range of Numbers

We have defined a function named rangeOfNumbers with two parameters. The function should return an array of integers which begins with a number represented by the startNum parameter and ends with a number represented by the endNum parameter.

The starting number will always be less than or equal to the ending number. Your function must use recursion by calling itself and not use loops of any kind. It should also work for cases where both startNum and endNum are the same.

function rangeOfNumbers(startNum, endNum) {
  if (startNum === endNum){
    return [startNum];
  } else {
    // how many 
    var arr = rangeOfNumbers(startNum, endNum - 1);
    arr.push(endNum);
    return arr
  }
};