JavaScript prompt function returning a value of undefined

In my current coding challenge, I'm attempting to create a function that takes two variables, adds them together, and displays the result in an alert format. Unfortunately, I seem to be encountering an issue with my code. Any guidance or assistance would be greatly appreciated!

var num1 = prompt("Enter first number:");
var num2 = prompt("Enter second number:");
var a = parseInt(num1);
var b = parseInt(num2);
var total = 0;

function addTwoNumbers(x, y) {
    total = x + y;
}

addTwoNumbers(a, b);

alert("The sum of the two numbers is: " + total);

Answer ā„–1

The function was not receiving any input parameters. Although variables a and b were declared, they were not being used as arguments for the function.

var num1 = prompt("How many items do you have?");
var num2 = prompt("How many items will you add?");
var a = parseInt(num1);
var b = parseInt(num2);

function calculateTotal(a,b) {
    return a + b;
}
alert("You will have a total of " + calculateTotal(a, b));

Example

http://jsfiddle.net/abc123def/2/

Edit 1

To clarify my point.

function calculateTotal(x, y) {
    return x + y;
}

The parameters mentioned above act as placeholders for the actual values that will be provided. Inside the function, you would reference these passed values through the specified parameters.

Answer ā„–2

Ensure that you are properly passing the values of a and b when calling your function. If you have declared them as parameters, make sure to use them in your function code or remove them from the declaration.

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

Moving a Ball Back and Forth Using Three.js

Iā€™m looking to create a continuous motion for a Ball in Three.js, where it moves to the right, returns to its starting position, and then repeats the sequence. var geometry = new THREE.SphereGeometry( 5, 32, 32); var material = new THREE.MeshPhongMateri ...

React: What is the best way to dynamically render images by iterating through a list?

Currently, I am attempting to iterate through an array of objects. Each object within the staff array in my JSON contains an imgUrl: { "home": { ... "staff": [ { ... "imgUrl": "../Images/Jon ...

Can you effectively link together AngularJS promises originating from various controllers or locations?

Attempting to explain in as much detail as possible, the configuration file config.js contains the following code snippet: .run(['$rootScope', '$location', 'UserService', 'CompanyService', function($rootScope, $loca ...

A more concise validation function for mandatory fields

When working on an HTML application with TypeScript, I encountered a situation where I needed to build an error message for a form that had several required fields. In my TypeScript file, I created a function called hasErrors() which checks each field and ...

Tips for utilizing Vue.js scoped styles with components that are loaded through view-router

I want to customize the styling of Vue.js components loaded through <view-router> using scoped styles. This is the code I have: <template> <div id="admin"> <router-view></router-view> </div> </template> ...

Creating a simple bootstrap code for developing plugins in Wordpress

After successfully coding in Brackets with the Theseus plugin on my local machine, I encountered a problem when attempting to transfer my code to a Wordpress installation. The transition from Brackets to Wordpress seems far more complicated than expected. ...

What is the process for enabling a Submit button and updating its value following a successful Ajax response within a Django environment?

My current project involves using Django as the web framework. I am in the process of setting up an AWS lab or AWS account. To avoid multiple submissions, I have disabled the Submit button once the user enters information such as lab name, CIDR block, etc. ...

avoidable constructor in a react component

When it comes to specifying initial state in a class, I've noticed different approaches being used by people. class App extends React.Component { constructor() { super(); this.state = { user: [] } } render() { return <p>Hi</p> ...

What is the best way to incorporate external JavaScript libraries using ES6 import syntax?

I'm currently facing a challenge in incorporating older javascript libraries into modern ES6 projects. Specifically, I am working on a React project that has been compiled using webpack, written with ES6, and transpiled with Babel. Each component util ...

Extracting the value of an HTML element from a string variable in AngularJS

I am facing an issue with my application where the content of an HTML element is received as a template from the server. I am attempting to assign this template, which is essentially a string, and have the variables within the template linked to the contro ...

Is it possible to send a data attribute from an HTML element using AJAX to a some.php page when it is clicked?

One scenario involves retrieving the product id from a database and inserting it into a data-idproduct attribute. <a class="cart" href="" data-idproduct="<?php echo $itemArtikal['id_product'] ?>">Add to C ...

Submitting Files with jQuery AJAX in the ASP.NET MVC Framework

Currently, I am working on an application that requires me to upload a file using AJAX. I have implemented the jQuery.form library for this purpose, but strangely, when the action is triggered, the controller receives an empty list of files. Below is the H ...

Can you provide an example of a basic JSON structure that defines a payment transaction using PayPal?

I need a JSON example that defines a simple PayPal donation, specifically including parameters for a payment date and an option to set the donation as recurring annually. This isn't covered in the official PayPal documentation. I've attempted the ...

What is the most effective method for transitioning between pages while incorporating eye-catching animation effects?

I'm currently working on a website and I'm looking to add some animated page transitions. Is there a method to achieve this without using ajax to call the next page and display it with all the required effects? Or should I consider using React to ...

The click function is a member of an object within an emit event

I stumbled upon the code snippet below, which triggers the notification-alert event and passes an object as a parameter. this.$root.$emit('notification-alert', { text, type: 'warning', click: () = ...

Creating a multiline textarea with ellipsis using ReactJS

Looking to create a component with a multiline textfield that displays ellipsis (...) for text overflow beyond 2 lines. How can I achieve this through CSS only without modifying the actual stored text? Utilizing the React component found at: link Appreci ...

Issues encountered when running a Vue.js project that already exists

I'm encountering an issue while trying to launch a pre-built UI project using vue.js. Despite having Python installed with the environment variable properly set, I keep getting an error when running the npm install command. How can this be resolved? n ...

The Functional Components array is not correctly adding the props content

I'm facing an issue with adding an array of <TokenFeed/> functional components to the <Feeds/> component. The problem arises when I try to use the onClick() event handler to pass text to my <App/>, as it doesn't behave as expect ...

What is the best way to retrieve multiple tables results from a stored procedure or function in Postgresql?

Is it possible to retrieve multiple table results in a stored procedure or function in PostgreSQL? I am looking to return multiple tables as a result set in PostgreSQL and then access the data in a Nest.JS Application. Can anyone provide guidance on how t ...

The Vue 3 composition API does not allow for the return of values from helper functions to components

It seems that there may be a scope issue within my helper function. I have successfully implemented the same logic in my component and it works flawlessly. I have already utilized a composable function to incorporate ref() with the variable that needs to ...