I am transmitting a function from an object to an array. However, when I attempt to calculate the sum of the array, the

Below is the provided code snippet:

var myObject = {
  x: 5,
  h: function(){
     varOne = 2*2;
     varTwo = 3*2;
     varThree = varOne*varTwo;
     },
  d: 4
};

var g = myObject.h();
var xyz = g;
var abc = 2;
var efg = 3;
var somearray = [xyz,abc,efg];
var z =  0;
for(i=0; i<somearray.length; i++){
    z += somearray[i];   
}

The output is NaN. What could be the reason behind this? If I modify it to: h: function(){return 2*2;}, then there are no issues. As a newcomer to JS, I've been researching online for answers without success. Would using parseInt or parseFloat help in this scenario and if so, which variable would require it? Appreciate your assistance.

Answer №1

The function h is not returning any value, resulting in undefined which when added produces NaN. Try the following code:

h: function(){
   var varOne = 2*2;
   var varTwo = 3*2;
   return  varOne*varTwo;
 },

It's advisable to use the var keyword for defining variables within a specific scope to avoid global pollution.

Because you are assigning numerical values to the variables, there is no need to parse them using parseInt.

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

Dynamically populate 7 select boxes with options using JQuery

I have a webpage that includes 14 different dropdown menus, one for each day of the week (Monday to Sunday). Each day has two dropdowns: one for opening time and one for closing time. I used jQuery to populate all 14 dropdowns with a pre-defined list of ho ...

The Chrome Keyboard on Android seamlessly passes words to the next input field when the focus changes in JavaScript

When using two input fields, pressing the enter key on the first field in Chrome (49) on Android (6.0.1) carries over the last word typed to the new input due to the right-bottom button on the standard keyboard. Is there a way to prevent this behavior or a ...

Numerous Capabilities of Javascript

I have a question about JavaScript and how to implement multiple functions on a webpage. I am new to JavaScript and struggling with getting multiple functions to work successfully. I have searched online but can't find a solution that fits my specific ...

Updating radio button based on selection change in jQuery

I'm struggling with jQuery and can't seem to figure out how to change the selected radio button based on a value in another select box. <div class="radio-inline" id="sourceDiv" role="group"> <input type="radio" id="sourceBtns1" na ...

Change the defaultValue of a React component following a successful request

In my project, I am dealing with a DropDown component from a separate library that renders various options. The component supports an array of objects as its source and setting a default value is straightforward when the list is static. However, I'm f ...

Creating intricate mazes using canvas drawing techniques

I recently developed a maze generator as a personal project utilizing a graph. While the generation logic works perfectly, I am facing challenges when it comes to rendering the maze. In my approach, each cell is represented by an array of 4 edges where the ...

Is the array in Java thread-safe when one thread changes the value and another thread reads the value?

I have implemented a simple ringbuffer where one thread is used for the poll() method and another thread is used for the offer() method in the test1() method. I have tested it multiple times and it always returns true. Can someone provide an explanation? ...

What causes the vertical scroll bar to shift on its own?

I'm puzzled by the behavior of the vertical scroll bar when clicking on "Line 9" - it automatically moves to the top position and subsequent clicks don't affect it. Can someone shed light on why this happens and how to resolve it? I am using Fire ...

What is the best way to assign an ID to a specific HTML element within NetSuite's Document Object Model

Attempting to utilize jQuery in NetSuite to assign a value to an element for testing purposes, but struggling to locate the HTML DOM ID of certain custom drop-down lists. It's not the internal ID. For example: <input type="text" id="soid"> Wh ...

Utilizing the Web Crypto API to implement encryption and decryption in Typescript

Due to security requirements, data cannot be sent in plain-text to the server and credentials should not be visible in the browser's network tab. Hashing could have been used but existing passwords are already stored with hash and salting applied. To ...

How to modify the overlay color in the TouchableHighlight component using an arrow function in React Native

With touchableHighlight, I found that I could easily modify the overlay color using the following code: <TouchableHighlight onPress={this.toggle.bind(this)} underlayColor="#f1f1f1"> However, when attemptin ...

Encountering issues with running NPM on my Ubuntu server hosted on Digital Ocean

After successfully installing node (nodejs), I encountered a persistent error when attempting to use NPM. Despite researching the issue extensively and trying different solutions, I have been unable to resolve it. Here is the error message displayed in th ...

Combining array elements to create a formatted string

If I am presented with an array such as the following: array = ["A", "P", "P", "L", "E"] My goal is to utilize the elements of the array to construct a sentence. For example, something along the lines of "I would like to have an APPLE" I vaguely recall ...

Slider malfunctioning following AJAX loading

After the user clicks on #mail-wrap, I am attempting to trigger the ready event which loads another page with AJAX so that sss() can be refired. Despite my efforts, it seems like it is not working as expected. Can you help me identify what might be going w ...

Generate a fresh array by evaluating the differences between two arrays of objects

Imagine having two arrays of objects like this: let oldBookDetails = [ {'name':'Harry pottar','amount':10, is_modified: false}, {'name':'LOTR','amount':20, is_modified: false}, {' ...

What is the best way to retrieve the parent of the initial jQuery object?

At the bottom of a lengthy table containing multiple button.enter-match elements, I am using the following code: $("button.enter-match") .button() .on('click', function() { $("form.enter-match").dialog({ modal: true, height: ...

Combining two arrays in Go results in a container assignment error

I've been troubleshooting my code all morning but can't seem to figure out what's wrong. The error message says it can't assign containers. Here's the playground link for you to check. Below is the problematic code: func My_Merg ...

One text label to display for a MultiLineString using MapLibre GL JS

I am attempting to show text labels for MultiLineString features within a geoJSON file using MapLibre GL JS. By utilizing the symbol-placement: point option, I aim to display the labels across various zoom levels rather than only when extremely close, as w ...

The function of the Nuxt.js server-side plugin is not functioning as intended

I recently developed a server-side plugin and encountered the following error: context.app.handleServerError is not a function // hanlde-server-error.js export default ({ app }, inject) => { app.handleServerError = (method, error, data) => { ...

Is it possible to author TypeScript modules in a format other than ES6?

Is it possible to utilize AMD for writing code? define([ 'hb!./some/file.hb' ], function(template) { // }) ...