FAQ: Introduction to JavaScript - Built-in Objects

I don’t think they support it here, because the goal is learning the fundamental syntax. But if you want intellisense/autocomplete you can use stackblitz in the browser or an IDE like Visual Studio Code

1 Like

Okay thank you! Makes sense. It’s good to know that there are options for later though.

Can somebody please explain to me what’s the difference between:

let name = “Codecademy”;
console.log(Hello, ${name});

//and

let name=“Codecademy”;
console.log(‘Hello’, name);

First big difference is the comma will not print in the second example.

The first example is a template literal, a form of string that permits variable and expression interpolation.

1 Like

Thank you sir. I appreciate your quick response. I now understand the purpose of template literal as I go deep into the lessons. I wish you a great weekend. :slight_smile:

1 Like

Bro, I got answer two by experimenting with the () it was frustrating though. but it dont make sense though because the instructions is telling me to do the exact thing as the code is written but it says another and yet in my first lesson in coding that its very literal so we have to be careful and type the exact word and number correctly. but its good to just remember those different answer because if the problem arises again in coding we can use those mistakes that we learned and implement them instead of the structions telling us whats right or wrong.

      \\ Zenistu :zap: //

hey,

does anyone know if there is a way to turn Math.floor() and Math.random() into a variable, or if that’s possible? what i mean is if you have console.log(Math.floor(Math.random()) is there a way to simplify it by making one object that randomly generates a whole number?

There may be a math object for this particular action I want to do-not sure-but the main purpose of my question is to ask if 2 math objects can be turned into 1 variable.

I also read that the math object is non-constructible; back to my question, does that mean that the variable (if possible) can only be named based on the name of the math function?

minahil :slight_smile:

const floor = Math.floor

const random = Math.random

const floorRandom = Math.floor + Math.random

console.log(floorRandom(3))

would this work?

Not in the way it is written. However, we can define a custom function using a factory…

const randInt = function () {
    return x => Math.floor(Math.random() * x)
}
let f = randInt()
for (let x of new Array(10)) {
    console.log(f(10))
}
3
5
7
2
7
8
2
4
undefined
for (let x of new Array(10)) {
    console.log(f(10))
}
8
6
5
8
5
9
7
6
9
undefined
for (let x of new Array(10)) {
    console.log(f(10))
}
8
0
7
9
0
1
3
8
7
0
undefined

Note that the max is 9 and the min is 0.


We can further refine our factory to implement an offset…

const randInt = function (b=0) {
    return x => Math.floor(Math.random() * x + b)
}
f = randInt(1)
for (let k of new Array(30) {
    console.log(f(10))
}
for (let k of new Array(30)) { console.log(f(10)) }
5
6
5
6
9
1
9
2
4
2
4
6
8
3
6
8
10
9
8
7
4
9
1
2
8
10
9
3
undefined

Now the min is 1 and the max is 10 owing that our function has a built in constant offset of 1.


Aside

for (let k of new Array(10)) {

}

This is a cheat way of counting to 10, even while k is undefined the whole time. The array still has ten elements and of is a membership operator that will find all ten of them.

1 Like

why don’t we use ’ ’ for Math.random() ?

“Find a method on the JavaScript Math object that returns the smallest integer greater than or equal to a decimal number.”

Normal people call it round a number (up/down/nearest), plus Math.round(), Math.ceil() and Math.floor() do not require a float number…

Please, fix your exercises and teach beginners properly…

1 Like

Math.random() - JavaScript | MDN (mozilla.org)

Just curious on why does Math.random() only generates number less than 1 when used only by itself. (not multiplying by anything)

Because .random() can only generate a float, meaning any number between 0 inclusive, and 1, exclusive. There is no practical reason to generate a number outside of that range.

Note that JS does not have integers, only floats.