Displaying error codes on a JavaScript line

Hello there! I'm new to JavaScript and currently working on a basic JavaScript module.

var starrySky = (function () {
    var defaults = {}
    var validate = {
        argNum: function (arg, num) {
            if (arg != num) {
                throw new Error('invalid argument count');
            };
        }
    }
    return {
        event: function (elementName, eventName, functionName) {
            validate.argNum(arguments.length, 3);
            if (elementName.addEventListener) {
                elementName.addEventListener(eventName, functionName, false);
            } else {
                elementName.attachEvent("on" + eventName, functionName);
            }
        }
    };
})();

When I use it like this:

starrySky.event("", document, "DOMContentLoaded", function() {
    console.log("DOM loaded");
});

The error line in the console says:

Is there a way to display an error message within the function call when there are 4 arguments instead of 3?

Answer №1

It appears that you are providing an extra argument "" for the event method

sky.event("", document, "DOMContentLoaded", function(){
----------^

However, your event method definition actually expects only three parameters instead of four

sky.event(document, "DOMContentLoaded", function(){

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

JavaScript Cookie Array Handling

Is it possible to create a 2D array as a cookie in JavaScript? If so, how can I go about creating this array cookie and looping through it to retrieve data efficiently? Any guidance on this would be greatly appreciated. Thank you! ...

Using ng-repeat with an AJAX-called JSON is not possible

I've been struggling with handling JSON data from an AJAX call and displaying it using ng-repeat. If you want to take a look at my code in a more organized way, I've shared it on http://jsfiddle.net/7quj9omw/. However, please note that the code ...

Coloring a table in vue.js based on performance rankings

I'm facing an issue with sorting my data performance in vue js. Here is the script I have created so far: <template> <div> <div class="row"> <h2> Campaign Performance </h2> <table class=&q ...

Creating a visual representation of system uptime using PHP and JavaScript by aggregating a series of heartbeats

I have multiple devices that send a signal every minute, known as a heartbeat. The timestamps of these heartbeats are stored in a MySQL table like so: +----+-----------+---------------------+ | id | entity_id | heartbeat | +----+-----------+---- ...

Merge similar elements in an array by a specific property

I am looking to create a new array that combines values of repeated campaigns let arrayName = [ { campaign: "a", value: 3 }, { campaign: "b", value: 5 }, { campaign: "c", value: 7 }, { campaign: "a", value: 9 }, ]; EXPECTED OUTPUT: [ { campaign: ...

What flaws are present in this authentication system?

As a developer with a passion for coding, rather than a security expert, I came across The definitive guide to form-based website authentication, which highlighted the importance of SSL or complex algorithms in safeguarding login data from eavesdropping. D ...

Puppeteer is not compatible with VPS hosting on DigitalOcean

I'm currently using a droplet on DigitalOcean and encountering the following error: (node:5549) UnhandledPromiseRejectionWarning: TimeoutError: Navigation Timeout Exceeded: 300000ms exceeded at Promise.then (/var/www/screenshot/node_modules/puppe ...

Function $locationChangeStart in AngularJS not triggering

My code using the $location service in angular seems to be malfunctioning. angular.module("app",[]).run(function($rootScope) { var $offFunction = $rootScope.$on("$locationChangeStart", function(e) { if (confirm("Are you sure")) return $ ...

Emphasize specific lines within a textarea

I am facing a challenge with a textarea dedicated to validating telephone numbers. My goal is to highlight the lines that contain invalid telephone numbers by adding two asterisks in front of them. However, I'm struggling to figure out how to highlig ...

Tips for smoothly animating and showing content as the user scrolls to a specific element on the page

Here is a sample template: <template> <div id="Test"> <transition name="fade"> <div class="row" id="RowOne"> <p>Lorem ipsum dolor odit qui sit?</p> </div> ...

Printing dynamic data

When the print button is clicked, I need to print dynamically generated data from a table that has an ID. The table data (td and tr) is dynamically generated. I have successfully retrieved the table data and attempted to print everything using window.prin ...

How can you ensure that the results are displayed only when all the fields in the form are properly

Take a look at the code below: <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js" type="text/javascript"></script> <script type="text/javascript"> $(document).ready(function(){ ...

Best Practices for Making Remote Requests in Ruby on Rails

Currently, I am creating a Single Page Application using Ruby on Rails for the first time. Although I am still learning, I have set up a side menu with links and a container on the right part of the page to display partial pages. An example of one of my me ...

The time parameter in jQuery's ScrollLeft animation method seems to be disregarded

Hey everyone, I'm having trouble understanding this: thumb.animate( {'scrollLeft': active.width()*3}, 'slow' ); The scrolling works fine, but the "slow" parameter seems to be ignored. Instead of moving slowly, it moves instan ...

Ways to extract link value in Angular

Is there a way to extract a value from a link using Angular 4? I have used *ngIf and would like to display a div based on the value within the link. <div *ngIf="obtain the value from the current href"> ...

Is it possible to swap out images using srcset attribute?

Currently, I am facing an issue regarding changing the img on mobile and tablet devices to display different images. As a beginner with jQuery, I couldn't resolve it using that framework, so I am exploring options with html5. I am wondering if there ...

The refresh method cannot be found in 'x'. (In the context of 'x()', 'x' represents an Object instance)

Is there a way for me to access the refresh() method within my UpdateLokalListe function? Can I integrate the function into my class? I followed the instructions in this guide: https://reactnavigation.org/docs/function-after-focusing-screen Thank you ht ...

Encountering a post AJAX issue in Spring JAVA

I have successfully implemented MVC spring and now I want to consume it with SAPUI5 (javascript) using AJAX. However, I encountered an error "415 (Unsupported Media Type)". I have used swagger in spring to test CRUD operations. In swagger, I was able to in ...

Using Rxjs to dynamically map values from an array with forkJoin

Greetings! I have a collection of Boolean observables and would like to apply a logical AND operation. Currently, I am passing static values 'a' and 'b', but I am unsure of the number of elements in the totalKeys array. import { forkJoi ...

Tips for shrinking a sticky header when scrolling using Angular and JavaScript

Looking for a way to modify the header component in an Angular application so that it shrinks as the user scrolls down while maintaining its sticky functionality. How can I adjust the display of the lower half of the header to show no text when the user s ...