<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Michael Flores</title>
    <description>The latest articles on DEV Community by Michael Flores (@michaelflore).</description>
    <link>https://dev.to/michaelflore</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F431333%2F6fd778ab-e857-469b-bf1e-48927bf0b149.jpg</url>
      <title>DEV Community: Michael Flores</title>
      <link>https://dev.to/michaelflore</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/michaelflore"/>
    <language>en</language>
    <item>
      <title>JavaScript Array Methods, String Methods, &amp; Math.random()</title>
      <dc:creator>Michael Flores</dc:creator>
      <pubDate>Sat, 31 Aug 2024 21:52:49 +0000</pubDate>
      <link>https://dev.to/michaelflore/javascript-array-methods-string-methods-mathrandom-5hmm</link>
      <guid>https://dev.to/michaelflore/javascript-array-methods-string-methods-mathrandom-5hmm</guid>
      <description>&lt;h2&gt;
  
  
  Array Methods
&lt;/h2&gt;

&lt;p&gt;First lets start with the built in JavaScript array methods.&lt;/p&gt;

&lt;p&gt;Here are some common methods that DO NOT mutate (change) the original array:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;.indexOf()&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let array = [10, 20, 30, 40, 50];

// Find the index of element 30
let index = array.indexOf(30);
console.log(index); // Output: 2

// Search for an element that is not in the array
index = array.indexOf(60);
console.log(index); // Output: -1

// Search starting from index 3
index = array.indexOf(30, 3);
console.log(index); // Output: -1 (30 is before index 3)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;.slice()&lt;/strong&gt;, shallow copy&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let array = [1, 2, 3, 4, 5];

// Create a new array from index 1 to 3 (4 is not included)
let newArray = array.slice(1, 4);
console.log(newArray); // Output: [2, 3, 4]

// Create a new array from index 2 to the end
let anotherArray = array.slice(2);
console.log(anotherArray); // Output: [3, 4, 5]

// Copy the entire array
let copyArray = array.slice();
console.log(copyArray); // Output: [1, 2, 3, 4, 5]

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;.join()&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let array = ['Apple', 'Banana', 'Cherry'];

// Join elements with a comma (default separator)
let joined = array.join();
console.log(joined); // Output: 'Apple,Banana,Cherry'

// Join elements with a custom separator (e.g., a space)
let joinedWithSpace = array.join(' ');
console.log(joinedWithSpace); // Output: 'Apple Banana Cherry'

// Join elements with a custom separator (e.g., a dash)
let joinedWithDash = array.join('-');
console.log(joinedWithDash); // Output: 'Apple-Banana-Cherry'

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;.includes()&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let array = [1, 2, 3, 4, 5];

// Check if the array includes the number 3
let hasThree = array.includes(3);
console.log(hasThree); // Output: true

// Check if the array includes the number 6
let hasSix = array.includes(6);
console.log(hasSix); // Output: false

// Check if the array includes the number 3, starting from index 2
let hasThreeFromIndexTwo = array.includes(3, 2);
console.log(hasThreeFromIndexTwo); // Output: true (3 is present starting from index 2)

// Check if the array includes the number 3, starting from index 4
let hasThreeFromIndexFour = array.includes(3, 4);
console.log(hasThreeFromIndexFour); // Output: false (3 is not present starting from index 4)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Some common methods with React.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;.map()&lt;/strong&gt;,  For arrays of primitive values (e.g., numbers, strings, booleans), the values themselves are copied into the new array (deep copy)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let numbers = [1, 2, 3, 4, 5];

// Create a new array with each element doubled
let doubled = numbers.map(x =&amp;gt; x * 2);
console.log(doubled); // Output: [2, 4, 6, 8, 10]

// Create a new array with the squares of each element
let squared = numbers.map(x =&amp;gt; x ** 2);
console.log(squared); // Output: [1, 4, 9, 16, 25]

// Create a new array with element indexes as strings
let indexed = numbers.map((x, i) =&amp;gt; `${i}: ${x}`);
console.log(indexed); // Output: ['0: 1', '1: 2', '2: 3', '3: 4', '4: 5']

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;.filter()&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let numbers = [1, 2, 3, 4, 5];

// Filter out elements less than 4
let filtered = numbers.filter(x =&amp;gt; x &amp;gt;= 4);
console.log(filtered); // Output: [4, 5]

// Filter out odd numbers
let evenNumbers = numbers.filter(x =&amp;gt; x % 2 === 0);
console.log(evenNumbers); // Output: [2, 4]

// Filter an array of objects
let people = [
  { name: 'Alice', age: 25 },
  { name: 'Bob', age: 30 },
  { name: 'Charlie', age: 35 }
];

// Filter people who are 30 or older
let adults = people.filter(person =&amp;gt; person.age &amp;gt;= 30);
console.log(adults); // Output: [{ name: 'Bob', age: 30 }, { name: 'Charlie', age: 35 }]

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;.reduce()&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let numbers = [1, 2, 3, 4, 5];

// Sum all elements in the array
let sum = numbers.reduce((accumulator, currentValue) =&amp;gt; accumulator + currentValue, 0);
console.log(sum); // Output: 15

// Find the maximum value in the array
let max = numbers.reduce((accumulator, currentValue) =&amp;gt; Math.max(accumulator, currentValue));
console.log(max); // Output: 5

// Create an object counting occurrences of each number
let countOccurrences = numbers.reduce((accumulator, currentValue) =&amp;gt; {
  accumulator[currentValue] = (accumulator[currentValue] || 0) + 1;
  return accumulator;
}, {});
console.log(countOccurrences); // Output: { '1': 1, '2': 1, '3': 1, '4': 1, '5': 1 }

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;Now some common array methods that DO mutate (change) the original array:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;.splice()&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let array = [1, 2, 3, 4, 5];

// Remove 2 elements starting from index 1
array.splice(1, 2); 
console.log(array); // Output: [1, 4, 5];

// Add elements starting from index 1
array.splice(1, 0, 'a', 'b'); 
console.log(array); // Output: [1, 'a', 'b', 4, 5];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;.unshift()&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let array = [2, 3, 4];

// Add elements to the beginning
array.unshift(0, 1);
console.log(array); // Output: [0, 1, 2, 3, 4]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;.fill()&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let array = [1, 2, 3, 4, 5];

// Fill the entire array with the value 0
array.fill(0);
console.log(array); // Output: [0, 0, 0, 0, 0]

// Fill a part of the array with the value 7
array.fill(7, 1, 4);
console.log(array); // Output: [0, 7, 7, 7, 0]

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;.sort()&lt;/strong&gt;, default is ascending&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let numbers = [4, 2, 5, 1, 3];

// Sort numbers in ascending order
numbers.sort((a, b) =&amp;gt; a - b);
console.log(numbers); // Output: [1, 2, 3, 4, 5]

// Sort numbers in descending order
numbers.sort((a, b) =&amp;gt; b - a);
console.log(numbers); // Output: [5, 4, 3, 2, 1]

// Sort strings
let strings = ['banana', 'apple', 'cherry'];
strings.sort();
console.log(strings); // Output: ['apple', 'banana', 'cherry']

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  String Methods
&lt;/h2&gt;

&lt;p&gt;Now lets talk about some of the built in JavaScript string methods.&lt;/p&gt;

&lt;p&gt;String are immutable, meaning they cannot be changed once they are created.&lt;/p&gt;

&lt;p&gt;Here are a few common JS string methods you might use.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;.replace()&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let original = 'hello';
// Replace characters
let replaced = original.replace('e', 'a');
console.log(replaced); // Output: 'hallo'
console.log(original); // Output: 'hello' (original string remains unchanged)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;.substring()&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let original = 'hello';
// Get a substring
let substring = original.substring(1, 4);
console.log(substring); // Output: 'ell'
console.log(original); // Output: 'hello' (original string remains unchanged)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;.split()&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let text = 'apple,banana,cherry';

// Split the string by commas
let fruits = text.split(',');
console.log(fruits); // Output: ['apple', 'banana', 'cherry']

// Split the string by commas with a limit of 2
let limitedFruits = text.split(',', 2);
console.log(limitedFruits); // Output: ['apple', 'banana']

// Split the string with no separator
let singleElementArray = text.split();
console.log(singleElementArray); // Output: ['apple,banana,cherry']

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;.charAt()&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let text = 'Hello, World!';

// Get the character at index 7
let char = text.charAt(7);
console.log(char); // Output: 'W'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;.charCodeAt()&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let text = 'Hello, World!';

// Get the Unicode code of the character at index 7
let code = text.charCodeAt(7);
console.log(code); // Output: 87 (Unicode for 'W')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;.search()&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let text = 'Hello, World!';

// Search for the first occurrence of 'World'
let index = text.search('World');
console.log(index); // Output: 7

// Search for a pattern using a regular expression
let regexIndex = text.search(/world/i); // Case-insensitive search
console.log(regexIndex); // Output: 7
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Math.random()
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;1. Random Number Between 0 and 4 (5 Items)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you want to generate a random integer between 0 and 4 (inclusive), you can use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let randomIndex = Math.floor(Math.random() * 5);
console.log(randomIndex); // Output: 0, 1, 2, 3, or 4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2. Random Number Between 1 and 5 (1 through 5)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To generate a random integer between 1 and 5 (inclusive), you need to adjust the formula slightly:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let randomNumber = Math.floor(Math.random() * 5) + 1;
console.log(randomNumber); // Output: 1, 2, 3, 4, or 5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are definitely a lot more, but these are some common ones that I have ran into at some point.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>JavaScript Basics</title>
      <dc:creator>Michael Flores</dc:creator>
      <pubDate>Sun, 27 Dec 2020 04:20:47 +0000</pubDate>
      <link>https://dev.to/michaelflore/my-first-post-2m0d</link>
      <guid>https://dev.to/michaelflore/my-first-post-2m0d</guid>
      <description>&lt;p&gt;We use JS to program the behavior of web pages. It can change..&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;HTML Content&lt;/li&gt;
&lt;li&gt;HTML Attributes&lt;/li&gt;
&lt;li&gt;HTML Styles&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We can also use it to hide and show elements on a page. The code must go either in a .js file or in script tags.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;body&amp;gt;
&amp;lt;p id="message"&amp;gt;Default Message&amp;lt;/p&amp;gt;
&amp;lt;button onclick="change()"&amp;gt;Change Message&amp;lt;/button&amp;gt;
//or &amp;lt;script src="external.js"&amp;gt;
&amp;lt;script&amp;gt;

function changeMessage() {
  document.getElementById("message").innerHTML = "Hello World";
}

&amp;lt;script&amp;gt;
&amp;lt;/body&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Normally we place the script tag in the bottom of the body to improve display speed. It is better to use an external JS file to separate code for easier read. You can also display data with&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;window.alert(5 + 6)
document.write() //dont use this, it will delete all HTML
console.log() //debugging
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A JS statement has values, operators, keywords, expressions, or comments. Keywords include for, if, else, function, let, switch, etc. An expression is what eventually computes to a value.&lt;/p&gt;

&lt;h2&gt;
  
  
  Variables
&lt;/h2&gt;

&lt;p&gt;Variable identifiers can start with letters, _, or $, but no numbers. Adding a number and a string the result will always be a string.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let _price = 45;
let myName; //value is undefined
const theStr = "myString";

let fullName = "john" + "doe"; //johndoe

const combine = "5" + 2 + 3; //523 as a string

const 2 + 4 + "8" + 2 + 3; //6823 as a string
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Operators
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let sum = 0;
let value = 5;
sum += value; // sum = sum + value
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;=== means equal value AND type&lt;br&gt;
!== means not equal value AND type&lt;br&gt;
? : ternary&lt;/p&gt;

&lt;p&gt;5 / 0 = infinity&lt;br&gt;
8 % 70 = 8&lt;br&gt;
0 % 5 = 0&lt;br&gt;
1 % 5 = 1&lt;br&gt;
7 % 0 = NaN&lt;br&gt;
let var; //type undefined&lt;/p&gt;

&lt;p&gt;Math.pow(5, 2) 5**2 (experimental)&lt;/p&gt;

&lt;p&gt;Use typeof operator to find type of a variable or expression.&lt;/p&gt;

&lt;p&gt;JavaScript is a &lt;strong&gt;dynamic typed&lt;/strong&gt; language. So the same variable can hold different data types.&lt;/p&gt;

&lt;p&gt;Primitive types: String, Number, Boolean, Undefined, Null, Symbol, BigInt&lt;br&gt;
Object Types: Arrays, Object Literals, Function, Date, RegExp, Map, Set&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Key Points&lt;/strong&gt;&lt;br&gt;
Undefined and Null same value but different types.&lt;br&gt;
Null is empty object means nothing.&lt;/p&gt;
&lt;h2&gt;
  
  
  Functions
&lt;/h2&gt;

&lt;p&gt;function definition consists of declaration, name, parameters, body, return&lt;/p&gt;

&lt;p&gt;What invokes a function?&lt;br&gt;
Events, () operator, *&lt;em&gt;without () it will return the definition&lt;br&gt;
*&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Variables in the body and params are local to that function.&lt;/p&gt;
&lt;h2&gt;
  
  
  Objects
&lt;/h2&gt;

&lt;p&gt;Contain properties, can access properties using&lt;br&gt;
const obj = { name: "john" }&lt;br&gt;
obj.name obj["name"]&lt;/p&gt;

&lt;p&gt;or methods, which are functions stored in a property as definitions.&lt;/p&gt;

&lt;p&gt;The this keyword usually is the Window object even in a function declaration in non strict mode otherwise undefined.&lt;/p&gt;

&lt;p&gt;When accessing an object method without invoking () then it would return the function definition.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Cannot compare with == or === will always be false because even though they may be same structure they have different references, it does not check values.&lt;/strong&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Events
&lt;/h2&gt;

&lt;p&gt;onClick, onChange, onKeyDown, onMouseEnter&lt;/p&gt;
&lt;h2&gt;
  
  
  Strings
&lt;/h2&gt;

&lt;p&gt;Strings are immutable and have a .length property which would include spaces in a string. Use \ to escape characters&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const str = "Coding in the \"right way\", but is it really?"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>firstpost</category>
    </item>
  </channel>
</rss>
