Vanilla js and es6 - need help - RESOLVED

Vanilla js and es6 - need help - RESOLVED
0

#1

Hi,
I’m stuck to calcul the result:
All the number and sign are in an array: arr=[]
I’m trying to have the result when I click on the class ".equal"

Thanks


#2

So arrNum is supposed to be only the numbers? Why?

Set result = first number, then loop through your array. If you find a sign, keep track of it in a variable defined outside of the loop. If you find a number, add/subtract/etc to result based on previous sign until the loop is finished.

This will have a few bugs as is, probably better fixed by managing your input logic than in the equals function, though.


#3

in the loop the sign are not number and I tried to add all number together,
arr = ['3', '+', '3', '4'] to arrNum = ['3', '+', '34']

// value current it's a sign: +-/*x  
// looking for the sign with arrSign
if (arrSign.indexOf(v) >= 0 && v !== 'undefined') {
    arrNum.push(v)
  }  else if (arrSign.indexOf(arr[i-1]) === -1 && arr[i-1] !== 'undefined') {// else previous value is a number   
    let n= arr[i]
    arrNum.push( arr[i-1]+n )
  } else if (arrSign.indexOf(arr[i-1]) !== -1) {// previous value is a sign
    arrNum.push(v)
  }

your idea is to set in the loop the first variable, then to compare this variable to the current variable in the next iteration?

What do you mean by fix the logic?

Thanks


#4

Oh, I see. That function is not working–though I can’t see what’s going wrong at a glance. You could do it by joining the array into a string then splitting out the operators:

str=arr.join('');
numArr=str.split(/[^[0-9]/); //splits at any character not(^)
                              //between 0-9 ([0-9])

But then if you join('') you could also loop through string and operate whenever the character at index matches an operator.

EDIT: Note that this gives you back the numbers as strings, you may need to parse them with Number(str), don’t remember off the top of my head.

All I meant by ‘fix the logic’ was setting up your input so, for example, the user couldn’t enter +*- or 9.40.266 or anything else that doesn’t make sense.


#5

I just did the same than your example with another array, and I used arrSign who’s contain ‘-+x/’, I will
need this array later to compare the sign(maybe), but I aggree with the regex it’s nicer solution and faster.
In both code my fist value is always start by undifened ?

I do 9x65 and it’s return in the console
["undefined9", "x", "65"]

let str=arr.join('');
let numArr=str.split(/[^[0-9]/); //splits at any character not(^)
                              //between 0-9 ([0-9])

  if (arr.length === 1 && typeof v !== 'undefined') {
    //console.log(0);
    arrSort.push(v)
  } else if(arrSign.indexOf(v) >= 0 && typeof v !== 'undefined'){
    arrSort.push(v)
    //console.log(1);
  } else if(arrSign.indexOf(v) === -1 
    && arrSign.indexOf(arr[i-1]) === -1
    && typeof v !== 'undefined'){
    //console.log(2);
    arrSort.push(arr[i-1] =''+ arr[i-1] + v)
  }

#6

arrSort.push(arr[i-1] =''+ arr[i-1] + v)

When i is 0, you are asking for arr[-1], which will return undefined because 0 is the first index.


#7

I did with match and it works fine:

arr=[ '6', '6', 'x', '3', '3' '/', '5' ]
strSign   = arr.join('')
//return an array with the number together
arrSort = strSign.match(/(\d+)|\D/g); 
//"66", "x", "33", "/", "5"]

EDIT: the result is weird, I never except the concatenation with the numbers?
Someone can explain why the pattern return this result?

Thanks