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

Utilizing AngularJS: Establishing a Universal Parent State in UI-Router for Modals and Shared Components

In my code using UI-Router and Bootstrap-ui modal, I have defined the state as shown below. reference var state = { name: 'modala', parent: 'home', onEnter: function($modal, $state) { modalInstance = $modal.open({ ...

Instructions for showing a timer on a webpage from a managed bean by utilizing JavaScript

I'm currently tackling the challenge of passing a Date from a managed bean to JavaScript and then displaying it as a timer in the format "hh:mm:ss aa". I've attempted it but so far, no luck. Code: DateTimeManagmentMB.java (Managed Bean) import ...

Using AngularJS, you can easily preselect an option in a select menu that is populated using ng-options

Exploring how to use AngularJS ng-option directive to populate a select menu and set a default option. The JSON data retrieved by AngularJS: {"AF":"Afghanistan","AX":"\u00c5land Islands","AL":"Albania","DZ":"Algeria","AS":"American Samoa","AD"} The ...

jQuery - can you identify which specific object from the entire set this is?

I am curious if there is a simple method to identify which DOM object I have when it is also part of another set of objects. To illustrate, let's consider the following scenario: There are 5 div elements: <div id="1"></div> <div id="2 ...

Issue: The DLL initialization routine failed for electron, but it works perfectly fine on node.js

Currently, I am facing an issue while attempting to load a custom module in electron that is written in D using the node_dlang package. The module loads successfully with node, but encounters failures within electron. The test run with node, which works w ...

"Troubleshooting a blank page issue in Angular UI Router caused by query string

I've been struggling with an issue related to query string parameters for quite some time. When I navigate to /, everything works perfectly fine. However, if I try something like /?anything, it simply doesn't work. These are the configurations in ...

Angular 7 router navigate encountering a matching issue

I created a router module with the following configuration: RouterModule.forRoot([ {path: 'general', component: MapComponent}, {path: 'general/:id', component: MapComponent}, {path: '', component: LoginComponent} ]) Sub ...

As the background image shifts, it gradually grows in size

I'm attempting to create an interesting visual effect where a background image moves horizontally and loops seamlessly, creating the illusion of an infinite loop of images. Using only HTML and CSS, I've run into an issue where the background ima ...

Tips for transferring a jQuery array to PHP

I am encountering an issue when trying to send a jQuery array to PHP. Initially, I have one form in HTML and upon clicking 'add', I end up with two forms. Afterwards, I input data into the form which is then stored in a jQuery array. However, I a ...

Set a variable to represent a color for the background styling in CSS

My goal is to create an application that allows users to change the background color of a button and then copy the CSS code with the new background color from the <style> tags. To achieve this, I am utilizing a color picker tool from . I believe I ...

Could someone please provide clarification on this specific JavaScript syntax? I am unsure about the usage of `const {

Although I am not very familiar with javascript, I have come across this syntax and I would greatly appreciate it if someone could help me understand it! Regarding Node.js const { check, validationResult } = require('express-validator/check') ...

Struggling with validating forms with JavaScript

I'm having trouble with the following JavaScript code: <script type="text/javascript"> function checkDetails(search) { var search = documment.getElementById('query'); if(search.value ==''||search.val ...

In React, ensure a component's state is preserved when the browser history changes

I recently developed a React application that features a classic layout with a left-side menu, title, footer, and main content section. The side menu includes navigation links structured using components such as <List>, <ListItem>, etc. from th ...

Executing a callback in Node.js after a loop is complete

As a newcomer to Node, I have been facing difficulties with the asynchronous nature of the platform while attempting to append data in a for loop of API calls. function emotionsAPI(data, next){ for(let url in data) { if(data.hasOwnProperty(url ...

Using scope in ng-style to manipulate a portion of a CSS property value in Angular

I've been attempting to add a border using ng-style, but I'm struggling to figure out how to concatenate the value from the scope variable. None of the methods below seem to be working for me: <div ng-style="{'border-top' :'1p ...

Expanding Text Box

My goal is to create a textarea that automatically adjusts its size as the user types or pastes long text into it. This is my current implementation. class AutoResizeTextArea extends InputBase { render() { const { label, placeholder, wrap, ro ...

Retrieve the ActiveTabIndex value from an Ajax TabContainer using Javascript

Is there a way to retrieve the ActiveTabIndex from TabContainer when a tab is selected by the user? I've attempted the following approach without success. <script type="text/javascript"> function GetActiveTabIndex() { var tc = docum ...

How can you retrieve attributes from a specific element within a dropdown menu?

I came across this intriguing HTML code snippet: <select id="selectSomething"> <option id="4" data-name="tomato" data-email="<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="cfbba0a2aebba08fbbe1aca0a2">[email ...

gulp-open not functioning properly following the use of createWriteStream

I am utilizing gulp, gulp-eslint, and gulp-open to generate a report detailing ESLint outcomes. The linting and file creation processes function correctly; however, the task aimed at opening the file containing my report encounters issues. gulp.task(&apos ...

Building a Next.js application in a docker container on a local machine using minikube is successful, however, the build fails when attempting to build it on a staging environment

I've encountered a puzzling issue. My next.js app is running in a docker container. It builds successfully on my local Ubuntu machine with minikube and ks8 orchestration, but it gets stuck indefinitely during the build process on the staging setup. T ...