AssertionError [ERR_ASSERTION]: The value of undefined is not equal to 390 in the GitLab

I'm a bit confused about the AssertionError [ERR_ASSERTION]: undefined == 390 in Gitlab.

What I need is:

The sumSalaries(obj) function, which should take an object obj as a parameter where the field names correspond to the employee's name and the values represent each employee's salary.

Consider the following code snippet:

export default function sumSalaries(obj) {
    let salaries = {
        John: 100,
        Jane: 160,
        Mike: 130
    };

    let sum = 0;
    for (let key in salaries) {
        if (salaries.hasOwnProperty(key)) {
            sum += salaries[key];
        }
    }
    obj = sum;
    console.log(obj);
}

sumSalaries();

The test should:

import sumSalaries from "../test.js";
import assert from "assert";

describe("\n\ntest.js", () => {
    it("should return the correct sum", () => {
        [
            [
                {
                    John: 100,
                    Ann: 160,
                    Pete: 130,
                },
                390,
            ],
            [
                {
                    John: 80,
                    Jane: 160,
                    Mike: 190,
                },
                430,
            ],
            [
                {
                    Charlie: 84,
                    Victor: 160,
                    Pete: 200,
                },
                444,
            ],
        ].map((obj) => {
            let salaries = obj[0];
            let sum = obj[1];
            assert.equal(sumSalaries(salaries), sum);
        });
    });
});

The error message I received from Gitlab is:

AssertionError [ERR_ASSERTION]: undefined == 390

Answer №1

Upon reviewing your code, let's analyze what you have written:

export default function sumSalaries(obj) {

  let salaries = {
    John: 100,
    Jane: 160,
    Mike: 130
  };

Your current function is meant to calculate the sum of the salaries passed in, but it seems like you have hardcoded a fixed list of salaries instead. This will limit the function to only summing these specific three salaries.

You proceed to add up these salaries as expected:

  let sum = 0;
  for (let key in salaries) {
    if (salaries.hasOwnProperty(key)) {
      sum += salaries[key];
    }
  }

The variable obj originally contained the incoming salaries, but now it stores the sum from your hardcoded list:

  obj = sum;

Then you log this sum to the console, which can be helpful for debugging purposes, but should be removed once done with debugging.

  console.log(obj);

The function concludes without encountering a return statement, leading to a return value of undefined. This explains the assertion error stating undefined == 390: your function returned undefined while the test expected 390.

}

To rectify this issue:

Firstly, rename the function parameter obj to salaries and remove the hardcoded values to allow the function to properly sum the given salaries.

Secondly, after calculating the sum, remember to use return to return the final result.

Your updated and functional code should look like this:

export default function sumSalaries(salaries) {
  let sum = 0;
  for (let key in salaries) {
    if (salaries.hasOwnProperty(key)) {
      sum += salaries[key];
    }
  }
  return sum;
}

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

extract the key identifier from the JSON reply

My JSON ResponseData example for form0 is provided below: { "MaterialType": "camera", "AssetID": 202773, "forms": [ { "release": "asyncCmd/accessCameraMulti", "action": "rest/Asset/202773/cameraAccessMultiple", ...

Leveraging hapi-auth-basic

I attempted to incorporate a basic authentication strategy following a tutorial I stumbled upon. Here is the setup of my server.js file. 'use strict'; const Hapi=require('hapi'); const sequelize = require('sequelize'); c ...

What are the steps to integrating an HTML source with jQuery Autocomplete?

Struggling with a challenging ajax call to an HTML source that is essential. The goal is to convert the html response into a format suitable for displaying in the jQuery autocomplete list. Working with Autocomplete and Ajax $("#From, #To, #FromVacation ...

Solving the issue of "_c is not defined" error in Vue functional component

I've been experimenting with creating functional components in Vue using the render method. Here's an example of how I attempted to do this: import Vue from "vue" const { render, staticRenderFns } = Vue.compile(`<div>Hello World</div&g ...

When attempting to navigate to a new route with a query, I encounter the error message "NavigationDuplicated: Avoided redundant navigation to current location."

I'm facing an issue with my navigation header setup. It includes a search bar that redirects users to the home view with the search query as a parameter. Here's the code snippet for reference: <template lang="html"> <div cl ...

What is the best way to merge an array of objects into a single object?

Is there a way to dynamically convert object1 into object2, considering that the keys like 'apple' and 'water' inside the objects are not static? const object1 = { apple:[ {a:''}, {b:'&apos ...

Leveraging a specialized Angular filter as a cellFilter within the columnDefs of ui-grid, set in an Angular constant

In my Angular application called myApp, I have a unique filter named myFilter. Additionally, I am utilizing UI Grid to display data in multiple grids such as myGrid1 and myGrid2. To streamline the process, I have organized column definitions for these grid ...

Alan AI does not support installation on React Native

❯ To install the @alan-ai/alan-sdk-react-native package, run: sudo npm i @alan-ai/alan-sdk-react-native --save > Post installation for @alan-ai/contact: > Copying AlanSDK.js, AlanButton.js, and AlanText.js to destination Mak ...

How does the function window.open determine whether to open a new tab or switch to an existing tab based on specific criteria?

As I navigate through new tabs and existing tabs, I am curious about the criteria that determines whether a new tab is opened or if the browser simply reopens an existing tab with the same name. Could someone shed light on the specific parameters being co ...

Verification of three-dimensional password format with the use of AngularJS

I am trying to implement password validation in Angular.js that requires a combination of alphabetical, numerical, one upper case letter, one special character, no spaces, and a minimum length of 8 characters. How can I achieve this using Angular.js? Here ...

Tips for displaying Japanese characters followed by numbers in a single line on a web browser using CSS

Combining Japanese characters with numbers without spacing can present formatting challenges. For example, with a string like "新しいフォルダ000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 ...

Searching for the perfect jQuery regex to validate date formats

In my application, there is an input box that allows users to enter a string date like "today" or "tomorrow". However, I am facing a new challenge now - dates such as "3 march" or "8 january." The input box includes a dropdown menu feature where users can ...

I am looking to develop a unique event that can be triggered by any component and listened to by any other component within my Angular 7 application

Looking to create a unique event that can be triggered from any component and listened to by any other component within my Angular 7 app. Imagine having one component with a button that, when clicked, triggers the custom event along with some data. Then, ...

A versatile jQuery function for extracting values from a :input selector

Is there a universal method in jQuery to retrieve the value of any :input element? I pose this question because I have a webpage containing select and checkbox inputs, as seen in the code below: for (var i = 0; i < arguments.length; i++) { ...

Even after dynamically removing the class from the element, the Click event can still be detected

My buttons have a design like this: <a class="h-modal-btn branch-modal-btn"> Buy Now </a> The issue arises when I need to remove certain classes and add attributes to these buttons based on the query string in the URL. Skipping ahead ...

How to Trigger a Child Component Function from a Parent Component in React.js

I want to trigger a function in my child component when a button is clicked in the parent component. Parent Component: class Parent extends Component{ constructor(props){ super(props); this.state = { //.. } } ...

What steps can be taken to integrate JavaScript into an ASP.NET control?

<script type="text/javascript"> $(document).ready(function () { $('input[name="time"]').ptTimeSelect(); }); </script> the script shown above is functioning correctly with this HTML input: <input name="time" value= ...

What are the best practices for setting access permissions when using Azure AD authorization flow?

I am in the process of creating a small Next.js application with the following structure: Authenticate a user via Azure AD using Next-Auth Allow the user to initiate a SQL Database Sync by clicking a button within the app with the access token obtained du ...

``How can one validate a radio button within a reducer by utilizing the event object that is passed into the reducer process

In my React app, I am using the following reducer: const initialState = { genderRadio : false, ageRadio : false } const reducer = (state = initialState, action) => { switch(action.type) { case "VALI_RADIO_INP": console. ...

Database hosted on Heroku platform

Curious to know, if you utilize Heroku for hosting, what database do you prefer? Do you lean towards PostgreSql, MongoDB, or another option altogether? I initially developed a bot using sqlite3, but quickly discovered that Heroku does not support it and ...