Firestore returns an empty array in javascript

I want to get some data from firestore and then display it in JSX style. This is why I have used useEffect() in the following code as I want all the promises to be resolved and the data to be in the variable before I return my react component. This component works when I manually define the data as an object in javascript so the error lies in firestore returning an empty array, not the react code.

This is my programme :

import React, { useEffect, useState } from "react";
import { useParams } from "react-router-dom";

import { addDoc, collection, onSnapshot } from "firebase/firestore";
import { db } from "../data/firestore-data";

import { UserAuth } from "../context/AuthContext";

export default function ProductPage() {
  const [products, setProducts] = useState([]);

  useEffect(() => {
    const productsRef = collection(db, "products");

    getDocs(productsRef).then((res) => {
      const ords = res.docs.map((doc) => ({
        data: doc.data(),
        id: doc.id,
      }));

      const products = ords.map((ord) => ({
        id: ord.id,
        name: ord.data.name,
        price: ord.data.price,
        imageSrc: ord.data.imageSrc,
        imageAlt: ord.data.imageAlt,
        productDescription: ord.data.productDescription,
      }));

      setProducts(products);
    });
  }, []);

  const productID = useParams().id;
  console.log(productID);

  const [count, setCount] = useState(1);

  const incrementCount = () => {
    setCount(count + 1);
  };

  const decrementCount = () => {
    if (count >= 2) {
      setCount(count - 1);
    }
  };

  console.log("Template : ");
  console.log(products);
  const product = products.find((obj) => obj.id === productID);
  const productName = product.name;
  const productPrice = product.price;
  const img = product.imageSrc;
  const productDescription = product.productDescription;

  const imgSize = 600;

  const { user } = UserAuth();

  function AddOrder() {
    console.log("Working...");
    const ordersRef = collection(db, "orders");
    addDoc(ordersRef, {
      ProductID: productID,
      UserID: user.uid,
    }).then((res) => {
      console.log(res);
    });
  }

  return (
    <React.Fragment>
      <div className="m-[70px] ml-[130px] flex flex-row items-start font-mono">
        <img
          className="mt-0 rounded-lg"
          style={{ width: imgSize, height: imgSize }}
          src={img}
          alt={productName}
        />
        <div className="mx-[90px] flex-grow space-y-9">
          <h1 className="text-5xl">{productName}</h1>
          <p className="text-lg font-bold">Available At: ${productPrice}</p>
          <p className="text-lg">{productDescription}</p>
          <div className="flex space-x-3">
            <div className="inline-flex h-[50px] flex-grow basis-1/4 items-center justify-center space-x-4 py-2 px-4">
              <button
                className="h-[25px] w-[25px] border-2 border-solid border-orange-600 text-center hover:bg-orange-600"
                onClick={decrementCount}
              >
                -
              </button>
              <span className="text-center text-xl text-black">{count}</span>
              <button
                className="h-[25px] w-[25px] border-2 border-solid border-orange-600 text-center hover:bg-orange-600"
                onClick={incrementCount}
              >
                +
              </button>
            </div>
            <button className="h-[50px] flex-grow basis-3/4 items-center rounded bg-orange-600 py-2 px-4 hover:bg-orange-500">
              <button
                className="text-center text-xl font-bold text-white"
                onClick={AddOrder}
              >
                Buy Now
              </button>
            </button>
          </div>
        </div>
      </div>
    </React.Fragment>
  );
}

When that didn’t work I tried with the onSnapshot() function as well but it returned the same empty array. My code for that was :

 useEffect(() => {
    const productsRef = collection(db, "products");
    onSnapshot(productsRef, (snapshot) => {
      setProducts(
        snapshot.docs.map((doc) => ({
          id: doc.id,
          name: doc.data.name,
          price: doc.data.price,
          imageSrc: doc.data.imageSrc,
          imageAlt: doc.data.imageAlt,
          productDescription: doc.data.productDescription,
        }))
      );
    });
  }, []);

This is an image of the console (the errors are because I’m trying to access values of an object that’s an empty array but they’ll get solved when the empty array part does). The error message and empty array is the same for the both getDocs() and onSnapshot():
enter image description here

If you need any context or the code of some of the files I have imported please let me know by commenting on this question.

Edit: Added image of the console

Why Adblock hides video tag?

Initial html.

<div id="liveData">
    <video src="video_url" onplay="myVid()"></video>
</div>


function myVid() {
      $.ajax({
         url : 'server_url',
         type:'GET',   
         dataType: 'json',
         cache: false,
            success: function(response) {
               console.log(response)
               $('div#liveData').html(response);

           }

    });
}

After I click play I receive new video tag with new url.

<div id="liveData">
   <video id="vid" src="new_video_url"></video>
</div>

But Adblock adds style="display:none" in this new video tag.

I tried to add visibility check in ajax,

       $('div#liveData').html(response);
       if ($('#vid').is(":visible")) {
          console.log('Good');
       }
   }

I always see Good because adblock put display:none after ajax code executed.

I need to show user message in new video tag, if Adblock enabled:

<div id="liveData">
  <video id="vid" src="new_video_url" style="display:none"></video>
  <div>Add website to Adblock whitelist, pls</div>
</div>

ESLint custom rule – no spaces for property access

I’ve configured my ESLint for node project with

'no-multi-spaces'  : ['error'],

rule. It automatically fixes object .property to object .property

object.
property

to

object 
  . property

, but I’m not satisfied with it. I want object.property as a fix result.

Is there any ideas which rule should be applied?

Jquery link click smooth scroll not working

Ive created a simple JS Table of Contents for my website. But it not scrolling when Click on link. I dont know why. Another fact is, why second shortcode contains parentesi bracket in frontend / title ? It should show / appear just Title / text inside it. Not () bracket. I tried in many ways, but not solving anyway.

$(document).ready(function() {
  var toc = "";
  var hasHeadings = false;
  var postBody = $(".post-body").html();
  var tocTitle = "";
  
  // Get the TOC title from the shortcode
  var tocShortcode = postBody.match(/(s*tocs*)s*(.*)/i);
  if (tocShortcode !== null) {
    tocTitle = tocShortcode[1];
  }

  if (postBody.indexOf('(toc)') > -1) {
    $(".post-body").find("h2,h3").each(function() {
      var title = $(this).text();
      var slug = title.toLowerCase().replace(/[^a-z0-9]+/g, '-');
      $(this).attr('id', slug);
      toc += "<li><a href='#" + slug + "'>" + title + "</a></li>";
      hasHeadings = true;
    });
  }
  
  if (hasHeadings) {

    // Add the table of contents list
    var tocListElement = "<div id='table-of-contents'><h2>" + tocTitle + "</h2><ul>" + toc + "</ul></div>";
    var tocListIndex = postBody.indexOf("(toc)");
    postBody = postBody.substring(0, tocListIndex) + "(toc)(" + postBody.substring(tocListIndex + 5, postBody.length) + "";
    postBody = postBody.replace(/(toc)((.*?))/, tocListElement);

    // Update postBody with the modified HTML
    $(".post-body").html(postBody);
  }
  
  // smooth scroll to section when clicked
  $("#table-of-contents a").on('click', function(event) {
    if (this.hash !== "") {
      event.preventDefault();
      var hash = this.hash;
      $('html, body').animate({
        scrollTop: $(hash).offset().top
      }, 800, function(){
        window.location.hash = hash;
      });
    }
  });
});
#table-of-contents {
  margin: 20px 0;
  padding: 10px;
  background-color: #f5f5f5;
  border: 1px solid #ddd;
  border-radius: 5px;
}
#table-of-contents ul {
  list-style: none;
  margin: 0;
  padding: 0;
}
#table-of-contents li {
  margin-bottom: 5px;
}
#table-of-contents a {
  text-decoration: none;
  color: #333;
}
#table-of-contents a:hover {
  color: #666;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.4/jquery.min.js"></script>

<div class="post-body">
  <h2>This is a header</h2>  (toc)(This is a Title)
  <p>Loeen Ishan Dowel Okay Google and Again corporate houses the div div like the way to complete</p>
  <h3>A Sub Section</h3>
  <p>This is a simple text</p>
  <h2>This is another Header</h2>
  <p>Iama Local storage bookmark or like system and Again corporate houses</p>
</div>

How can we change the color of the columns every 2 weeks? c# sql

**> Hi, I’m pulling this information from SQL. I just want the other variable to change color every 2 weeks in the table Ocak, Ocak1

Ocak= 2 weeks- first 2 weeks
Ocak1= 2 weeks – 3rd and 4th week //(Ocak = January) // (Mart=March)**

how can i solve this problem

 @if (Data.Mart == "")
   {
       MartArkaplan = "#6c757d45";
   }

Now that we are in March, when we enter the 3rd week of March, I want the column with no value in it to be gray. I want it to travel column by column every 2 weeks
3rd and 4th week of march is equal to Mart1 variable

‘ Code; ‘

 @{
            if (Model != null)
            {
                foreach (var Data in Model)
                {
                    int deneme = Convert.ToInt16(Data.Id);
                    deneme = deneme - 1;
                    <tr class="text-center">
                        <td>@deneme</td>
                        <td class="text-center">@Data.ProjectName</td>
                        <td>@Data.Factory</td>
                        <td>@Data.PersonalName</td>
                        @*----------OCAK------------*@
                        @if (Data.Ocak == "")
                        {
                            OcakArkaplan = "white";
                        }
                        else if (Data.Ocak == "ÖNÇ")
                        {
                            OcakArkaplan = "white";
                        }
                        else if (Data.Ocak == "TSR")
                        {
                            OcakArkaplan = "#F8CBAD";
                        }
                        else if (Data.Ocak == "İML")
                        {
                            OcakArkaplan = "#2F75B5";
                        }
                        else if (Data.Ocak == "OTO")
                        {
                            OcakArkaplan = "#A9D08E";
                        }
                        else if (Data.Ocak == "YZL")
                        {
                            OcakArkaplan = "#D9E1F2";
                        }
                        @*----------OCAK-1------------*@
                        @if (Data.Ocak1 == "")
                        {
                            OcakArkaplan1 = "white";
                        }
                        else if (Data.Ocak1 == "ÖNÇ")
                        {
                            OcakArkaplan1 = "white";
                        }
                        else if (Data.Ocak1 == "TSR")
                        {
                            OcakArkaplan1 = "#F8CBAD";
                        }
                        else if (Data.Ocak1 == "İML")
                        {
                            OcakArkaplan1 = "#2F75B5";
                        }
                        else if (Data.Ocak1 == "OTO")
                        {
                            OcakArkaplan1 = "#A9D08E";
                        }
                        else if (Data.Ocak1 == "YZL")
                        {
                            OcakArkaplan1 = "#D9E1F2";
                        }
                        @*----------SUBAT------------*@
                        @if (Data.Subat == "")
                        {
                            SubatArkaplan = "white";
                        }
                        else if (Data.Subat == "ÖNÇ")
                        {
                            SubatArkaplan = "white";
                        }
                        else if (Data.Subat == "TSR")
                        {
                            SubatArkaplan = "#F8CBAD";
                        }
                        else if (Data.Subat == "İML")
                        {
                            SubatArkaplan = "#2F75B5";
                        }
                        else if (Data.Subat == "OTO")
                        {
                            SubatArkaplan = "#A9D08E";
                        }
                        else if (Data.Subat == "YZL")
                        {
                            SubatArkaplan = "#D9E1F2";
                        }
                        @*----------SUBAT-1------------*@
                        @if (Data.Subat1 == "")
                        {
                            SubatArkaplan1 = "white";
                        }
                        else if (Data.Subat1 == "ÖNÇ")
                        {
                            SubatArkaplan1 = "white";
                        }
                        else if (Data.Subat1 == "TSR")
                        {
                            SubatArkaplan1 = "#F8CBAD";
                        }
                        else if (Data.Subat1 == "İML")
                        {
                            SubatArkaplan1 = "#2F75B5";
                        }
                        else if (Data.Subat1 == "OTO")
                        {
                            SubatArkaplan1 = "#A9D08E";
                        }
                        else if (Data.Subat1 == "YZL")
                        {
                            SubatArkaplan1 = "#D9E1F2";
                        }
                        @*----------MART------------*@
                        @if (Data.Mart == "")
                        {
                            MartArkaplan = "#6c757d45";
                        }
                        else if (Data.Mart == "ÖNÇ")
                        {
                            MartArkaplan = "white";
                        }
                        else if (Data.Mart == "TSR")
                        {
                            MartArkaplan = "#F8CBAD";
                        }
                        else if (Data.Mart == "İML")
                        {
                            MartArkaplan = "#2F75B5";
                        }
                        else if (Data.Mart == "OTO")
                        {
                            MartArkaplan = "#A9D08E";
                        }
                        else if (Data.Mart == "YZL")
                        {
                            MartArkaplan = "#D9E1F2";
                        }
                        @*----------MART-1------------*@
                        @if (Data.Mart1 == "")
                        {
                            MartArkaplan1 = "white";
                        }
                        else if (Data.Mart1 == "ÖNÇ")
                        {
                            MartArkaplan1 = "white";
                        }
                        else if (Data.Mart1 == "TSR")
                        {
                            MartArkaplan1 = "#F8CBAD";
                        }
                        else if (Data.Mart1 == "İML")
                        {
                            MartArkaplan1 = "#2F75B5";
                        }
                        else if (Data.Mart1 == "OTO")
                        {
                            MartArkaplan1 = "#A9D08E";
                        }
                        else if (Data.Mart1 == "YZL")
                        {
                            MartArkaplan1 = "#D9E1F2";
                        }
                        @*----------NİSAN------------*@
                        @if (Data.Nisan == "")
                        {
                            NisanArkaplan = "white";
                        }
                        else if (Data.Nisan == "ÖNÇ")
                        {
                            NisanArkaplan = "white";
                        }
                        else if (Data.Nisan == "TSR")
                        {
                            NisanArkaplan = "#F8CBAD";
                        }
                        else if (Data.Nisan == "İML")
                        {
                            NisanArkaplan = "#2F75B5";
                        }
                        else if (Data.Nisan == "OTO")
                        {
                            NisanArkaplan = "#A9D08E";
                        }
                        else if (Data.Nisan == "YZL")
                        {
                            NisanArkaplan = "#D9E1F2";
                        }
                        @*----------NİSAN-1------------*@
                        @if (Data.Nisan1 == "")
                        {
                            NisanArkaplan1 = "white";
                        }
                        else if (Data.Nisan1 == "ÖNÇ")
                        {
                            NisanArkaplan1 = "white";
                        }
                        else if (Data.Nisan1 == "TSR")
                        {
                            NisanArkaplan1 = "#F8CBAD";
                        }
                        else if (Data.Nisan1 == "İML")
                        {
                            NisanArkaplan1 = "#2F75B5";
                        }
                        else if (Data.Nisan1 == "OTO")
                        {
                            NisanArkaplan1 = "#A9D08E";
                        }
                        else if (Data.Nisan1 == "YZL")
                        {
                            NisanArkaplan1 = "#D9E1F2";
                        }
                        @*----------MAYIS------------*@
                        @if (Data.Mayis == "")
                        {
                            MayisArkaplan = "white";
                        }
                        else if (Data.Mayis == "ÖNÇ")
                        {
                            MayisArkaplan = "white";
                        }
                        else if (Data.Mayis == "TSR")
                        {
                            MayisArkaplan = "#F8CBAD";
                        }
                        else if (Data.Mayis == "İML")
                        {
                            MayisArkaplan = "#2F75B5";
                        }
                        else if (Data.Mayis == "OTO")
                        {
                            MayisArkaplan = "#A9D08E";
                        }
                        else if (Data.Mayis == "YZL")
                        {
                            MayisArkaplan = "#D9E1F2";
                        }
                        @*----------MAYIS-1------------*@
                        @if (Data.Mayis1 == "")
                        {
                            MayisArkaplan1 = "white";
                        }
                        else if (Data.Mayis1 == "ÖNÇ")
                        {
                            MayisArkaplan1 = "white";
                        }
                        else if (Data.Mayis1 == "TSR")
                        {
                            MayisArkaplan1 = "#F8CBAD";
                        }
                        else if (Data.Mayis1 == "İML")
                        {
                            MayisArkaplan1 = "#2F75B5";
                        }
                        else if (Data.Mayis1 == "OTO")
                        {
                            MayisArkaplan1 = "#A9D08E";
                        }
                        else if (Data.Mayis1 == "YZL")
                        {
                            MayisArkaplan1 = "#D9E1F2";
                        }
                        @*----------HAZİRAN------------*@
                        @if (Data.Haziran == "")
                        {
                            HaziranArkaplan = "white";
                        }
                        else if (Data.Haziran == "ÖNÇ")
                        {
                            HaziranArkaplan = "white";
                        }
                        else if (Data.Haziran == "TSR")
                        {
                            HaziranArkaplan = "#F8CBAD";
                        }
                        else if (Data.Haziran == "İML")
                        {
                            HaziranArkaplan = "#2F75B5";
                        }
                        else if (Data.Haziran == "OTO")
                        {
                            HaziranArkaplan = "#A9D08E";
                        }
                        else if (Data.Haziran == "YZL")
                        {
                            HaziranArkaplan = "#D9E1F2";
                        }
                        @*----------HAZİRAN-1------------*@
                        @if (Data.Haziran1 == "")
                        {
                            HaziranArkaplan1 = "white";
                        }
                        else if (Data.Haziran1 == "ÖNÇ")
                        {
                            HaziranArkaplan1 = "white";
                        }
                        else if (Data.Haziran1 == "TSR")
                        {
                            HaziranArkaplan1 = "#F8CBAD";
                        }
                        else if (Data.Haziran1 == "İML")
                        {
                            HaziranArkaplan1 = "#2F75B5";
                        }
                        else if (Data.Haziran1 == "OTO")
                        {
                            HaziranArkaplan1 = "#A9D08E";
                        }
                        else if (Data.Haziran1 == "YZL")
                        {
                            HaziranArkaplan1 = "#D9E1F2";
                        }
                        @*----------TEMMUZ------------*@
                        @if (Data.Temmuz == "")
                        {
                            TemmuzArkaplan = "white";
                        }
                        else if (Data.Temmuz == "ÖNÇ")
                        {
                            TemmuzArkaplan = "white";
                        }
                        else if (Data.Temmuz == "TSR")
                        {
                            TemmuzArkaplan = "#F8CBAD";
                        }
                        else if (Data.Temmuz == "İML")
                        {
                            TemmuzArkaplan = "#2F75B5";
                        }
                        else if (Data.Temmuz == "OTO")
                        {
                            TemmuzArkaplan = "#A9D08E";
                        }
                        else if (Data.Temmuz == "YZL")
                        {
                            TemmuzArkaplan = "#D9E1F2";
                        }
                        @*----------TEMMUZ-1------------*@
                        @if (Data.Temmuz1 == "")
                        {
                            TemmuzArkaplan1 = "white";
                        }
                        else if (Data.Temmuz1 == "ÖNÇ")
                        {
                            TemmuzArkaplan1 = "white";
                        }
                        else if (Data.Temmuz1 == "TSR")
                        {
                            TemmuzArkaplan1 = "#F8CBAD";
                        }
                        else if (Data.Temmuz1 == "İML")
                        {
                            TemmuzArkaplan1 = "#2F75B5";
                        }
                        else if (Data.Temmuz1 == "OTO")
                        {
                            TemmuzArkaplan1 = "#A9D08E";
                        }
                        else if (Data.Temmuz1 == "YZL")
                        {
                            TemmuzArkaplan1 = "#D9E1F2";
                        }
                        @*----------AGUSTOS------------*@
                        @if (Data.Agustos == "")
                        {
                            AgustosArkaplan = "white";
                        }
                        else if (Data.Agustos == "ÖNÇ")
                        {
                            AgustosArkaplan = "white";
                        }
                        else if (Data.Agustos == "TSR")
                        {
                            AgustosArkaplan = "#F8CBAD";
                        }
                        else if (Data.Agustos == "İML")
                        {
                            AgustosArkaplan = "#2F75B5";
                        }
                        else if (Data.Agustos == "OTO")
                        {
                            AgustosArkaplan = "#A9D08E";
                        }
                        else if (Data.Agustos == "YZL")
                        {
                            AgustosArkaplan = "#D9E1F2";
                        }
                        @*----------AGUSTOS-1------------*@
                        @if (Data.Agustos1 == "")
                        {
                            AgustosArkaplan1 = "white";
                        }
                        else if (Data.Agustos1 == "ÖNÇ")
                        {
                            AgustosArkaplan1 = "white";
                        }
                        else if (Data.Agustos1 == "TSR")
                        {
                            AgustosArkaplan1 = "#F8CBAD";
                        }
                        else if (Data.Agustos1 == "İML")
                        {
                            AgustosArkaplan1 = "#2F75B5";
                        }
                        else if (Data.Agustos1 == "OTO")
                        {
                            AgustosArkaplan1 = "#A9D08E";
                        }
                        else if (Data.Agustos1 == "YZL")
                        {
                            AgustosArkaplan1 = "#D9E1F2";
                        }
                        @*----------EYLÜL------------*@
                        @if (Data.Eylul == "")
                        {
                            EylulArkaplan = "white";
                        }
                        else if (Data.Eylul == "ÖNÇ")
                        {
                            EylulArkaplan = "white";
                        }
                        else if (Data.Eylul == "TSR")
                        {
                            EylulArkaplan = "#F8CBAD";
                        }
                        else if (Data.Eylul == "İML")
                        {
                            EylulArkaplan = "#2F75B5";
                        }
                        else if (Data.Eylul == "OTO")
                        {
                            EylulArkaplan = "#A9D08E";
                        }
                        else if (Data.Eylul == "YZL")
                        {
                            EylulArkaplan = "#D9E1F2";
                        }
                        @*----------EYLÜL-1------------*@
                        @if (Data.Eylul1 == "")
                        {
                            EylulArkaplan1 = "white";
                        }
                        else if (Data.Eylul1 == "ÖNÇ")
                        {
                            EylulArkaplan1 = "white";
                        }
                        else if (Data.Eylul1 == "TSR")
                        {
                            EylulArkaplan1 = "#F8CBAD";
                        }
                        else if (Data.Eylul1 == "İML")
                        {
                            EylulArkaplan1 = "#2F75B5";
                        }
                        else if (Data.Eylul1 == "OTO")
                        {
                            EylulArkaplan1 = "#A9D08E";
                        }
                        else if (Data.Eylul1 == "YZL")
                        {
                            EylulArkaplan1 = "#D9E1F2";
                        }
                        @*----------EKİM------------*@
                        @if (Data.Ekim == "")
                        {
                            EkimArkaplan = "white";
                        }
                        else if (Data.Ekim == "ÖNÇ")
                        {
                            EkimArkaplan = "white";
                        }
                        else if (Data.Ekim == "TSR")
                        {
                            EkimArkaplan = "#F8CBAD";
                        }
                        else if (Data.Ekim == "İML")
                        {
                            EkimArkaplan = "#2F75B5";
                        }
                        else if (Data.Ekim == "OTO")
                        {
                            EkimArkaplan = "#A9D08E";
                        }
                        else if (Data.Ekim == "YZL")
                        {
                            EkimArkaplan = "#D9E1F2";
                        }
                        @*----------EKİM-1------------*@
                        @if (Data.Ekim1 == "")
                        {
                            EkimArkaplan1 = "white";
                        }
                        else if (Data.Ekim1 == "ÖNÇ")
                        {
                            EkimArkaplan1 = "white";
                        }
                        else if (Data.Ekim1 == "TSR")
                        {
                            EkimArkaplan1 = "#F8CBAD";
                        }
                        else if (Data.Ekim1 == "İML")
                        {
                            EkimArkaplan1 = "#2F75B5";
                        }
                        else if (Data.Ekim1 == "OTO")
                        {
                            EkimArkaplan1 = "#A9D08E";
                        }
                        else if (Data.Ekim1 == "YZL")
                        {
                            EkimArkaplan1 = "#D9E1F2";
                        }
                    
                        <td style="background-color:@OcakArkaplan">@Data.Ocak</td>
                        <td style="background-color:@OcakArkaplan1">@Data.Ocak1</td>
                        <td style="background-color:@SubatArkaplan">@Data.Subat</td>
                        <td style="background-color:@SubatArkaplan1">@Data.Subat1</td>
                        <td style="background-color:@MartArkaplan">@Data.Mart</td>
                        <td style="background-color:@MartArkaplan1">@Data.Mart1</td>
                        
                    </tr>
                }

Can someone help explain the Fisher Yates Array Shuffle

So I am trying to get an understanding for array shuffling.

I came across this article:
https://bost.ocks.org/mike/shuffle/

But I am confused.

function shuffle(array) {
  var m = array.length, t, i;

  // While there remain elements to shuffle…
  while (m) {

    // Pick a remaining element…
    i = Math.floor(Math.random() * m--);

    // And swap it with the current element.
    t = array[m];
    array[m] = array[i];
    array[i] = t;
  }

  return array;
}

Here are my questions:

On line 2 “m” is defined, but I don’t understand why “t” and “i” are part of that definition. Are “t” and “i” being defined as true?

The while loop condition is: “as long as m=true”. However, as far as I can tell, nothing happens to the length of the array, so won’t it stay true indefinitely?

The last part of my confusion is this:

  t = array[m];
    array[m] = array[i];
    array[i] = t;

So “t” equals a value in the array. “m” is the length of the array and this tells “t” which value to choose.

Then that value is replaced with another value in the array. This new value uses the previously generated random number to choose a different value (I can’t tell if there is a possibility to choose the same value).

This new value is re-defined as “t”. This part I just don’t understand.

It almost seems to be looping in on itself with array[i] = t and the whole thing starting all over again.

I hope someone can help.

Thanks in advance!

How can get a video to play using javascript?

I have an HTML with a video element with no controls and a button to play the video.
But when I try to play the video using the button, it doesn’t do anything.
I don’t get any error in the console, and the video just doesn’t play.
I can’t figure out what can possibly be wrong.
The video plays perfectly when I add the controls attribute in the video tag, but my aim is to not use browsers controls and to build my own controls.

Here is a sample of my code. I used this random video here just so the source is valid.
I hope someone can help. Thanks in advance.

const video = document.querySelector('video');
const playButton = document.querySelector('button');

playButton.addEventListener('click', video.play);
<video>
<source src="https://mdn.github.io/learning-area/javascript/apis/video-audio/finished/video/sintel-short.mp4" type="video/mp4"></video>
<button>Play</button>

React rerenders 4 times when calling setState after pointermove?

I have a bug where calling a single setState causes a component to rerender 4 times (2 times with strict mode off) under a specific scenario.

I’m calling setState when a swipe gesture begins. The state causes a React.lazy component to render.

const Content = React.lazy(async () => import(''));

...

const [showContent, setShowContent] = useState(false);
console.log('render'); // called 4 times

...

elem.on('pointermove', () => {
  if (isFirstMove) {
    setShowContent(true);
  }
});

...

<Suspense fallback={<Spinner />}>
  {showContent && <Content />}
</Suspense>

I checked that setShowContent is only called once. This bug goes away if I do any of the following:

  • change “pointermove” to “click”, “touchstart”, etc
  • change React.lazy + import() to a normal import

It seems like the combination of a pointermove event handler and React.lazy causes this bug. I can’t figure out a minimal reproducible example. The pointermove handler is defined in @use-gesture, a library for handling gesture.

I think this might have something to do with concurrent mode? Maybe while React is attempting to rerender, it detects that there’s another pointermove event with a handler, so it interrupts the rerender. Is there a better explanation?

In React Native, how can a user get to the previous page the user last visitted even after quitting the app?

After doing some reasearch on it, it seems like making use of navigation state in React navigation is good to get started.
So, I was reading the officual document and tring to see what it’s like on the demo app “Snack”. https://reactnavigation.org/docs/state-persistence/#:~:text=Try%20this%20example%20on%20Snack

However, it simply doesn’t work. Whenever refreshing the app, it just makes me start the first screen. I also copied the codes and tried that on VS code, but the result was the same. Maybe is it just me?? The app always starts with the first page, not the screen you last visited.

Now, I’m stack. Here is the official doc.
https://reactnavigation.org/docs/state-persistence/

Is there anyone who makes use of this thing for that? or any alternative out there?
I need some help. If you have time and try to just take a look at the link, it’d be nice!

Thank you in advance

How to reduce quality of SVG (Scalable Vector Graphics) File?

I have a 50MB SVG file (Example: here) and I have created this zoom in and out functionality to zoom in and out of SVG image but it gets extremely laggy, unfortunately. Is there a way to convert this large file SVG file into a smaller SVG file Size. Thus, I was wondering whether we can adjust the resolution of SVG image by stopping the vector graphics processing (High CPU Usage, very laggy) with a button which hopefully will stop SVG file from lagging when zooming in and out or panning it? Any help or suggestions will be extremely useful for me 🙂

(I have tried a lot of online compressor but unfortunately, all of them cannot handle this immense file size).

How to send AWS SNS notification from JavaScript lambda

I’m new to JS trying to send SNS notification from my lambda function. Can’t figure out what I am doing wrong here;

code:

const { execSync } = require("child_process");
const { writeFileSync, unlinkSync } = require("fs");
const AWS = require("aws-sdk");

const s3 = new AWS.S3();
const sns = new AWS.SNS();

const SNS_TOPIC_ARN = 'arn:aws:sns:eu-west-1:96xxxxxxxxxx:test-cost-lambda';

module.exports.virusScan = async (event, context) => {
  if (!event.Records) {
    console.log("Not an S3 event invocation!");
    return;
  }

  for (const record of event.Records) {
    if (!record.s3) {
      console.log("Not an S3 Record!");
      continue;
    }
    
    const key = decodeURIComponent(record.s3.object.key.replace(/+g/, " "));

    console.log('getting the file', key, record);

    // get the file
    const s3Object = await s3
      .getObject({
        Bucket: record.s3.bucket.name,
        Key: record.s3.object.key,
      })
      .promise();
      
    console.log('got the file', record.s3.object.key);

    // write file to disk
    writeFileSync(`/tmp/${record.s3.object.key}`, s3Object.Body);

    console.log('wrote the file');
    
    try {
      // scan it
      execSync(`./bin/clamscan --database=./var/lib/clamav /tmp/${record.s3.object.key}`);

      console.log('updating tag to clean');

      await s3
        .putObjectTagging({
          Bucket: record.s3.bucket.name,
          Key: record.s3.object.key,
          Tagging: {
            TagSet: [
              {
                Key: 'av-status',
                Value: 'clean'
              }
            ]
          }
        })
        .promise();

      console.log('updated tag to clean');
    } catch(err) {
      console.log('err', err);
      if (err.status === 1) {
        // tag as dirty, AND send SNS notification
        await s3
          .putObjectTagging({
            Bucket: record.s3.bucket.name,
            Key: record.s3.object.key,
            Tagging: {
              TagSet: [
                {
                  Key: 'av-status',
                  Value: 'dirty'
                }
              ]
            }
          })
          .promise();
        
        // send SNS notification
        await sns
          .publish({
            TopicArn: SNS_TOPIC_ARN,
            Message: `Object ${record.s3.object.key} in bucket ${record.s3.bucket.name} has been tagged as dirty.`
          })
          .promise();
      }
    }

    // delete the temp file
    unlinkSync(`/tmp/${record.s3.object.key}`);
  }
};

Basically above written lambda code scans objects in s3 bucket, then tag the objects clean/dirty.

I want to send a SNS notification whenever a AWS S3 Bucket object is tagged “dirty”. The tagging functionality is working fine, but it is not sending SNS notification as I was expecting it to.