<?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>Forem: AZIZ NAJLAOUI</title>
    <description>The latest articles on Forem by AZIZ NAJLAOUI (@aziz999).</description>
    <link>https://forem.com/aziz999</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%2F2315786%2Ffb1a9298-fafb-4dbd-ade1-1fdda7c5f217.png</url>
      <title>Forem: AZIZ NAJLAOUI</title>
      <link>https://forem.com/aziz999</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/aziz999"/>
    <language>en</language>
    <item>
      <title>pointer of a pointer in c</title>
      <dc:creator>AZIZ NAJLAOUI</dc:creator>
      <pubDate>Fri, 31 Jan 2025 16:46:21 +0000</pubDate>
      <link>https://forem.com/aziz999/pointer-of-a-pointer-in-c-2ap</link>
      <guid>https://forem.com/aziz999/pointer-of-a-pointer-in-c-2ap</guid>
      <description>&lt;p&gt;Did you know that variables have addresses in memory? Imagine a variable as a home—it contains a value and an address. A pointer is a variable that stores the address of another variable.&lt;/p&gt;

&lt;p&gt;To initialize a pointer, we need to specify the type of variable it will point to:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int *p;

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

&lt;/div&gt;



&lt;p&gt;Here, p is declared as a pointer to an integer.&lt;/p&gt;

&lt;h2&gt;
  
  
  Assigning an Address to a Pointer
&lt;/h2&gt;

&lt;p&gt;We use the address-of operator (&amp;amp;) to assign the address of a variable to a pointer:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int x = 5;
p = &amp;amp;x;

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

&lt;/div&gt;



&lt;p&gt;If we print p, it will output a random-looking number—this is the memory address of x. However, if we print &lt;em&gt;p, we get the value stored at that address (which is 5). This operation is called **dereferencing.&lt;/em&gt;*&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;printf("I'm p, the address of x: %p\n", p); // Address of x
printf("I'm *p, the value of x: %d\n", *p); // 5

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

&lt;/div&gt;



&lt;p&gt;Note: When printing memory addresses, it's better to use %p instead of %d. &lt;/p&gt;

&lt;h2&gt;
  
  
  Modifying a Variable Through a Pointer
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;*p = 20;
printf("%d", x); // 20

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

&lt;/div&gt;



&lt;p&gt;This means that changing *p also changes x.&lt;/p&gt;

&lt;h2&gt;
  
  
  Pointer to a Pointer (Double Pointer)
&lt;/h2&gt;

&lt;p&gt;Even a pointer has its own memory address, and we can store that address in another pointer! To initialize a pointer that holds the address of another pointer, we write:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int **q;
q = &amp;amp;p;

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

&lt;/div&gt;



&lt;p&gt;The first * indicates that q is a pointer.&lt;br&gt;
The second * means that q stores the address of another pointer.&lt;/p&gt;

&lt;h2&gt;
  
  
  Accessing Values Using a Double Pointer
&lt;/h2&gt;

&lt;p&gt;Since q holds the address of p,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;*q&lt;/code&gt; is the value inside &lt;code&gt;p&lt;/code&gt;, which is the address of &lt;code&gt;x&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;**q&lt;/code&gt; is the value of &lt;code&gt;x&lt;/code&gt;.&lt;br&gt;
More simply:&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;q&lt;/code&gt; → Address of &lt;code&gt;p&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;*q&lt;/code&gt; → Value inside p (which is &lt;code&gt;&amp;amp;x&lt;/code&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;**q&lt;/code&gt; → Value inside x (which is &lt;code&gt;5&lt;/code&gt;).&lt;br&gt;
Final Code&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#include &amp;lt;stdio.h&amp;gt;

int main() {
    int *p;
    int x = 5;

    p = &amp;amp;x;

    int **q;
    q = &amp;amp;p;

    printf("I'm q, the address of p: %p\n", q);
    printf("I'm the address of p: %p\n", &amp;amp;p);
    printf("I'm the value of p (the address of x): %p\n", *q);
    printf("I'm the address of x: %p\n", &amp;amp;x);
    printf("I'm the value of x: %d\n", x);
    printf("I'm the value of the variable with address p (so I'm x): %d\n", **q);

    return 0;
}

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

&lt;/div&gt;



</description>
      <category>beginners</category>
      <category>tutorial</category>
      <category>algorithms</category>
      <category>learning</category>
    </item>
    <item>
      <title>[Boost]</title>
      <dc:creator>AZIZ NAJLAOUI</dc:creator>
      <pubDate>Fri, 31 Jan 2025 16:03:36 +0000</pubDate>
      <link>https://forem.com/aziz999/-2k1b</link>
      <guid>https://forem.com/aziz999/-2k1b</guid>
      <description>&lt;div class="ltag__link"&gt;
  &lt;a href="/thedevricha" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2403640%2Fc563d74e-9911-4557-b8d9-de6869fb8940.png" alt="thedevricha"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="https://dev.to/thedevricha/understanding-this-in-javascript-with-a-real-world-application-1l2i" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;Understanding "this" in JavaScript and its behavior in various scenarios&lt;/h2&gt;
      &lt;h3&gt;Richa ・ Jan 29&lt;/h3&gt;
      &lt;div class="ltag__link__taglist"&gt;
        &lt;span class="ltag__link__tag"&gt;#javascript&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#webdev&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#beginners&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#learning&lt;/span&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;


</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>learning</category>
    </item>
    <item>
      <title>6 Must Know JavaScript Array Methods</title>
      <dc:creator>AZIZ NAJLAOUI</dc:creator>
      <pubDate>Wed, 22 Jan 2025 20:25:59 +0000</pubDate>
      <link>https://forem.com/aziz999/6-must-know-javascript-array-methods-1bac</link>
      <guid>https://forem.com/aziz999/6-must-know-javascript-array-methods-1bac</guid>
      <description>&lt;p&gt;&lt;strong&gt;1. Filter&lt;/strong&gt;&lt;br&gt;
The filter method creates a new array containing only the elements that pass a specified condition. If the condition is true, the element stays in the array; otherwise, it is removed.&lt;/p&gt;

&lt;p&gt;How It Works:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The method checks each element in the array.&lt;/li&gt;
&lt;li&gt;If the callback function returns true, the element is included in the new array.&lt;/li&gt;
&lt;li&gt;If it returns false, the element is excluded.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let boxers = [
    { name: "Tyson Fury", weight: 280 },
    { name: "Mairis Briedis", weight: 199 },
    { name: "Artur Beterbiev", weight: 175 },
    { name: "Jermall Charlo", weight: 160 },
    { name: "Terence Crawford", weight: 146 }
];

// Remove boxers with weight under 170
boxers = boxers.filter(function (boxer) {
    return boxer.weight &amp;gt; 170;
});

// Using arrow function for brevity
boxers = boxers.filter(boxer =&amp;gt; boxer.weight &amp;gt; 170);

console.log(boxers);

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

&lt;/div&gt;


&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[
    { name: "Tyson Fury", weight: 280 },
    { name: "Mairis Briedis", weight: 199 },
    { name: "Artur Beterbiev", weight: 175 }
]


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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2. Map&lt;/strong&gt;&lt;br&gt;
 The map method creates a new array by applying a specified transformation to each element of the original array. Each element in the new array is replaced by the return value of the callback function.&lt;/p&gt;

&lt;p&gt;How It Works:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The method iterates through each element in the array.&lt;/li&gt;
&lt;li&gt;It applies the transformation logic defined in the callback function.&lt;/li&gt;
&lt;li&gt;The result of the callback function replaces the original element in the new array.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let decimalNumbers = [222, 354, 4684, 123, 5];

// Convert decimal numbers to hexadecimal
let hexNumbers = decimalNumbers.map(function (num) {
    return num.toString(16);
});

// Using arrow function for brevity
hexNumbers = decimalNumbers.map(num =&amp;gt; num.toString(16));

console.log(hexNumbers);

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;3. Reduce&lt;/strong&gt;&lt;br&gt;
The reduce method is used to reduce an array to a single value by applying a callback function to each element and accumulating the result.&lt;br&gt;
How It Works:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The method iterates over the array.&lt;/li&gt;
&lt;li&gt;It applies the callback function to an accumulator and the current element.&lt;/li&gt;
&lt;li&gt;The result of the callback becomes the new accumulator value.&lt;/li&gt;
&lt;li&gt;The final accumulator value is returned.
Example:
&lt;/li&gt;
&lt;/ul&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];

// Find the sum of all numbers
let sum = numbers.reduce(function (accumulator, current) {
    return accumulator + current;
}, 0); // 0 is the initial value of the accumulator

// Using an arrow function for brevity
sum = numbers.reduce((accumulator, current) =&amp;gt; accumulator + current, 0);

console.log(sum); // Output: 15
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;4. Some&lt;/strong&gt;&lt;br&gt;
The some method checks if at least one element in the array passes a specified condition. It returns true if any element satisfies the condition and false otherwise.&lt;/p&gt;

&lt;p&gt;How It Works:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The method iterates through the array.&lt;/li&gt;
&lt;li&gt;It stops and returns true as soon as it finds an element that passes the condition.&lt;/li&gt;
&lt;li&gt;If no element passes, it returns false.
let ages = [16, 20, 14, 18];
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Check if there is at least one adult (age ≥ 18)
let hasAdult = ages.some(function (age) {
    return age &amp;gt;= 18;
});

// Using an arrow function for brevity
hasAdult = ages.some(age =&amp;gt; age &amp;gt;= 18);

console.log(hasAdult); // Output: true

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;`5. Every&lt;/strong&gt;&lt;br&gt;
The every method checks if all elements in the array pass a specified condition. It returns true only if all elements satisfy the condition; otherwise, it returns false.&lt;/p&gt;

&lt;p&gt;How It Works:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The method iterates through the array.&lt;/li&gt;
&lt;li&gt;If it finds an element that fails the condition, it stops and returns false.&lt;/li&gt;
&lt;li&gt;If all elements pass, it returns true.
let scores = [80, 85, 90, 95];&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;`&lt;br&gt;
// Check if all scores are above 75&lt;br&gt;
let allAbove75 = scores.every(function (score) {&lt;br&gt;
    return score &amp;gt; 75;&lt;br&gt;
});&lt;/p&gt;

&lt;p&gt;// Using an arrow function for brevity&lt;br&gt;
allAbove75 = scores.every(score =&amp;gt; score &amp;gt; 75);&lt;/p&gt;

&lt;p&gt;console.log(allAbove75); // Output: true&lt;br&gt;
`&lt;code&gt;&lt;/code&gt;&lt;br&gt;
&lt;strong&gt;6. Includes&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The includes method checks if an array contains a specific value. It returns true if the value exists in the array and false otherwise.&lt;br&gt;
How It Works:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The method checks for the value in the array.&lt;/li&gt;
&lt;li&gt;It uses strict equality (===) for comparison.&lt;/li&gt;
&lt;li&gt;It can also check for specific values starting from a given index.
Example:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;`&lt;br&gt;
let numbers = [1, 2, 3, 4, 5];&lt;/p&gt;

&lt;p&gt;// Check for "3" starting from index 2&lt;br&gt;
let includesThree = numbers.includes(3, 2);&lt;/p&gt;

&lt;p&gt;console.log(includesThree); // Output: true&lt;/p&gt;

&lt;p&gt;`&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>tutorial</category>
      <category>webdev</category>
    </item>
    <item>
      <title>string.indexOf() under the hood</title>
      <dc:creator>AZIZ NAJLAOUI</dc:creator>
      <pubDate>Thu, 16 Jan 2025 22:42:50 +0000</pubDate>
      <link>https://forem.com/aziz999/stringindexof-under-the-hood-2699</link>
      <guid>https://forem.com/aziz999/stringindexof-under-the-hood-2699</guid>
      <description>&lt;p&gt;Hello Developers,&lt;/p&gt;

&lt;p&gt;Today, I was trying to implement the &lt;code&gt;indexOf()&lt;/code&gt; method from scratch. So far, this is what I've found:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function myIndexOf(string, target, start = 0) {
    let l = target.length;

    // Adjust the start index if it's negative
    if (start &amp;lt; 0) {
        start = (string.length + start % string.length) % string.length;
    }
    console.log(start);

    // Loop through the string, looking for the target
    for (let i = start; i &amp;lt;= string.length - l; i++) {
        if (string.substr(i, l) === target) {
            return i;
        }
    }

    // Return -1 if the target isn't found
    return -1;
}

console.log(myIndexOf("search me in me", "me", -34)); 

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

&lt;/div&gt;



&lt;p&gt;Code Explanation:&lt;br&gt;
The indexOf() method accepts three parameters:&lt;/p&gt;

&lt;p&gt;1.string: The string in which we are searching.&lt;br&gt;
2.target: The substring we are looking for.&lt;br&gt;
3.start: The index where the search will begin (defaults to 0).&lt;/p&gt;

&lt;p&gt;First Attempt:&lt;br&gt;
My initial idea was simple: loop through the string, and when I find string[i] === target, return i. If no match is found by the end of the loop, return -1. The code looked like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for (let i = 0; i &amp;lt; string.length; i++) {
    if (string[i] === target) {
        return i;
    }
}
return -1;

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

&lt;/div&gt;



&lt;p&gt;However, this approach only works when the target is a single character, since we're comparing character by character.&lt;/p&gt;

&lt;p&gt;Second Attempt:&lt;br&gt;
I then realized that if the target is longer than one character, I need to compare substrings. I used the &lt;code&gt;substr()&lt;/code&gt; method to compare a substring of the same length as the target. The loop was adjusted to stop when there are enough characters left in the string to compare:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for (let i = 0; i &amp;lt; string.length - target.length + 1; i++) {
    if (string.substr(i, target.length) === target) {
        return i;
    }
}
return -1;

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

&lt;/div&gt;



&lt;p&gt;Third Attempt:&lt;br&gt;
Next, I needed to handle the &lt;code&gt;start&lt;/code&gt; parameter, which can be negative. The built-in &lt;code&gt;indexOf()&lt;/code&gt; method starts searching from &lt;code&gt;string.length + start&lt;/code&gt; when start is negative. For example, if the string length is 10 and the start is -4, the search will start at index 6 (i.e., 10 - 4).&lt;/p&gt;

&lt;p&gt;To account for this, I updated the code to handle negative start values:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function myIndexOf(string, target, start = 0) {
    let l = target.length;

    if (start &amp;lt; 0) {
        start = Math.max(0, string.length + start);  // Adjust negative start
    }
    console.log(start);

    for (let i = start; i &amp;lt;= string.length - l; i++) {
        if (string.substr(i, l) === target) {
            return i;
        }
    }

    return -1;
}

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

&lt;/div&gt;



&lt;p&gt;Final Version:&lt;br&gt;
Curious about handling start values greater than the string length, I decided to modify the function to keep "rolling around" the string if start exceeds the string length. This way, the function will continue searching from the appropriate index after wrapping around. The final solution uses this formula to adjust the start index:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;start = (string.length + start % string.length) % string.length;

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

&lt;/div&gt;



&lt;p&gt;How It Works:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The modulo operation start % string.length ensures that start is within a range of -string.length to string.length.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Adding string.length ensures that any negative result becomes positive.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The final modulo operation ensures that the value of start is wrapped around and falls within valid index boundaries.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;im thinking next to use binary search instead of the linear what do you think ! &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>programming</category>
      <category>frontend</category>
    </item>
    <item>
      <title>Fixing a "Show More / Show Less" Button with JavaScript</title>
      <dc:creator>AZIZ NAJLAOUI</dc:creator>
      <pubDate>Tue, 31 Dec 2024 18:17:35 +0000</pubDate>
      <link>https://forem.com/aziz999/fixing-a-show-more-show-less-button-with-javascript-he0</link>
      <guid>https://forem.com/aziz999/fixing-a-show-more-show-less-button-with-javascript-he0</guid>
      <description>&lt;p&gt;Hello developers! 👨‍💻👩‍💻&lt;br&gt;
I'm a CS student and im learning javascript because i want to be a frontend developer  , and today I wanted to share a concept I recently learned while making a simple "Show More / Show Less" button using JavaScript.&lt;br&gt;
can you tell what the diffrence between the first code and the second &lt;br&gt;
and witch one is true !!&lt;/p&gt;

&lt;p&gt;I was trying to create a button that toggles the visibility of text, like this:&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxj20zkwikqebhe8zpwsa.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxj20zkwikqebhe8zpwsa.png" alt="show more" width="318" height="117"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsic7f6bzuzawseniwtyr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsic7f6bzuzawseniwtyr.png" alt="show less" width="377" height="261"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;simple html 2 paragrah one of them with the class of text hidden-text&lt;br&gt;
it the one we will toggle and a button that will do the job&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;p&amp;gt;lorem ipsum dolor sit amet consectetur adipisicing elit. &amp;lt;/p&amp;gt;
&amp;lt;p class="text hidden-text"&amp;gt;fsdfsfsd .... &amp;lt;/p&amp;gt; 
&amp;lt;button class="show-more"&amp;gt;show more....&amp;lt;/button&amp;gt;

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

&lt;/div&gt;



&lt;p&gt;css:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.hidden-text {
  display: none;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;ive declared all the variable i need&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let showButton = document.querySelector(".show-more");
let text = document.querySelector(".text");
let value = false; // Initially, the text is hidden

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

&lt;/div&gt;



&lt;p&gt;my first code was :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;showButton.addEventListener("click", (value) =&amp;gt; {
    value = !value;
    text.classList.toggle("hidden-text");
    showButton.textContent = value ? "show less..." : "show more..."; // Update button text
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;but this didn't work the value was always false even that i swithced using value=!value&lt;br&gt;
after some search i found out why this happend &lt;/p&gt;

&lt;p&gt;Every time the button was clicked, the local value parameter inside the callback function was being redeclared, which shadows the outer value.&lt;/p&gt;

&lt;p&gt;that means that The outer value remains unchanged, always staying false.&lt;br&gt;
and The local value is toggled inside the callback, but it only exists during that function's execution and doesn’t affect the outer value.&lt;/p&gt;

&lt;p&gt;To solve this, I needed to remove the value parameter from the callback and directly use the outer value that persists across button clicks.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let value = false; // Persistent state variable

showButton.addEventListener("click", () =&amp;gt; { 
    value = !value; // Toggle the outer value
    text.classList.toggle("hidden-text");
    showButton.textContent = value ? "show less..." : "show more..."; // Update button text based on value
});

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

&lt;/div&gt;



</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>frontend</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
