The variable referenced is not defined, resulting in a TypeError

Having some trouble with my code..

function compare(string,pattern){
var patternUpper = pattern.toUpperCase(); // Convert pattern to uppercase
var stringUpper = string.toUpperCase(); // Convert string to uppercase
    for(var i=0;i<stringUpper.length-1;i++){
        if(stringUpper.indexOf(patternUpper.charAt(i))<0)
        return false;
    }
    return true;
}

I'm puzzled by the "pattern is undefined" error message in Firefox debugger, even though it's clearly defined in the function. Any ideas why?

Any assistance would be greatly appreciated.

- Liam

UPDATE: The debugger is also reporting "string is undefined" when I comment out the second line of that snippet.

The call to compare2 is made here:

alert(compare("thisisatest","ahtsit"));

The result seems correct, but I believe this issue could be causing problems elsewhere in my program.

Answer №1

If the "pattern" parameter is undefined, regardless of the browser you are using, it typically indicates an issue with how the function is being called. Take a look at the call stack and inspect the parameters being passed one step before within the function to identify any potential errors. (And remember, avoid naming variables after keywords or class names, like the variable named "string")

I hope this information proves helpful.

Cheers!

Answer №2

The scenario will occur only if the second parameter is not provided or if it is explicitly set to undefined

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

Is it possible to show an image without altering the Box dimensions?

Hi there, I am currently working on designing a footer and I have encountered an issue. I want to add an image in a specific position, but when I do so, it affects the size of the box. I was wondering if there is a way to set the image as a background or b ...

Discovering all class names following the same naming convention and storing them in an array through Javascript

Hey everyone, I could use some assistance with a coding challenge. I'm aiming to extract all class names from the DOM that share a common naming convention and store them in an array. For instance: <div class="userName_342">John</div> & ...

Changing PHP string into a JavaScript array for use in Google Charts

Attempting to construct a Google Sankey Chart using data fetched from a MySQL database. Once the page has loaded, an ajax call is initiated to retrieve the data from MySQL and present it on the view. This is how the data is being returned from PHP: echo ...

Material Style Minimal - There is a slight space between the text field and the colored line at the bottom

Having a problem with Material Design Lite text field where there is a small gap between the bottom colored line and the gray starting line. Every MDL text field example I try on my page shows the same issue. What could be causing this locally? My frontend ...

Providing both app and io as parameters to a module

Extracted from my server.js: const app = require('express')(); const server = require('http').createServer(app); const io = require("socket.io").listen(server); server.listen(port, function(){ console.log('Server listening at ...

Saving URI from HTTP request in Javascript using Google Drive API: A step-by-step guide

I'm struggling to understand and implement the instructions provided at https://developers.google.com/drive/v3/web/manage-uploads#save-session-uri While I've successfully used javascript to upload files to Google Drive, I am now faced with a cha ...

Having trouble with Ajax not sending data to PHP in JSON format

After attempting various methods using different ajax techniques, I still cannot get this to work. Being new to ajax and json, I initially thought that the issue might be due to the data not being in a proper form. However, other ajax processes structured ...

How can I stop a user from navigating through links or submitting forms using jQuery?

I'm exploring the idea of converting my website into a Single Page Application. One challenge I am facing is capturing the navigation process, for example: <a href="myData.php">Change My Data</a> When a user clicks on the "Change My Data ...

jQuery is failing to properly render dynamic content data identifiers

Need help with a dynamic HTML div <a data-id="17" onclick="getcustomer();"> <div class="note note-success"> <h4 class="block">A</h4> <p>Email : <a href="/cdn-cgi/l/email-protection" class="__cf_email__" ...

The collapse feature of Bootstrap 4 Navbar is unresponsive

Struggling with the Bootstrap 4 navbar collapse feature issue. In the mobile viewport, instead of collapsing, it displays items as a vertical list. Here is the code snippet: <html> <body> <!-- Navbar --> <nav class="navbar na ...

jQuery Soundboard - Pressing a single button will automatically deactivate all other buttons

I am currently developing a unique jQuery/PHP soundboard feature where I am faced with the challenge of stopping the HTML5 Audio playback when clicking on just one button, while attached to several other buttons. Here is what I have managed to code so far: ...

Having Difficulty Rendering Tube Shapes in Three.js

Having just started working with Three.js, I'm facing challenges in displaying a tube on the screen. Despite using a basic material to simplify lighting, the tube is not showing up as expected. Any assistance in troubleshooting this issue would be gre ...

Preventing the triggering of events or functions while utilizing angular-gantt

Currently, I am utilizing the Angular directive called angular-gantt to create a gantt chart that displays tasks. Among the various options available, the ones I am focusing on are on-row-clicked, on-task-clicked, and on-task-updated. The issue I am facin ...

"A common error that may occur in Node.js is the inability to set headers after they have

I have encountered an issue while working on my node js application. I can load the page successfully once, but then I start getting an error message saying "Can't set headers after they are sent". If anyone has any suggestions or advice on how to re ...

Using jQuery to Delete a DOM Element Once the Ajax Call is Successful

I'm currently facing an issue with removing an element after a successful ajax request. Below is my ajax code snippet: verifyRequest.denyUser = function(requestId,element){ $.ajax({ url: loaderURL+'idverified/denyRequest', ...

What distinguishes the act of altering attributes using React.createContext() in comparison to the value property?

Imagine you have the code snippet below: //App.js //logoutHandler function defined here function App { const testContext = React.createContext({ isLoggedIn: false, onLogout: logoutHandler }) return ( <testContext.Provider> //Some code w ...

After receiving a response from the .ajax function, the message will be added to the specified

After a successful AJAX request, the returned data will include a status parameter. If the status is false (indicating no AJAX failure), the data will contain an array of element names along with their corresponding error messages. The objective here is to ...

Top suggestions for maintaining the Firefox extension popup open when clicking the navigation button

As I work on developing a Firefox extension using jetpack, I have encountered an issue. It seems that when I click on the navigation button, a popup opens but then closes automatically when I click elsewhere. Is there a way for me to override this automa ...

Plunker is displaying the same code, but unfortunately, it cannot run due to an "ERROR: mixed

I'm having trouble with a code I copied from a Plunker example. It's not working as expected. The code is from this Plunker example: http://embed.plnkr.co/xCyDvQ/ I copied and pasted it into my own Plunker creation, but as you can see in my Plu ...

html5-jquery checkbox change event not firing

I successfully implemented an on/off switch using the method outlined in for HTML5. The functionality of the switch itself is working perfectly - click on it to toggle between states and query the checked state of the input/checkbox. However, I am facing ...