Saturday, June 17, 2023

JavaScript Random

Math.random()

JavaScript Math.random()

Math.random() returns a random number between 0 (included) and 1 (excluded):

0.5602783686247255

EXAMPLE

<!DOCTYPE html>

<html>

<body>


<h2>JavaScript Math.random()</h2>


<p>Math.random() returns a random number between 0 (included) and 1 (excluded):</p>


<p id="demo"></p>


<script>

document.getElementById("demo").innerHTML = Math.random();

</script>


</body>

</html>


JavaScript Random Integers

Math.random() used with Math.floor() can be used to return random integers.

JavaScript Math

Math.floor(Math.random() * 10) returns a random integer between 0 and 9 (both included):

7

EXAMPLE

<!DOCTYPE html>

<html>

<body>


<h2>JavaScript Math</h2>


<p>Math.floor(Math.random() * 10) returns a random integer between 0 and 9 (both 

included):</p>


<p id="demo"></p>


<script>

document.getElementById("demo").innerHTML =

Math.floor(Math.random() * 10);

</script>


</body>

</html>


Thursday, June 15, 2023

JavaScript Arrays Sort/Reverse

 

JavaScript Arrays

The sort() Method

The sort() method sorts an array alphabetically:

Banana,Orange,Apple,Mango

Apple,Banana,Mango,Orange


:::Code:::

<!DOCTYPE html>

<html>

<body>

<h1>JavaScript Arrays</h1>

<h2>The sort() Method</h2>


<p>The sort() method sorts an array alphabetically:</p>


<p id="demo1"></p>

<p id="demo2"></p>


<script>

const fruits = ["Banana", "Orange", "Apple", "Mango"];

document.getElementById("demo1").innerHTML = fruits;


fruits.sort();

document.getElementById("demo2").innerHTML = fruits;

</script>


</body>

</html>

JavaScript Arrays

Sort in Reverse

The reverse() method reverses the elements in an array.

By combining sort() and reverse() you can sort an array in descending order:

Banana,Orange,Apple,Mango

Orange,Mango,Banana,Apple

:::Code:::

<!DOCTYPE html>

<html>

<body>

<h1>JavaScript Arrays</h1>

<h2>Sort in Reverse</h2>


<p>The reverse() method reverses the elements in an array.</p>

<p>By combining sort() and reverse() you can sort an array in descending order:</p>


<p id="demo1"></p>

<p id="demo2"></p>


<script>

// Create and display an array:

const fruits = ["Banana", "Orange", "Apple", "Mango"];

document.getElementById("demo1").innerHTML = fruits;


// First sort the array

fruits.sort();


// Then reverse it:

fruits.reverse();


document.getElementById("demo2").innerHTML = fruits;

</script>


</body>

</html>


Tuesday, June 13, 2023

JavaScript Arrays

 

JavaScript Arrays

Why Use Arrays?

If you have a list of items (a list of car names, for example), storing the cars in single variables could look like this:

let car1 = "Saab";
let car2 = "Volvo";
let car3 = "BMW";

However, what if you want to loop through the cars and find a specific one? And what if you had not 3 cars, but 300?

The solution is an array!

An array can hold many values under a single name, and you can access the values by referring to an index number.

Creating an Array

Using an array literal is the easiest way to create a JavaScript Array.

Syntax:

const array_name = [item1, item2, ...];   <!DOCTYPE html>
<html>
<body>
<h1>JavaScript Arrays</h1>

<p id="demo"></p>

<script>
const cars = ["Saab", "Volvo", "BMW"];
document.getElementById("demo").innerHTML = cars;
</script>

</body>
</html>

JavaScript Animated

  JavaScript Animated <!DOCTYPE html> <html> <style> #myContainer {   width: 400px;   height: 400px;   position: relative;...