JSFiddle - React, Tailwind, and code Playground

by Stepan Parunashvili

JavaScript

// hello

// matrix 
// spits out the sum of all the elements inside the matrix

function matrixSearch(mtx) {
  return mtx.reduce(
    (total, row) => {
      return row.reduce((acc, v) => acc + v, total);
    },
    0
  );
}

const mtx = [[1, 2], [3, 4]];

console.log(matrixSearch(mtx));

['[', '{', '(']

// stack of all the open parens

// string

// go through the string

// if we see an opening paren
  // add it into the stack

/*
  [ { ] } 
    
    stack
      [
      {
    
    closing 
      ]
    
    pop 
      [
    
    return false
  
  [ { } ] [
    
  [ { } ]  
*/





const openToClose = {
  '{': '}',
  '[': ']',
  '(': ')',
}

function invertObj(x) {
  return Object.keys(x).reduce(
    (res, k) => {
      res[x[k]] = k;
      return res;
    },
    {}
  );
}
const closeToOpen = invertObj(openToClose);

console.log(closeToOpen);

// [[{}]]
// [[{}]] [
// [[{]}]

function checkValidFn(fnStr) {
  const stack = [];
  for (let i = 0; i < fnStr.length; i++) {
    const char = fnStr[i];
    if (openToClose[char]) {
      stack.push(char);
    } else if (closeToOpen[char]) {
      const expected = closeToOpen[char]
      const lastOpen = stack.pop();
      if (lastOpen !== expected) {
        return false;
      }
    }
  }
  return !stack.length;
}

console.log(
  'expect true', checkValidFn('[[]]'),
  'expect false', checkValidFn('[[]}'),
  'expect false', checkValidFn('[[]][')
);


function sumMatrix(matrix) {
    let sum = 0;
    for (var i = 0; i < matrix.length; i++) {
        var currentRow = matrix[i];
        
        for (var j = 0; j < currentRow.length; j++) {
            sum += currentRow[j];
        }
    }

    return sum;
}


function sumMatrixBroken(matrix) {
    let sum = 0;
    for (let i = 0; i < matrix.length; i++) {
        var currentRow = matrix[i];
        for (let i = 0; i < currentRow.length; i++) {
            sum += currentRow[i];
        }
    }
    return sum;
}

const mtx1 = [[1, 2], [3,...