Tips for adding values to an array using an arrow function

Can someone assist me with pushing even numbers into an array using an arrow function? I'm unsure of how to do this.

Here's my code:

var arrayEvenNumbers = [];
var evenNumbers = (arrayEvenNumbers) => {
  for (i = 2; i <= 20; i++) {
    if (i % 2 == 0) {
      arrayEvenNumbers.push(i);
    }
  }
}
console.log(evenNumbers(arrayEvenNumbers));

I am a beginner and learning as I go, so any help is appreciated. Thank you!

Note: My English may not be perfect as it is not my first language.

Answer №1

in this manner

let evenNumArray = [];

const findEvenNumbers = arr =>
  {
  for (let j = 2; j <= 20; j++)  
    if (j % 2 === 0)  arr.push(j);
 
  return arr
  }

console.log( findEvenNumbers(evenNumArray) )

console.log( evenNumArray )

Answer №2

evenNumbers successfully adds the numbers, however it does not return a value. To achieve this, consider using:

evenNumbers(listOfEvenNumbers);
console.log(listOfEvenNumbers);

Answer №3

Don't forget to include the return statement in your code:

var newArray = [];
var perfectSquares = (array) => {
    for(let j = 1; j <= 10; j++) {
        let square = Math.pow(j, 2);
        array.push(square);
    }
    return(array);
};
console.log(perfectSquares(newArray));

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

The React application is functioning properly; however, during compilation, it continually displays an error stating that the module cannot be found

Compilation Failed. Error: Module not found, unable to resolve the specified path. webpack compiled with 1 error I was hoping for a successful compilation, but unfortunately encountered an error. It's perplexing as the application is functioning co ...

now.js - There is no method in Object, however the click event works in jQuery

Here is a simple NowJS code snippet for the client side: $j(document).ready(function() { window.now = nowInitialize('http://xxx.yyy:6564'); now.recvMsg = function(message){ $j("body").append("<br>" + message); } $ ...

Replacing data in a Node server

I am currently working on a server that temporarily stores files in its memory before uploading them to the database. Below is the code snippet I'm using: uploadImage(file, uid, res) { var fs = require('fs'); mongoose.connect(config ...

Send an AJAX request to the server without waiting for a response using a JavaScript variable

My click counter is not sending variables to the server. I have tried finding examples on how to do this, but no matter what I attempt, the data is not being sent to the server. It seems like using AJAX would be the best option, but I must be doing someth ...

What is the reason behind allowing JavaScript to perform mathematical operations with a string type number?

let firstNum = 10; let secondNum = "10"; console.log(firstNum * secondNum); // Result: 100 console.log(secondNum * secondNum); // Result: 100 ...

Change the left position of the sliding menu in real-time

I am currently designing a website with a sliding menu feature. By default, the menu is set to -370px on the left, displaying only the "MENU" text initially. When a user hovers over the menu, it expands to the right, allowing them to select different menu ...

Instructions for executing this task in C: An error has been detected due to incompatible operands in a binary expression involving a 'char [3]' and an 'int'

needed: * * * * * * * * * * * * * * * * * * * * * * * * * my solution //THIS CODE IS NOT WORKING #include<stdio.h> int main() { char arr[] = "* * * * *"; printf("%s\n", arr); for (int i=1; i<5; i ...

Stop node.js from automatically converting a nested object containing numeric keys into an array

Every time I send an object with a nested object containing keys that are numbers to my node.js server, the nested object gets converted into an array. Is there a way to prevent this from happening? Client: $.ajax({ url : `/cctool/report`, method ...

Display interactive form data on webpage post submission utilizing Knockout.js

I am in need of guidance on displaying the content of a form on the same page beneath it. Essentially, I need to fetch the value using document.getElementById() or a similar jQuery method. I am currently experimenting with some code samples I found onlin ...

Unraveling dependencies in deno for debugging purposes

When working with Node + NPM, dependencies are installed in node_modules, making it easy to debug by adding debugger statements or console logs directly in the node_modules/some-pkg/some-file.js. In Deno, things are a bit more complex as dependencies are ...

Dealing with null route parameters for Express applications

I am facing a challenge in handling an empty route parameter when a path is not specified. My intention is to return a new date if the route parameter is empty. However, the server's response so far is: Cannot GET /api/timestamp/ app.get("/api/timest ...

Retrieving specific object properties within an Angular 2 and Ionic 2 form

Within the @Page, I have a few select inputs. In addition to storing the value of the selected option, such as {{project.title}}, I also require the ability to return another selected object property, such as {{project.id}} or even the entire object. When ...

oidc-client-js failing to display all profile claims that are supported by oidc-client-js

When setting up UserManager on the oidc-client-ts TypeScript module using the config object below: var config = { authority: "https://localhost:3000", client_id: "js", redirect_uri: "https://localhost:3001/callback.ht ...

Using jQuery to encapsulate HTML within a div tag

I am looking to insert some HTML markup into my webpage using jQuery. I want to wrap the highlighted yellow code below with <div class="section">...</div>. Here is an example of what I need: <div class="section"> <div>...< ...

Trouble with JavaScript loading in HTML webpage

<!DOCTYPE html> <html> <head> <title>Breakout Game</title> <link rel="stylesheet" href="css/style.css"> </head> <body> <canvas width="900" height="450" class="canvas"></canvas> ...

Is it possible to call a JavaScript file located inside a Maven dependency's jar?

In the process of developing a Spring MVC web application using Apache Tiles, I have incorporated JavaScript libraries such as JQuery by including them in the pom.xml file as dependencies. My query pertains to whether I can access these scripts from within ...

I'm seeking assistance in identifying the issue with my form validation code. Could anyone lend a hand?

<!DOCTYPE html> <html lang="en"> <head> <link rel="stylesheet" href="./css/createanaccount.css"> <script src="https://kit.fontawesome.com/c90e5c3147.js" crossorigin=&quo ...

Substitute names and make copies in a bash associative array

My bash script is designed to determine if the input date ($1) falls within a specified date range. The user must provide a date as well as specify whether to check against range a or b ($2). #!/usr/bin/env bash today=$(date +"%Y%M%d") declare -A dict=$2 ...

Unable to establish a connection with the TCP server on CloudFoundry, although the localhost node.js is functioning properly

I am experiencing difficulty connecting to my TCP server example that is running on CloudFoundry. Interestingly, when I run my app.js file on a local node.js installation, everything works perfectly. Upon using vmc push to deploy on CloudFoundry, the servi ...

Is there a way for me to determine when AJAX-loaded content has completely loaded all of its images?

After making an AJAX request to load HTML, it includes img tags within it. I am in need of a way to detect when these images have finished loading so that I can adjust the container size accordingly. Since I do not know how many images will be present in ...