Home > Mobile >  Get the sum of the first index value of an array and array's length using javascript
Get the sum of the first index value of an array and array's length using javascript

Time:12-29

"Given an array, print or log the sum of the first value in the array, plus the array’s length. Assume that the array is composed of numbers."

I have already formulated a javascript, my goal is to iterate on each dataArray and print the sum of each dataArray. The current script only outputs the sum of the dataArray1. Hope anyone can help on this. Thanks in advance!

// Enter code below
var sum =0;
var numList = [];

var dataArray1=[1,2,5];  // to print 4
var dataArray2=[3,0,2,5];// to print 7
var dataArray3=[-5,0,2,5];// to print -1
var dataArray4=[1]; // to print 2

for (let i=0; i<=4; i  ){
if (!numList.length){
  numList.push.apply(numList, dataArray1);
var listLength = numList.length;
var firstNum = numList[0];
}
sum = listLength   firstNum;
}
console.log(sum);
//the expected log/print should be 4, 7, -1, 2

CodePudding user response:

You can create a function to make the sum between first item and length then append the resulted number to your array

// Enter code below
let numList = [];

const dataArray1 = [1, 2, 5]; // to print 4
const dataArray2 = [3, 0, 2, 5]; // to print 7
const dataArray3 = [-5, 0, 2, 5]; // to print -1
const dataArray4 = [1]; // to print 2

// create a function and insert in numList the sum resulted
function returnSum(arr) {
  const listLength = arr.length;
  const firstNum = arr[0];

  let sum = listLength   firstNum;
  numList.push(sum);

  return sum;
}

// run the function for each array
returnSum(dataArray1);
returnSum(dataArray2);
returnSum(dataArray3);
returnSum(dataArray4);

console.log(numList)

EDIT

Based on your comment " I was planning to log the sum separately and not on a single array"

// Enter code below
let numList = [];

const dataArray1 = [1, 2, 5]; // to print 4
const dataArray2 = [3, 0, 2, 5]; // to print 7
const dataArray3 = [-5, 0, 2, 5]; // to print -1
const dataArray4 = [1]; // to print 2

// create a function and return the sum
function returnSum(arr) {
  const listLength = arr.length;
  const firstNum = arr[0];

  return listLength   firstNum;
}

// run the function for each array
console.log(returnSum(dataArray1));
console.log(returnSum(dataArray2));
console.log(returnSum(dataArray3));
console.log(returnSum(dataArray4));

CodePudding user response:

Try this:

var dataArray1=[1,2,5];  // to print 4
var dataArray2=[3,0,2,5];// to print 7
var dataArray3=[-5,0,2,5];// to print -1
var dataArray4=[1]; // to print 2

let process=[dataArray1,dataArray2,dataArray3,dataArray4]

for (let i=0; i<process.length; i  )
    console.log(process[i][0] process[i].length);

OR

var dataArray1=[1,2,5];  // to print 4
var dataArray2=[3,0,2,5];// to print 7
var dataArray3=[-5,0,2,5];// to print -1
var dataArray4=[1]; // to print 2


console.log([dataArray1,dataArray2,dataArray3,dataArray4].map((arr) =>console.log(arr[0] arr.length)));

CodePudding user response:

function sum(arr) {
  return arr[0]   arr.length;
}

Then, you can pass in any array you like to this function.

CodePudding user response:

Tricky way, for browsers:

var dataArray1 = [1, 2, 5]; // to print 4
var dataArray2 = [3, 0, 2, 5]; // to print 7
var dataArray3 = [-5, 0, 2, 5]; // to print -1
var dataArray4 = [1]; // to print 2

function trickySum(prefix) {
  var sum = 0;
  var numList = [];
  var i = 1;

  while (window[`${prefix}${i}`]) {
    numList = window[`${prefix}${i}`];
    i  ;
    var listLength = numList.length;
    var firstNum = numList[0];
    sum = listLength   firstNum;
    console.log(sum);
  }
}

trickySum('dataArray');

  • Related