Modify the values within two arrays in JavaScript

As a novice, I am attempting to code a function that will change values within an array. Specifically, my goal is to convert ºC to ºK. However, the current code I have written is not functioning correctly and is throwing an error message stating "j is undefined". What could be causing this issue?

//Initial Arrays

var I_t1 = new Array();
var V_t1 = new Array();

//Arrays for Table 1
var K_t1 = new Array();
var P_t1 = new Array();

function kelvin() {
    var i;
    var j = new Array();
    var k = new Array();
    
    var k;
    var j = V_t1.lenght;   
    
    var k = I_t1.lenght; // The k variable will be set to the length of array T

    for(i=0; i < j.length; i++){   
        K_t1[i] = (V_t1[i] * 200);
    }

    for(i=0; i < k.length; i++){      
        P_t1[i] = (I_t1[i] * 400);
    }
}

That's the current state of the code. My main question troubles on how to adjust this function to accomplish its intended purpose.

-What modifications should I make to ensure this function works as intended?

Answer №1

Pay attention to your spelling - the correct syntax is .length, not .lenght!

Answer №2

Some have pointed out the issue with the .length method, but my suggestion for resolving this issue would be to utilize higher order functions such as "map". This can help simplify the process and make it more efficient. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

For example, a function to convert ºC to ºK could look like:

function celciusToKelvin(celciusVal){
   return celciusVal + 274.15;
}

If you have an array of celcius values such as

var celciusValues = [12,23,34,45];
, you can simply use the map function as follows:

var kelvinValues = celciusValues.map(celciusToKelvin);

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

Tips for passing a void function as a parameter in C++?

I'm working on passing a function as a parameter. I've created a function called sortingRunTime() that takes an array and a function as parameters and then prints out the execution time. void sortingRunTime(int* arr, void(*functiontocall)(int* ar ...

To enhance your React Class Component, make sure to utilize the withStyles feature when

This particular component is not set as a default export component. I am attempting to apply some styles to it but struggling to figure out how to encapsulate it in an HOC. Hence, the current issue where it does not recognize the classes variable. import ...

How can I correctly rotate an object around a specific point using three.js?

Most tutorials/questions about three.js suggest using a parent object to rotate an object around a specific point. This involves creating a parent object at the desired position, attaching the object to it, and then rotating the parent to achieve the desir ...

Javascript function remains dormant

I am currently developing a program that assesses individuals' capacity to learn various foreign language words. To achieve this, I require a javascript function that conceals one DIV, populates an empty one, and subsequently invokes setTimeout(). How ...

After clicking in the Firefox browser, the Window.open function is unexpectedly showing [object Window]

I have added an anchor link that, when clicked, opens a new window with the specified link. Below is the code I used for this purpose: <a href="javascript:window.open('https://www.remax.fi/fi/kiinteistonvalitys/tietosuojaseloste/', &apos ...

Corrupted Excel file after downloading using the axios library in Vue.js

I'm having trouble downloading an Excel file using axios. Below is the code I have tried: axios.post(backendURL + 'api/client?file_name='+file_name,params, { file_name: file_name }, { responseType: 'blob' ...

Converting an object to an array functions better as an independent function rather than as a method of the object

After decoding the JSON data from an API, I attempted to convert the object into an array. To achieve this, I created a class function specifically for this task. Here is the code snippet: public function objectToArray($result) { if (is_object($result ...

Determine the difference in time

There are two input types for time: 1. Time of entry. 2. Time of exit. For example: Start: 00:00 End: 01:30 Result: 1.5 Start: 14:00 End: 00:00 Result: 10 An algorithm needs to be created to calculate the number of employees working at a given time. Th ...

Change the colors of a dynamic row in an HTML table using various options

I have successfully implemented a dynamic table using HTML and JavaScript. However, instead of applying alternate row colors, I want to assign a unique color to each row. How can I achieve this? <!DOCTYPE HTML> <html> <head> ...

The scale configuration for scale: x is not valid for creating a time scale chart using chart.js

I am currently utilizing VueJS and attempting to integrate a time scale chart using Chart.js. However, I encountered the following error: Invalid scale configuration for scale: x Below is my configuration : First, I have a component named Chart.vue with ...

Toggle visibility (individually duplicating) one thousand sentences while maintaining optimal browser and host performance

I have created JQuery scripts to toggle the display of future sentences on my page (approximately 1000 duplicates) and I am seeking the most efficient method (for browser/host) to accomplish this task. Here are two functional solutions that I have already ...

Tips for activating Vue.js production mode with webpack 2.7

I have encountered performance issues with Vue.js in my existing code base. Additionally, I noticed a notice in the browser console: https://i.sstatic.net/KY1B3.png So, I believe that one simple solution could be to switch Vue into production mode. Foll ...

Having difficulty assigning a selected value in select2 using JavaScript with Ajax mode

I am trying to use a select2 element that loads data from a database using ajax. My goal is to load the value from the database and have it selected as the default value in edit mode. However, I am encountering issues with using the trigger function to ach ...

Curious as to why JavaScript restricts the use of two variables entering at the same time

Currently, I am tackling an Ajax-php livesearch programming challenge. I am facing an issue where JavaScript is unable to receive the values of 2 parameters that I want to input in HTML input boxes. Any idea why this might be happening? This is my HTML co ...

unorthodox method of transmitting parameters within express.js source code

While searching for the source code of express router, I came across this interesting piece: var debug = require('debug')('express:router:route'); I am curious to know more about this unusual way of passing arguments. Can someone ...

Difficulty with Bootstrap 4 mobile navbar dropdown feature

<div class="baslik baslik1 baslik2 "> <nav class="navbar bg-light navbar-light navbar-expand-sm sticky-top "> <a href="./index.html" class="navbar-brand"><img src="img/512x512logo.png" ...

Troubleshooting issues with Ajax and Jena

Whenever I attempt to utilize AJAX to call Jena in my servlet, I encounter this error: java.lang.ClassNotFoundException: com.hp.hpl.jena.sparql.core.Prologue at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1516) at org.apa ...

When incorporating an array into a span tag, only the final string is being shown by the loop

I need assistance with changing the content of a span tag every 1.5 seconds, but currently it only displays the final word in the 'list' array. Below is my JavaScript code: var list = [ "websites", "user interfaces" ]; setInterval(fun ...

Utilizing ReadableStream as a body for mocking the HTTP backend in unit testing for Angular 2

Looking to simulate the http backend following this helpful guide. This is my progress so far: Test scenario below describe('DataService', () => { beforeEach(async(() => { TestBed.configureTestingModule({ imports: [ ...

Using Ajax to implement Basic Authentication for securely accessing an https-protected webpage without requiring users to manually enter their username and password in a

Is there a way to access and display a website page hosted at this URL - , without encountering any authentication dialog box from my application on the same network? I have been unable to bypass the username and password entry request. I successfully imp ...