Today we'll familiarize ourselves with ready made JS functions used/available in our browsers.

Requirement :

week2-basic-javacript/
|- monday.js
|- tuesday.js
|- wednesday.js

What You'll Learn

This is one of the most asked interview question :

Requirement

Write a function that takes two numeric arguments, a start and an end. Print out list of result following this conditions:

This should console.log:

function ziffZubb(start, end) {
  //your code here
}

//example
ziffZubb(1, 20);

Expected result

1
2
Ziff
4
Zubb
Ziff
7
8
Ziff
Zubb
11
Ziff
13
14
ZiffZubb
16
17
Ziff
19
Zubb

Another common interview question, people refer it as gcd

Write a function that takes two numeric arguments. Find the greatest number that divides evenly into both numbers.

Example: gcd(12, 18) This should return 6.

gcd(7, 15) This should return 1.

Another common interview question, people refer it as lcm Write a function called lcm, which finds the least common multiple, defined as the smallest positive integer that is divisible by both a and b.

Example: `lcm(15, 20)`
This should return 60

Examples:

Rocket: Write another function called nthPrime, that takes one number (n) and returns the nth prime.

Examples:

nthPrime(0); // returns 2, because 2 is the first prime number
nthPrime(1); // returns 3, because 3 is the second prime number
nthPrime(2); // returns 5, becaues 5 is the third prime number
nthPrime(3); // returns 7, because 7 is the fourth prime number

nthPrime(n); // returns XXX, becaues XXX is the `nth` prime number

We'll use something built into JavaScript to calculate time. Date is a class object with many useful methods. For create, modify, format and everything related to Date , use this Date manual If you type Date.now(), you'll get the number of milliseconds since January 1st, 1970 (it's a long story).

Write a function that:

Rocket: If you get this out, you can also figure out your precise age. Find the precise age for everyone in your group, expressed in Weeks.

let yourAgeInMilliseconds = Date.now() - new Date("1985-09-29");

Use your birthday, as a string as above.

Previously, we learned how to get a random floating-point number ranging from 0 to less than 1 (inclusive of 0 and exclusive of 1) with the use of the Math.random() function.

Requirement

Get random int

// From start = 5 (inclusive) to end= 8 (inclusive)
console.log(getRandomInt(5, 8)); // example output: 5 or 6 or 7 or 8

Get random item

var arr = ["apple", "orange", "kiwi"];

console.log(getRandomItem(arr)); // example: orange

Docs

A composition of loops is called a nested loop. The most common type of nested loops will be one loop inside another. The first loop is usually called the outer loop, while the second loop is called the inner loop. The outer loop always executes first. The inner loop executes inside the outer loop each time the outer loop executes once.

Run this to Take a look at the example below and visualize how the nested loop works.

for (var i = 0; i < 10; i++) {
  var s = "";
  for (var j = 0; j < 10; j++) {
    s = `${s} outer ${i} inner ${j}`;
  }
  console.log(s);
}

Output:

 outer 3 inner 0 outer 3 inner 1
 outer 3 inner 2 outer 3 inner 3
 outer 3 inner 4 outer 3 inner 5
 outer 3 inner 6 outer 3 inner 7
 outer 3 inner 8 outer 3 inner 9
 outer 4 inner 0 outer 4 inner 1
 outer 4 inner 2 outer 4 inner 3
 outer 4 inner 4 outer 4 inner 5
 outer 4 inner 6 outer 4 inner 7
 outer 4 inner 8 outer 4 inner 9

Try to explain this in your own word and take note on your own.

Assignment

1
2 2
3 3 3
4 4 4 4
...
n n n n ... n

For example, given n = 3, your function should print out:

1
2 2
3 3 3
// Expected output:
 # # # #
# # # #
 # # # #
# # # #
 # # # #
# # # #
 # # # #
# # # #

Docs

for loops are commonly used to iterate over the items in an array. To do this, we use the property length and call it on the variable associated with the array we want to iterate over. This property returns the length of, or the number of elements in, an array.

var fruits = ["apples", "oranges", "bananas"];

function listFruits() {
  for (var i = 0; i < fruits.length; i++) {
    console.log("I have some " + fruits[i]);
  }
}

Assignment

Peter gets paid $25 per hour on his remote job. The array below holds 10 numbers, the working hours he worked each day last two weeks (Sat and Sun off).

var workingHours = [6, 6, 7, 7, 8, 8, 6, 7, 8, 7];

Some things are true that aren't...

Some things are false that arent...

const one = 1;

if (one) {
  console.log("Is truthy");
}
const zero = 0;

if (zero) {
  console.log("Is truthy");
} else {
  console.log("Isn't truth");
}

Positive : We're observing coercion. It gets a lot more complicated than these examples. This guide illustrates.

See more examples of truthy here & falsy here if you want your programming skills to go over 9000.

Github links here