Oops! Looks like there was an error: weight is not defined when trying to calculate BMI with the HTMLButtonElement

I'm currently working on a project to create a BMI calculator following specific instructions. I've managed to follow all the guidelines except one regarding the letsCalculateBMI function. The instruction states:

letsCalculateBMI should extract the selected value from the SELECT element, pass that value to a getSelectedUser function call, which in turn should return the user object corresponding to the selected value. This user object needs to be assigned to a variable called user.

I'm confused about how to make the getSelectedUser function work within the letsCalculateBMI function to retrieve the user object and assign it to the user variable.

In the computeBMI arrow function, the user parameter is immediately destructured into weight, height, and country properties for quick reference.

The current error message I'm encountering is

Uncaught ReferenceError: weight is not defined at HTMLButtonElement.letsCalculateBMI
.

index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" 
          content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />

    <title>Mini App</title>


  </head>
  <body>    


    <div class="select">
      <select class="select-text">
        <option disabled selected>Select User</option>        
      </select>
    </div>


    <div class="details mdc-elevation--z3">
      <p>
        <span class="prop" data-age>Age :</span>
        <span class="value" data-age-value>23 years</span>
      </p>
      <p>
        <span class="prop" data-height>Height :</span>
        <span class="value" data-height-value>169cm</span>
      </p>
      <p>
        <span class="prop" data-weight>Weight :</span>
        <span class="value" data-weight-value>68kg</span>
      </p>
      <p>
        <span class="prop" data-gender>Gender :</span>
        <span class="value" data-gender-value>Female</span>
      </p>
      <p>
        <span class="prop" data-country>Country :</span>
        <span class="value" data-country-value>Nigerian</span>
      </p>
    </div>

    <button id="oracle" class="mdc-button" onclick="letsCalculateBMI()">
      Calculate BMI
    </button>
    <div id="outcome">
      <h5 class="mdc-typography--headline5">
        BMI
      </h5>
      <p class ="bmi-text"></p>
    </div>

    <script>
      const users = [];

      const countriesWithLowerBmi = ["Chad", "Sierra Leone", "Mali", "Gambia", "Uganda", "Ghana", "Senegal", "Somalia", "Ivory Coast", "Isreal"];      

      const featToMeter = 0.3048;

      const bmiCountryRatio = 0.82;

      const computeBMI = ({weight, height, country}) => {

        const heightInMeters = height * featToMeter;
        let BMI = weight / (heightInMeters^2);

       if (countriesWithLowerBmi.includes(country)) 
         BMI *= bmiCountryRatio;

       return Math.round(BMI, 2);
      };

      const getSelectedUser = (userId) => {
        return users.find(({id}) => id === userId);
      };

      const displaySelectedUser = ({target}) => {
        const user = getSelectedUser(target.value);
        const properties = Object.keys(user);

        properties.forEach(prop => {
          const span = document.querySelector(`span[data-${prop}-value]`);
            if(span) {
              span.textContent= user[prop];   
            }
        })               
      }

      const letsCalculateBMI = () => {

        const value = document.querySelector('.select-text').value;

        getSelectedUser(value);

        const user = {weight, height, country}
        const bmi = computeBMI(user);

        document.querySelector('.bmi-text').innerHTML = bmi
      };

      const powerupTheUI = () => {
        const button = document.querySelector('#oracle');

        const select = document.querySelector('.select-text');

        select.addEventListener('change', displaySelectedUser);

        button.addEventListener('click',letsCalculateBMI);
      };      

      const displayUsers = (users) => {
        users.forEach(user => {
        const select = document.querySelector('.select-text');
        const option = document.createElement('option');

        option.text = user.name; 
        option.value = user.id;
        select.appendChild(option);
        });
      };


      const fetchAndDisplayUsers = () => {
        users.push(
          {
          age: 40,
          weight: 75,
          height: 6,
          country: 'Nigeria',
          name: 'Charles Odili',
          id: 'dfhb454768DghtF'
          },
          {
          age: 23,
          weight: 68,
          height: 6,
          country: 'Nigeria',
          name: 'Simpcy',
          id: 'gibb12erish'
          }
        );

        displayUsers(users);
      };

      const startApp = () => {
        powerupTheUI();
        fetchAndDisplayUsers();
      };

      startApp();
    </script>
  </body>
</html>

Answer №1

The issue at hand is clear: the variable weight is not defined because it has not been declared (the same applies to height and country). These variables are considered as properties of the user, hence they need to be fetched from the user object that is returned by the getSelectedUser function.

One way to address this is:

user = getSelectedUser(value);
computeBMI(user.weight, user.height, user.country);

This resolution should rectify your problem, however....

To streamline the implementation in the computeBMI arrow function, the user parameter can be immediately destructured into its weight, height, and country properties.

In my viewpoint, this approach does not align with good OOP design - as all necessary information already exists within the object; there's no need for additional code to separate them.

A preferable method would be something like the following:

...

computeBMI(getSelectedUser(value));

...

const computeBMI = (user) => {

    const heightInMeters = user.height * featToMeter;
    let BMI = user.weight / (heightInMeters^2);

    if (countriesWithLowerBmi.includes(user.country)) 
        BMI *= bmiCountryRatio;

    return Math.round(BMI, 2);
};

Answer №2

Here is the ideal code for letsCalculateBMI:

const letsCalculateBMI = () => {
        const value = document.querySelector('.select-text').value;
        const user = fetchSelectedUserData(value);      
        const bmiResult = calculateUserBMI(user);
        document.getElementById("bmi-result").innerHTML = bmiResult;
};

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

Arrange list items in a circular pattern

Is there a way to achieve the desired appearance for my navbar like this link? I have tried rotating the text, but the vertical text ends up too far from the horizontal. Any suggestions? .vertical { float:right; writing-mode:tb-rl;/*IE*/ w ...

Sending form data using Node.js

Is there a way to send input form data as part of a URL in Node.js? Below is an example code snippet demonstrating this. app.get('/', (req, res) => { res.render('index'); }); app.post('/scrape', function(req, res){ ...

Issues encountered with invoking function in CodeIgniter controller through Ajax

Running a codeigniter website with an add to cart functionality. When the user clicks the add to cart button, the product is successfully added to the cart after the page reloads. The controller code for this feature is as follows: public function buy($ ...

The function that iterates through the 'categoria' state and returns a new array is not functioning properly

Having difficulty with the object of a function using .map(). It works when the code is used directly, but not when put inside a function. For example: if(this.state.cat){ return _.map(this.state.cat, categoria => { if(this.state.search_ ...

What is the best way to generate a live map with constantly updating markers?

Is it possible for me to learn how to develop a live map similar to the one on this site: www.lightningmaps.org? It's fascinating to watch new markers pop up every few seconds. I'm interested in building a real-time map that can track IP locatio ...

Maintain the expanded menu even after selecting a sub-item using jQuery

After conducting a thorough search, I was unable to find exactly what I needed. I have successfully implemented cookies on my menu so that when the page is reloaded, it remembers which menus were open. However, I noticed that clicking on a sub-item of Hy ...

Obtaining Mouse Click X, Y, and Z Coordinates with Three.js

I am currently utilizing version 68 of three.js. My objective is to gather the X, Y, and Z coordinates upon clicking somewhere on the canvas. Despite following the steps outlined in this guide, I am encountering a consistent Z value of 0: Mouse / Canvas X ...

"Upon invoking the console log, an empty value is being returned when attempting to access the

Why is console.log returning a blank line when trying to retrieve the value of a text input variable? HTML <label for="subject">Subject</label> <input type="text" id=" ...

Having trouble identifying the issue with the dependent select drop down in my Active Admin setup (Rails 3.2, Active Admin 1.0)

I am currently working on developing a Ruby on Rails application that involves three models: Games that can be categorized into a Sector (referred to as GameSector) and a subsector (known as GameSubsector) A sector consists of multiple subsectors. A Subs ...

Establishing the preset values for Material-UI toggle button group

I utilized the Material UI library to implement a button toggle widget for selecting options. Check out my codesandbox project here - https://codesandbox.io/s/50pl0jy3xk The user can interact by choosing a membership type: adult, child, or infant. Option ...

What is the purpose of using square brackets in the angular.module() function in AngularJS?

const myapp=angular.module('myApp',[]); As someone venturing into the realm of angularjs, I have a question. What is the significance of using [] in angular.module()? If anyone could shed some light on this, it would be greatly appreciated. ...

Interactive data visualization with hover-over details

I am utilizing datamaps to showcase the countries of the world, however, I want the graph to be centered. The issue arises when I hover over a country and the pop up appears all the way to the left, aligned with where the country would be if it wasn't ...

Could Flexbox CSS be used to create a responsive layout like this?

Appreciate your help in advance. I am facing a challenge with the current layout. <article> <section class="content">1</section> <aside class="ads">2</aside> <section class="comments">3</section> < ...

working with html data in a bootstrap modal using javascript

Utilizing an id, I pass data to a bootstrap modal and link that id with stored data. $("#fruit").html($(this).data("fruit")); In addition, I am appending data to the bootstrap modal $('#mymodal').find('.modal-body').append('< ...

Is it possible to extract user-defined keywords from the URL?

Is it possible to implement dynamic functionality like this using PHP on my website (www.mywebsite.com)? If a user types www.mywebsite.com/banana into the URL bar, can my website take the keyword "banana" as input and search for it in a database table? ...

Retrieve a JavaScript object based on a specific value

Looking at my object : TeamMember = { 0 : {"ID": 100, "Name": "MNO", "Role": 2}, 1 : {"ID": 101, "Name": "PQR", "Role": 3}, 2 : {"ID": 103, "Name": "STU", "Role": 3} } I am trying to retrieve TeamMember[1] : {"ID": 101, "Name": "PQR", "Role": 3} a ...

The Bootstrap form fails to function properly on mobile devices

On my Bootstrap form, everything works fine when entering data on the desktop version. However, I am facing an issue with the mobile version where I cannot input any information into the fields. The fields are unresponsive and not allowing me to enter an ...

Retrieving Django view information using AJAX

Currently, I am in the process of developing an application that updates the main page's data via AJAX. After reading through a helpful response on this Stackoverflow post, I implemented AJAX to refresh my page. In addition to this, I have a specific ...

Utilizing React for incorporating HTML5 canvas gradients

I'm currently working on an app that allows users to generate a background gradient with two distinct colors using React. The issue I'm facing is that while the first color appears correctly, the second color ends up looking more like a solid col ...

Using Three.js, generate a series of meshes that combine to create a seamless 90-degree donut shape

I'm on a quest to discover an algorithm that can create the following shape in Three.js. Here is my rough sketch of the expected shape The number of meshes needed to form the 90 degree donut, as well as the thickness and spacing between them, should a ...