JavaScript is so confusing to me


    operator[i].addEventListener('click', performOperation, false)

    if ([i].includes("-")){

        i.slice(0, -1)&& i.pop()

    }else if  

I want to take the second operator out if its a “-”
and then put my sting back in. but I’m lost. will anyone help?

[i] is an array that just contain i

what is it that you want to check instead?

for(let i=0; i < operator.length; i++) {
	operator[i].addEventListener('click', performOperation, false)
	if ([i].includes("-")){
		i.slice(0, -1)&& i.pop()
	}else if 

I didn’t get all of it.

To me this line doesn’t make sense as code will never go inside this if block. Because [i] will always be [0],[1],…[array.length] and this array will never include “-”.
Can we see the whole code?

1 Like

var decimal = document.getElementById('decimal');
var clear = document.getElementById('clear');
var backspace= document.getElementById('backspace');
var displayValElement = document.getElementById('display');

var displayVal = '0'; /*Simulates the zero on default*/
var pendingVal; /* Undefined. '5+3' then 3 would be displayed while 5 is pendingVal*/
var evalStringArray = []; /*Holds all values then can use eval()*/

/* Array by class*/
var number = document.getElementsByClassName('number');
var operator = document.getElementsByClassName('operator');

/* clickObj automatically passes click event to the function*/
var updateDisplayVal = (clickObj) => { 
	/* If '8' is clicked, then btnText = 8 */
	var Text = clickObj.target.innerText;
	/* Check display if 0, then clear it and add numbers to it*/
	if(displayVal === '0'){
		displayVal = '';
	}

	displayVal += Text;
	displayValElement.innerText = displayVal;
}
var performOperation = (clickObj) => {
	var operator = clickObj.target.innerText;
	switch(operator) {
		case '+':
			pendingVal = displayVal;
			displayVal = '0';
			displayValElement.innerText = displayVal;
			evalStringArray.push(pendingVal);
			evalStringArray.push('+');
			break;
		case '-':
			pendingVal = displayVal;
			displayVal = '0';
			displayValElement.innerText = displayVal;
			evalStringArray.push(pendingVal);
			evalStringArray.push('-');
			break;
		case 'x':
			pendingVal = displayVal;
			displayVal = '0';
			displayValElement.innerText = displayVal;
			evalStringArray.push(pendingVal);
			evalStringArray.push('*');
			break;
		case '÷':
			pendingVal = displayVal;
			displayVal = '0';
			displayValElement.innerText = displayVal;
			evalStringArray.push(pendingVal);
			evalStringArray.push('/');
			break;
		case '=':
			evalStringArray.push(displayVal);
			var evaluation = eval(evalStringArray.join(''));
			displayVal = evaluation + ''; /*As a string??*/
			displayValElement.innerText = displayVal;
			evalStringArray = [];
			break;
		}
}

/* Places click event on numbers that triggers updateDisplayVal()*/
for(var i=0; i < number.length; i++) {
	number[i].addEventListener('click', updateDisplayVal, false);
}
/* Click event for operators*/
for(let i=0; i < operator.length; i++) {
	operator[i].addEventListener('click', performOperation, false)
	if ([i].includes("-")){
		i.slice(0, -1)&& i.pop()
	}else if 

		
	
}

clear.onclick = () => {
	displayVal = '0'
	evalStringArray = [];
	displayValElement.innerHTML = displayVal;
}
backspace.onclick = () => {
	/* Take index of last string and remove it */
	let lengthOfDisplayVal = displayVal.length;
	/* Array starts at 0 */
	/* The end slice is not included in returned slice */
	displayVal = displayVal.slice(0,lengthOfDisplayVal - 1);
	/* Check if displayVal is empty*/
	if(displayVal === '') {
		displayVal = '0';
	}
	displayValElement.innerHTML = displayVal;
}
decimal.onclick = () => {
	/* not false is true */
	if(!displayVal.includes('.')){
		displayVal += '.';
	}
	displayValElement.innerHTML = displayVal;
}

what’s the thing you want to check if it contains -?

you need to apply the includes method on that, not on the [i] array

1 Like
User Story #13: If 2 or more operators are entered consecutively, the operation performed should be the last operator entered (excluding the negative (-) sign). For example, if 5 + * 7 = is entered, the result should be 35 (i.e. 5 * 7); if 5 * - 5 = is entered, the result should be -25 (i.e. 5 x (-5)).

This what i need to do. I’ve done it before I think it was react. I have tied this calculator four times i think. and all of them failed. i can’t get anything to work so maybe a glitch.

no, you are checking the wrong thing

you need to individuate what’s the element you want to check

you are using [i].includes('-'), but [i] will never include that. So what do you want to check instead?

The second operator.

I should be able to isolate. If there is a second operator then use the first or the first and use the second however it goes with slice.

also, i is a number
this line will not work

you may need to review how to work with arrays and loops

I know its don’t look right either. I suck. I just need a pointer.

Js is so hard to understand . I may just have to find more courses.

You might not be quite ready to tackle this challenge. If you go back and look at some of the JS lessons on FCC with your new knowledge and perspective from this project, I think it will really pay off!

1 Like

I am giving you the pointers…

  • review how a for loop works
  • review how to use a for loop to iterate over an array
  • review how to use array methods on arrays in a for loop
  • review what slice actually does
1 Like

I roughly skimmed through your code and I feel your whole logic is wrong. You might need to take a step back as others suggested and re learn some of the basic things listed by @ilenia . Then try re writing this from beginning and do not copy paste code that you do not understand how it works. You should be able to understand what each line of your code do. Good luck

I have done that. and it didn’t work or pass the test. Guys i have done all that and will do it again not a problem but I saw this yesterday Learn JavaScript by building 7 Games-full course.
and that way help. I’ve finished course here and in solo-learn and another place i can’t remember. I just need some hands on. So that has got to help.

The definition of insanity is doing the same thing over and over again and expecting a different result.
-Albert Einstein

Learning by building is great, but you really need to have a firm grasp on the logical building blocks, such as loops, arrays, and conditionals before you can build anything.

2 Likes

Type along coding doesn’t really do much for your learning unless you take what is shown an use it in your own project (and not just an exact clone).

Watching a video and just typing what the person does is really not an effective way to learn. It creates the illusion of having learned something, but in reality, you will find when you go and try to implement the idea again you will struggle (which is good) and only then will you start to really learn what was taught.

1 Like

My Github

So I’m doing this and I’m suck again I can’t even copy along and get it right. I need people to help me please I’m just ready to quiet. I really give up @RandellDawson I need you again just tell me what to do and i will do it.