Executing a console.log statement within an array nested inside a function of an object

Today in school, I learned about objects and how a function inside an object is actually called a method. In my method, I have an array and I'm trying to change a global variable to display a different location in the console.log. However, all I keep getting is undefined in the last console.log. I'm attempting to change the global variable, var myLocation = 0, to output a different myLocation inside the getLocation method.

var myLocation = 0

var worker = {
    realName:       "Willson",
    title:          "Assistant Maintenance Supervisor",
    maintenance:    true,
    vehicals:       ["2008 Dodge Caliber SRT-4", "2012 Jeep Wrangler Unlimited"      ],
    getLocation:    function () { 
        myLocation [0] = "Villas at Sunrise Mountain";
        myLocation [1] = "Reno Villas"; 
        myLocation [2] = "lost"; 
        myLocation [3] = "back home";
    }
};

console.log(worker.realName + " is a " + worker.title + " and drives a " + worker.vehicals[1] + " to work."); 

var destination = {
    property: ["Villas at Sunrise Mountain", "Reno Villas", "lost", "back home"]
};

console.log ("He sold his " + worker.vehicals[0] + "." + " Today he is working at " + worker.getLocation.myLocation);

Answer №1

var myPlace = 0

var helper = {
    realName:       "Willson",
    title:          "Assistant Maintenance Supervisor",
    maintenance:    true,
    vehicles:       ["2008 Dodge Caliber SRT-4", "2012 Jeep Wrangler Unlimited"      ],
    getPlace:    function () { 
        var myPlaces = [];
        myPlaces [0] = "Villas at Sunrise Mountain";
        myPlaces [1] = "Reno Villas"; 
        myPlaces [2] = "lost"; 
        myPlaces [3] = "back home";

        return myPlaces[myPlace];

    }
};

console.log(helper.realName + " is a " + helper.title + " and drives a " + helper.vehicles[1] + " to work."); 

var stop = {
    property: ["Villas at Sunrise Mountain", "Reno Villas", "lost", "back home"]
};

console.log ("He sold his " + helper.vehicles[0] + "." + " Today he is working at " + helper.getPlace());

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

Ways to create collapsible navigation bars in your website

As someone exploring client-side development, I may be misusing the term "collapsible" in my title. What I aim to accomplish in my web application is allowing users to collapse header bars into small chevrons and expand them back when necessary. I am on t ...

Designing a carousel-style menu list with navigation buttons for moving forward and backward

I'm running into some trouble while attempting to create a carousel. Firstly, the issue I am facing is that when you continuously click on the Next button, the function keeps working even after reaching the last item. I'm not sure how to make th ...

The issue arises when using Hive Serde with an Array of Structures where the JSON Array is unable to be converted to a Java Object Array

I have built a table: Add jar /../xlibs/hive-json-serde-0.2.jar; CREATE EXTERNAL TABLE SerdeTest (Unique_ID STRING ,MemberID STRING ,Data ARRAY> ) PARTITIONED BY (Pyear INT, Pmonth INT) ROW FORMAT SERDE "org.apache.hadoop.hive.contrib.serd ...

Java servlet is unable to interpret the name of an html select tag

Currently, I am implementing a feature where table rows with select boxes are added dynamically and the values of these select boxes are retrieved in a servlet. The process of adding the select boxes is functioning properly; however, when attempting to ret ...

Establishing the NumbroJS cultural settings

I have been attempting to modify numbro's culture. I initially tried the straightforward method, but encountered an error: Unknown culture : ' + code numbro.culture('fr-FR'); My attempt looked like this: const br = require('numb ...

What is the best way to ensure the remaining PHP script is executed after using json_encode()?

My current project involves creating a form with four input fields: name, email, phone, and message. To handle the submission process, I am utilizing JavaScript in conjunction with Ajax to send the user inputs to a PHP file for validation and mailing using ...

Is it possible to populate an empty list of objects within the Page_Load scope after the page has finished loading?

I am facing a challenge where I need to populate the list var docfiles = new List<string>(); with file names uploaded by the user using dropzone js. However, the list remains empty because when the page loads for the first time, httpRequest.Files.C ...

PHP count function providing incorrect results

Here are the details of two sets of data: $info = Array ( [@url] => url [@type] => image/jpeg [@expression] => full [@width] => 644 [@height] => 429 ) $total_count = count($info); // This returns 5, but I need it to be ...

Creating a numpy array with elements set to either 0 or 1 according to a given probability array

When working with Python and the random.choices function, I can achieve the following: import random array_probabilities = [0.5 for _ in range(4)] print(array_probabilities) # [0.5, 0.5, 0.5, 0.5] a = [random.choices([0, 1], weights=[1 - probability, p ...

On Windows systems, where exactly does npm place its packages during installation when using Node version 10.x

Does anyone know where I can locate the locally installed npm modules when using NodeJS version 10? I have checked under C:\Users\MyUser\AppData\Roaming, but I cannot find the "npm" folder. ...

Changing from localhost:3000/admin to localhost:3000 within the local server

I am currently developing a node.js application. My goal is to have the homepage rendered after the admin successfully uploads data, transitioning from localhost:3000/admin to localhost:3000. I attempted to achieve this using the code snippet below: route ...

Tips for switching out images depending on the time of day

Currently, I have a script that dynamically changes the background color of my webpage based on the time of day. However, I am facing issues trying to implement a similar functionality for replacing an image source. The current code is also time zone-based ...

What is the best way to determine someone's age if their birthdate is stored in an array

I need to determine the ages of students by using their birthdate values stored in arrays like this: $day = array(30, 03); $month = array(07, 12); $year = array(1998, 1997); What is the best way to calculate their current age and store it in an array? $ ...

Various issues arising from a single input in Foundation Abide with AngularJS

Trying to set up a form using Foundation has proven challenging, especially when it comes to implementing more advanced error checking. If we take the example of an input field like this: <input pattern="username" required type="text" placeholder="user ...

The viewport width in NextJS does not extend across the entire screen on mobile devices

I'm currently tackling a challenge with my NextJS Website project. It's the first time this issue has arisen for me. Typically, I set the body width to 100% or 100vw and everything works smoothly. However, upon switching to a mobile device, I not ...

Assistance required for activating an unidentified function or plugin within a Chrome extension

I am currently working on a project involving a chrome extension designed to automate tasks on a specific website. My main focus right now is trying to trigger the click event of a link that has an event handler set up through an anonymous function as show ...

Strategies for creating a dynamic progress bar using jQuery and JavaScript

I'm currently working on a project that involves increasing a percentage number while filling up the background color inside it based on the percentage value. The current setup is functional in terms of animating the background, but I need it to dynam ...

Is there a way to refresh my list following a POST request when utilizing a separate endpoint?

I'm struggling with updating my list after a POST request. I haven't been able to find any solutions online. Typically, I would just push an object into an array, but it seems different in this case. This function utilizes 2 API endpoints. The f ...

What is the best way to establish a new JavaScript data type that can be accessed using both key and index?

Looking to create a unique datatype or object in JavaScript that allows access by both index and key, as well as having a length property to display the number of items in the datatype. Something along the lines of: MyDataType[0].name="John" MyDataType[0 ...

When altering the object's state value in ReactJs, be cautious of the warning that states not to directly mutate the state. It is recommended to

Is there a proper way to change the status as an object? I attempted it in the following manner, but it seems incorrect as it generates a setState warning during compilation. I am seeking guidance on how to modify a state with an object value. class Anim ...