Ad

How To Split String, Convert To Numbers And Sum

I have a function that I have modified to get a string (which consists of zeros and ones only). The string (timesheetcoldata):

100000000000000000000000100000000000000000000000100000000000000000000000100000000000000000000000100000000000000000000000100000000000000000000000

The string items (the numbers one and zero) will change every time the function is run. It will always be the same length. I have made the string above easier to see what I am trying to achieve.

I want to return the first character and then every 24th character (as in the variable colsCount in the function). so, in the example above, it would return something like: 111111

I then want to convert these characters to numbers (something like [1, 1, 1, 1, 1, 1]).

I then want to sum these number together (so it would return, in the example: 6).

I then want to check if the returned number matches the variable: rowsCount or true if it does, false if it does not.

My function:

$("#J_timingSubmit").click(function(ev){

  var sheetStates = sheet.getSheetStates();
  var rowsCount = 6;
  var colsCount = 24;
  var timesheetrowsdata = "";
  var timesheetcoldata = "";

  for(var row= 0, rowStates=[]; row<rowsCount; ++row){
    rowStates = sheetStates[row];
    timesheetrowsdata += rowStates+(row==rowsCount-1?'':',');
  }

  timesheetcoldata = timesheetrowsdata.replace(/,/g, '');
  console.log(timesheetcoldata);
});

Thank you very much to both Rajesh and MauriceNino (and all other contributers).

With their code I was able to come up with the following working function:

$("#J_timingSubmit").click(function(ev){

  var sheetStates = sheet.getSheetStates();
  var rowsCount = 6;
  var timesheetrowsdata = "";
  var timesheetcoldata = "";

  for(var row= 0, rowStates=[]; row<rowsCount; ++row){
    rowStates = sheetStates[row];
    timesheetrowsdata += rowStates+(row==rowsCount-1?'':',');
  }

  timesheetcoldata = timesheetrowsdata.replace(/,/g, '');

var count = 0;
var list = [];
for(var i = 0; i< timesheetcoldata.length; i+=24) {
  const num1 = Number(timesheetcoldata.charAt(i));
  list.push(num1);
  count += num1;
}

let isSameAsRowsCount = count == rowsCount;
console.log('Is Same? ', isSameAsRowsCount);

});
Ad

Answer

Here is a step by step explanation, on what to do.

  • Use match() to get every nth char
  • Use map() to convert your array elements
  • Use reduce() to sum your array elements

Everything needed to say is included in code comments:

const testData = '100000000000000000000000100000000000000000000000100000000000000000000000100000000000000000000000100000000000000000000000100000000000000000000000';

// Step 1) Create array of numbers from string
const dataArr = testData.match(/.{1,24}/g) // Split on every 24th char
                      .map(s => Number(s[0])) // Only take the first char as a Number

console.log(dataArr);

// Step 2) Sum array Numbers
let dataSum = dataArr.reduce((a, b) => a + b); // Add up all numbers

console.log(dataSum);

// Step 3) Compare your variables
let rowsCount = 123; // Your Test variable
let isSameAsRowsCount = dataSum == rowsCount;

console.log('Is Same? ', isSameAsRowsCount);

Ad
source: stackoverflow.com
Ad