Having trouble with a JavaScript coding issue - struggling to make the cell contents match up

Currently, I am working on a project and trying to determine if a specific store appears within a certain date range in a set of cell ranges. Once identified, I need to count how many times that particular store shows up within the range. Although my formula is mostly functional, I am encountering an issue when comparing the store to the unique values in the range - they do not match even though the input for both values is identical.

Array.prototype.getUnique = function () {
    var u = {}, a = [];
    for (var i = 0, l = this.length; i < l; ++i) {
        if (u.hasOwnProperty(this[i])) {
            continue;
        }
        a.push(this[i]);
        u[this[i]] = 1;
    }

  return a;
}   
    function countOnDate(datestart,dateend,daterange,sheet,valuesrange){
      var datestart = Utilities.formatDate(new Date(datestart), "GMT+1", "M/d/yyy")
      var dateend = Utilities.formatDate(new Date(dateend), "GMT+1", "M/d/yyy")
      var range = sheet.getRange(daterange).getValues();
      var countrange = sheet.getRange(valuesrange).getValues()
      var count = [];
      var countrangeunique = countrange.getUnique()
      //Logger.log("countrange:"+countrange)
      //Logger.log("unique"+countrangeunique)
      for (var a =0;a < countrangeunique.length;a++){
        var countnumber = 0;
        for (var b = 0; b < range.length;b++){
          if (range[b] !="" || range[b] !=[]){
          var rangeformated = Utilities.formatDate(new Date(range[b]),"GMT+1","M/d/yyy")
          Logger.log((datestart <= rangeformated) +":"+(rangeformated <= dateend))    
          if (datestart <= rangeformated && rangeformated <= dateend){
            Logger.log("activestore:"+countrangeunique[a]+" storescan:"+countrange[b]+"match:"+(countrange[b] == countrangeunique[a]))    
            if (countrange[b] == countrangeunique[a]) {
                  countnumber++
                  count[a][0] = countrangeunique[a] 
                  count[a][1] = countnumber
                  Logger.log("counternumber"+countnumber+" store: "+countrangeunique[a])
              }
          }
        }
      }
      }   

Initially, the 'countUnique' function is invoked to identify the unique value in the store range specified by the 'valuesrange' variable. This range contains IDs for the stores where the employee has worked, such as 2512 or 1533. The 'countOnDate' function then searches for unique store IDs and verifies whether each ID falls within the current month based on the provided start ('datestart') and end ('dateend') dates along with the 'daterange'. Dates are formatted as 'm/d/yyy' representing when the employee worked. Any dates outside the month or empty should be disregarded. The system proceeds to compare the unique number against the range of store numbers occurring within the month and aims to increment their count. However, the comparison 'countrange[b] == countrangeunique[a]' does not yield correct matches despite same values being present. How can this discrepancy be rectified? Currently, the logs indicate:

Pastebin Log

Answer №1

I discovered a solution which involved coercing the JavaScript to interpret the returned values as integers by utilizing parseInt(value,10). Best wishes to anyone encountering a similar issue.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

JS implementing a listener to modify a Google Map from a separate class

Currently, I am in the process of migrating my Google Map functionality from ionic-native to JavaScript. I am facing an issue while attempting to modify the click listener of my map from a separate class. The problem seems to be related to property errors. ...

Determining If Props Have Been Undefined In React

Greetings and thank you for taking the time to read this: I am currently working through a tutorial that can be found here: My issue lies in the creation of an author, where the application is trying to load the URL of the current author's ID, which ...

Having trouble with jQuery events not triggering properly after dynamically inserting elements using an ajax request?

It's strange that all my jQuery events become unresponsive after an AJAX call. When I use a load function, once the JSP reloads, none of the events seem to work properly. Any suggestions? Below is the code that triggers the function call: $('#p ...

How to use regular expressions to extract a specific query parameter from a URL

I am trying to extract the first character after "action/" in a URL similar to: server/area/controller/action/4/?param=2" The server could be one of the following: http://localhost/abc https://test.abc.com https://abc.om Is there a way to achieve this ...

Getting the parent object based on a filtered nested property can be achieved by utilizing a

I am currently facing an issue with filtering an array of nested objects. The problem arises when trying to filter the parent object based on a specific property within its child object. let line = "xyz"; let data = [ { "header": { ...

Guide to switching the text within an <a> tag using Angular 7

Trying to change the text of a button from "Unfreeze" to "Freeze" when clicking on it. Below is the code snippet: <a class="btn btn-primary full-width" (click)="clickFreeze($event)"> <i class="fa fa-plus-circle"></i>U ...

"Steps for implementing a multiselect feature with checkboxes, including the ability to check all and uncheck all, in a React application

After creating a custom component for selecting multiple options and adding a check all feature, the challenge arises when needing an uncheck option. Solution? Implementing an uncheck all feature alongside the select all functionality, but how to modify th ...

What is the reason behind the limitation of resizing only the most recent page element I added?

On my webpage, I have implemented a feature where I can click on an image to enlarge it. Clicking on the image again shrinks it back to its original size. However, I am encountering an issue where only the latest image added to the screen is resizable, e ...

What is the best way to select the enclosed <a> tag within an <li> element?

Below is the JavaScript code I have attempted: $('#footer').find('.browse li').click(function(e){ $(this).find('a').click(); }); Here is the relevant HTML: <div id="footer" class="span-24"><div ...

Using AJAX in Django to detect fields with JavaScript

I am currently working on a model: class Name(models.Model): name = models.CharField(max_length=255) project = models.CharField(max_length=255) story = models.CharField(max_length=500) depends_on = models.CharField(max_length=500, ...

Multiple Button Triggered jQuery Ajax Function

I'm currently working on a project where I retrieve data from MySQL to create 4 buttons. I am using jQuery/ajax to trigger an event when any of the buttons are clicked. However, only the first button seems to be functioning properly, while the other t ...

Is there a way to seamlessly integrate typeahead.js with jquery.validate?

Currently, I have a website built on ASP.NET MVC 5 which utilizes jQuery validation (specifically 'jquery.validate.js' in the MVC project template). My goal is to implement type-ahead functionality using 'typeahead.js' on an input field ...

Using JQuery to swap out background images in CSS

I have been working on a slider that is supposed to fade one picture over another. The issue is that the background change seems to only work in the Dreamweaver preview and not in the actual browser. loop = setInterval(function(){ $("#slider").css("ba ...

Exploring Fetch API with Promise.all

I have successfully implemented a functionality that fetches data from two URLs and performs an action only when both requests are successful. However, I am curious if there is a more efficient and succinct way to achieve the same result. Helper functions ...

Ways to detect and respond to events in this particular scenario

I'm creating necessary components dynamically based on the provided CSS. This process involves iterating through a response array and generating HTML elements accordingly. for (var i = 0; i < responseinner.length; i++) { for (var k = 0; k < ...

Can you explain the meaning of <!-- in javascript?

Have you ever noticed the <!-- and --> characters being used around JavaScript code like this: <script type="text/javascript"> <!-- function validateForm() { if(document.pressed == 'Submit') { ...

Retrieve the element by its id using JavaScript even when there are no form tags present on the webpage

Is it possible to retrieve an element by its ID using JavaScript when there is no form tag available? I am trying to obtain the value of a textbox with the following command, but I keep receiving a 'null or undefined' error message. It works cor ...

Exploring ways to loop through a JSON array and embed it into an HTML element

After the ajax request, the data returned is structured as follows: Data = [ ["18/02/2019", "A"], ["19/03/2019", "B"], ["21/05/2019", "C"], ] The ajax request was successful and the data is stored in a variable named Data within a function. ...

Execute an executable file with elevated permissions using Node.js

I'm looking to run an .exe file as an administrator using Node. I attempted the following code, but it's not working: exec('runas /user:Administrator "app.exe"', function(err, data) { console.log(err) ...

Displaying success and failure messages on a modal window using Ajax in PHP form

Unable to display error messages or success messages in the same modal window. I have set up Wordpress and using the following code: Form <form id="formcotizador" method="post" action="<?php echo get_template_directory_uri(); ?>/cotizador/procc ...