 # Vanilla js and es6 - need help - RESOLVED

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

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.

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

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.

1 Like

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)
}``````

`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.

1 Like

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

1 Like