NietzscheProgrammer NietzscheProgrammer - 6 months ago 23
Javascript Question

Clear inputs parting from index

I have an array

betSlip
, where I am doing some validation, lets say that the array has 4 indexes right now, every index has an
input
so I have 4 inputs, one per index.

Example: Values I type in the inputs:


1 input: 10

2 input: 10

3 input: 10

4 input: 10


The next input can not be greater than the previous.
Which in my app means that if I go to the 2nd input and type the number 1, the inputs 3 and 4 should be back to 0 again. But as I have it in my app so far, if you type 1 in the 2nd input, ONLY the 3rd input get back to 0 and not the others.

So, to be clear: If I have 10 inputs where I typed the number 100 in every single input, and I go to the 3rd input and type 99, then the rest of the inputs should be back to 0. Except the previous inputs.

I am using Angular and also Lodash.

I took a couple pictures

enter image description here

In this image I typed 1 in the 2nd input, and only the 3rd input is on 0, I need every single input back in 0, parting from the current index.

enter image description here

and here my code:

$scope.riskWinCalculations2 = function(slip, type, index) {
var previousSlip = $scope.betSlip[index - 1],
currentSlip = $scope.betSlip[index],
nextSlip = $scope.betSlip[index + 1];


if (currentSlip.risk.length && nextSlip != undefined) {
if (currentSlip.risk < nextSlip.risk) {
nextSlip.risk = 0;
nextSlip.win = 0;
}
}
}


I know my mistake is here in this var
nextSlip
, where all I am doing is tell the app to put an empty string in the next input if the current one is a less amount. But that is why I am here, I need to know how to solve it, taking into account all the next inputs and not only the right next input.

Also, I made a video

UPDATE TO ADD MORE EXPLANATION

if every input has 10 as a value, and you have 5 inputs, it means:


1 input: 10

2 input: 10

3 input: 10

4 input: 10

5 input: 10


if you go to the 3rd input and type 5, it means:


1 input: 10

2 input: 10

3 input: 5

4 input: 0

5 input: 0


so, as you see the other inputs get back to 0 which is the initial value. But the previous inputs keeps the same.

Answer
      //if the user tries to play with the input:
      //he can not put lower amount in a previous input,
      //when the next input is already filled out
      _.each($scope.betSlip, function(slip, indexSlip) {
        if (indexSlip > index) {
          slip.risk = '';
          slip.win = '';
        }
      });
Comments