Challenges encountered while creating a Number Tables Program (such as displaying 12 x 1 = 12) using Javascript

Currently, I am working on developing a program that can generate mathematical tables for any given number. For example:

3 x 1 = 3

3 x 2 = 6

3 x 3 = 9

3 x 4 = 12

To achieve this, the user should provide the following inputs:

(1) The number they want to create the table for (e.g., 3)

(2) Specify the starting point (e.g., 1)

(3) Specify the ending point (e.g., 4)

Below is the code snippet illustrating my current attempt:

    function validateNumber(number){
        while (isNaN(number) == true){
         number = parseInt(prompt("Please enter a valid number","5"));
         }
         }
    
    
    function mathTable (num, start, end){
        for (var i=start; i<=end; i++){
        var result = num*i;
        document.write(num + " x " + i + " = " + result + "</br>");
         }
        }
    
    
    var inputNum = parseInt(prompt("Enter the number you want the table for", "40"));
    inputNum = validateNumber(inputNum);
    
    
    var startPoint = parseInt(prompt("Enter the start point of the table", "1")); 
    startPoint = validateNumber(startPoint);
    
    var endPoint = parseInt(prompt("Enter the end point of the table", "10"));  
    endPoint = validateNumber(endPoint);
    
    
    mathTable(inputNum, startPoint, endPoint);

Answer №1

CheckIfNumber returns null (default output for functions lacking a return statement), which is then stored in the variable checkNum each time (checkNum = CheckIfNumber(...)). Consequently, the value of checkNum is null.

Upon completing the loop over the NaNs, ensure that you return the variable and assign it to the correct one:

function CheckIfNumber(number)
{
    while (isNaN(number)) {
            number = parseInt(prompt("Please enter a valid number","5"));
    }
    return number;
}

function myGrid (num, beginning, end)
{
    for (var j = beginning; j <= end; j++) {
        var z = num * j;
        document.write(num + " x " + j + " = " + z + "</br>");
    }
}

var checkNum = parseInt(prompt("Enter the number you want to generate a table for", "40"));
checkNum = CheckIfNumber(checkNum);

var startLocation = parseInt(prompt("Enter the starting point of the sequence", "1")); 
startLocation = CheckIfNumber(startLocation);

var finishLocation = parseInt(prompt("Enter the ending point of the sequence", "10"));  
finishLocation = CheckIfNumber(finishLocation);

myGrid(checkNum, startLocation, finishLocation);

Answer №2

You have committed the following errors:

inconsistent function return values, misaligned startpoint variable assignment, incorrect endpoint variable assignment

function checkIfNumber(number){
    while (isNaN(number) == true){
            number = parseInt(prompt("Please enter a valid number","5"));
        }
        return number;
     }


function createTable (num, firstVal, secondVal){
    for (var i=firstVal; i<=secondVal; i++){
        var result = num*i;
        document.write(num + " x " + i + " = " + result + "</br>");
     }
    }


var inputNum = parseInt(prompt("Enter the number you want to generate a table for", "40"));
inputNum = checkIfNumber(inputNum);


var startPoint = parseInt(prompt("Enter the starting point of the table", "1")); 
startPoint = checkIfNumber(startPoint);

var endPoint = parseInt(prompt("Enter the ending point of the table", "10"));  
endPoint = checkIfNumber(endPoint);


createTable(inputNum, startPoint, endPoint);

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

Adjust the horizontal position of a text element using CSS

I am faced with a specific challenge where I need to center the text "test" within a text tag using only CSS, without being able to directly change the HTML. <text width="13.89" height="13.89" x="291.46999999999997" y="156.55" transform= ...

Is it necessary to always pause before I click?

Currently, I am in the process of writing tests for my website using WebdriverIO with Mocha and Chai. However, I encountered an issue where my element is not rendered before attempting to interact with it. it('select application', function(done) ...

Navigating through Next.js for slug URLs such as site.com/username

Can someone help me figure out how to create a profile page for each username, like site.com/jack or site.com/jill? I'll be pulling the username info from an API that also contains the user ID and email. I'm new to Next.js and would really appre ...

Focus on an element in ThreeJS

Is it possible to adjust the zoom direction in three.js so that it zooms towards the mouse cursor? I'm having trouble finding where to change the zoom target for this specific feature. ...

Create a "load additional data" button

I'm working on building my blog and I've run into a roadblock while trying to implement a load more button. Here's what I currently have: actions: { LOAD_ARTICLE_LIST: function ({ commit }) { axios.get('someurl/articles') ...

What is the best way to extract numbers from a string using JavaScript?

I am working with a string in javascript that looks like this: var xyz= "M429,100L504.5,100L504.5,106L580,106L570,98M580,106L570,114"; My goal is to extract the numbers and store them in an array. I attempted the following code: var x=xyz.match(/\ ...

Interactive image rotator featuring navigation buttons for next and previous slides

I recently followed a tutorial from W3Schools Now, I am looking to enhance it by adding previous / next buttons for the indicators, rather than for the slider itself Here is what I aim to accomplish: https://i.sstatic.net/qH1PQ.png Below is the code sn ...

Is there a way to populate an array with Twilio fax results and then successfully send them via Express?

Currently, I am working on integrating the Twilio API into our secured backend system before sending the data to our client app. This is being done to protect our API keys and for security purposes. We have successfully implemented fax sending and checkin ...

`Increase Your Javascript Heap Memory Allocation in Next.js`

We are facing a challenge with the development environment for our Next.js application. Issue The Javascript heap memory is consistently depleting. Here are the specific error logs: FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out ...

Jpicker is renowned for its transparency feature

I am currently using the Jpicker jpicker-1.1.6.js script which can be found at Below is a snippet of my code: <script type="text/javascript"> $(function() { $.fn.jPicker.defaults.images.clientPath='/img'; var ...

Creating a state in React may lead to an endless loop

Currently, I am working on retrieving data from a Firebase real-time database and storing it in an array named 'users'. The goal is to use this array to set the "post" state. However, there seems to be an issue with the line "setPost(users);" as ...

Tips for choosing elements based on the length of an array

When using an each function, I scan the DOM to find multiple elements with a specific className. Depending on the length of this ClassName, it will create an array that is either 2 or 4 elements long. I need to distinguish between these two types of elem ...

Angular.js fails to load successfully every other time

My angular application is running into some issues with bower. At times, when I start up the server, I encounter the following error: Uncaught Error: [$injector:modulerr] Failed to instantiate module myApp due to: Error: [$injector:modulerr] Failed to in ...

Implementing dynamic width with ng-style in AngularJS

I am trying to dynamically resize a div when an event is fired from S3. In the code below, I pass the progress variable to the updateProgress function as a parameter. This function resides in the top scope of my Angular controller. s3.upload(params).on(& ...

I'm attempting to render HTML emails in ReactJS

I have been attempting to display an HTML page in React JS, but I am not achieving the same appearance. Here is the code snippet I used in React JS: <div dangerouslySetInnerHTML={{ __html: data }}/> When executed in regular HTML, the page looks lik ...

Elements on the page appear and disappear as you scroll down

Whenever my scroll reaches the bottom of element B, I want my hidden sticky element to appear. And when I scroll back up to the top of element B, the sticky element should be hidden again. Here are my codes: HTML <html> <head> </ ...

Is there a way to send a request before opening a new page that doesn't involve using synchronous XMLHttpRequest on the main thread, which is

With the deprecation of synchronous XMLHttpRequest, I am curious about any potential alternatives to achieve a similar behavior. Non-blocking asynchronous XMLHttpRequests may not always be suitable for specific cases. I am specifically referring to a scen ...

"Learn how to dynamically update the user interface in express.js using handlebars without having to refresh the

As a newcomer to using express.js and Handlebars, I am faced with the challenge of implementing autocomplete functionality. Specifically, I want to enable autocompletion in a text input field without triggering a page refresh when users interact with it. C ...

Tips for customizing the time selector in material-ui-time-picker

Is there a way to enable keyboard input control for the material-ui-time-picker? Currently, it only allows editing when clicking on the clock interface. Here is a snippet of my code: import React, { Component } from "react"; import { TimePicker } from " ...

Avoid the ability for individuals to interact with the icon embedded within a button

I'm currently working on a website where users need to interact with a button to delete an "Infraction." The button has a basic bootstrap style and includes an icon for the delete action. <button referencedInfraction="<%= i.infractionID %>" ...