Having trouble accessing properties of null (reading 'split'). Please provide a valid prompt

Hey there,

I could really use some assistance with an issue I've encountered. Whenever I try to click on "cancel" in a prompt, I keep getting this error message: "Cannot read properties of null (reading 'split')".

Basically, all I want is for the loop to terminate when I cancel it in the prompt.

ps.: In case you're wondering, this involves a simple array containing names and surnames that will be displayed using console.log in the future.

function UserList () {
    let users = [];
    while(true) {
        users.push (prompt('Please, enter your name surname?').split(' '));
        if (prompt=== null) {
            alert('cancel');
        }
    }
}
let userList = new UserList();

Answer №1

Make sure to verify if the outcome of prompt() is not null prior to attempting to separate it.

Don't forget to exit the loop when the user decides to cancel, otherwise the function will run indefinitely.

Furthermore, given that you are utilizing this as an object constructor, designate users as a property this.users.

function UserList () {
    this.users = [];
    while(true) {
        let response = prompt('Please enter your name and surname');
        if (response == null) {
            alert('Canceled');
            break;
        }
        this.users.push(response.split(' '));
    }
}

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

Revive your Chart JS visualization with interactive re-animation via onclick action!

I've been exploring Chart.js and trying to achieve something simple, but I'm having trouble. I just want the chart to re-animate when clicking on a button, but I can't seem to make it work. I attempted to attach chart.update to the onclick e ...

What is the best way to separate a char[] into two arrays in C++ after counting symbols before the '.' symbol?

I need my function to take a char[] and divide it into two arrays. The first array will represent the integer value of the number, while the second array will store the double values. I have to use two arrays because the number can be in binary, decimal, ...

How can I properly release a pointer after it has been relocated in C programming?

I am attempting to extract space-separated words and store them in a 2D array. My approach involves replacing spaces with '\0' in order to copy the string to the array. Then, I add the string pointer to the position after '\0' ...

Resizing columns in HTML table remains effective even after the page is refreshed

I've created HTML pages with tables that have multiple columns, but I'm experiencing an issue. The columns are not resizable until I refresh the page. Could someone assist me in fixing this problem and explaining why it's happening? ...

Ways to trigger an event or invoke a function after initializing Stripe JS

My checkout page is optimized with the new Stripe Payment Element for fast loading using SSR. However, I am facing an issue where the element sometimes causes the page to reload 2 or more times before functioning properly. Occasionally, it also displays ...

Updating checkbox Icon in Yii2

Is there a way to customize the checkbox icon when it is checked in Yii2? Currently, I am adding a checkbox inside a div along with an icon: <i class="fa fa-check-circle icon-check-circle"></i>. However, the checkbox shows a default check symbo ...

How to use the transform function in JavaScript

Hey there, I'm looking to create a new function. I am just starting out with javascript. I want to wrap the code below in a function like: ConfirmDelete() { }; This function should perform the same action as this code snippet: $('.btn-danger ...

What is the best method to activate a JavaScript function after 1 minute of user inactivity?

I need to set up a web page where a JavaScript function is activated after 1 minute of user inactivity. The page primarily features the <form> component. What's the best way to implement this type of function? ...

Exploring multidimensional arrays in PHP

Looking to extract information from a complex array structure $myArray = array( array('id' => 6), array( 'id' => 3, 'children' => array( 'id' => 5, 'c ...

JQuery Ajax Success does not fire as expected

I am facing an issue with my ajax call where the success function is not getting triggered. My controller gets called and the code executes without any errors. However, since my method returns void, I am not returning anything. Could this be the reason why ...

The correct terminology for divs, spans, paragraphs, images, anchor tags, table rows, table data, unordered lists, list

Just wondering, I've noticed that every time I come across a page element '<###>[content]<###>' and want to learn how to manipulate it, I usually search for something like "how to do x with div" or "how to get y from div". I know ...

Halting the execution of a jQuery function when a condition is true

Currently in the process of building a website for my new brand, I am faced with a challenge while working with jQuery for the first time. Specifically, I am encountering issues related to the state of a specific div. The code contains if statements that ...

Understanding the Button That Was Clicked! GTK

I'm working on a board setup similar to this GtkWidget *board[x][y]; If I use an array of buttons, how can I determine which button was clicked? Does g_signal_connect(G_OBJECT(board[][]), "clicked", G_CALLBACK(board_button_pressed), NULL ...

Checked boxes in the React dynamic checkbox list are not being marked

Can you please assist me with the following issue: I am in the process of creating a nested dynamic list of checkboxes for selecting companies and groups within those companies. Upon investigation, I have come up with the following code snippet const [ch ...

What is the best way to ensure that a link fragment scrolls to the top of the page in Angular?

Having trouble with link fragments in my Angular single-page-app: <a href="/#/search">Search</a> When clicking this link, it takes me to the right page but keeps my scroll position. I want it to scroll to the top of the page so I'm curre ...

Encountering a build error while using npm and node-gyp

When attempting to install client-session using npm, I encountered an error. I also tried to install node_gyp using npm, as well as cloning it using git, but the node gyp error persisted. Unfortunately, I could not locate proper documentation on the root ...

Is it possible to attach a mouse click event to styled text?

Is there a way to specify a mouse click event for an element with a decoration applied to the text, matched with regex? The option to specify a hoverMessage is available, but I would like to find a way to execute a function based on which decorated text ...

Is your blockui overlay failing to cover the entire page?

I have implemented blockui to display a "Wait ... loading" popup on my webpage. It is mostly working fine, but I am facing a small issue where the overlay does not cover the entire width of the scroll window when I scroll right (although it covers the full ...

Implementing a for loop within a scrolling function

How do I multiply functions inside the scroll loop? This is what I currently have: $(window).scroll(function() { b1Center = $("#block-1").offset().top - ($(window).height() - divHeight) / 2; b1Bottom = $("#block-1").offset().top - $(window).height(); b1T ...

How can HTML and CSS be linked to display images independently?

Check out this code: body{ background-image:url('http://wallpoper.com/images/00/31/33/51/black-background_00313351.jpg'); } div.header{ background-color:#F0F8FF; text-align:center; padding:3px; ...