0

I have 3 arrays of same length. I'd like to subtract the values in the first two arrays,without clicking a button, and if the answer is negative, i get the index of that array number, and display a hidden input field(in html) from the 3rd array, whose array index corresponds to the array index of the negative number.

For example:

//assuming this is the first array
array('10','2','3','4','5');
//and assuming this is the second array
array('9','1','4','3','7');

i'd like to subtract the values of the second from the first array, such that i have results, for example:

array('1','1','-1','1','-2');

the above results determines which input fields(from the 3rd array) are to be displayed. For example the input fields to be displayed are those whose array indexes are 2 and 4

here's my code for the 3rd array of input fields:

<?php while($roww = mysql_fetch_array($result)) { ?>
     <input type='text' name="reason[]" class="explanation">
<?php } ?>

is there any way i can make the above possible?

So far, this what i've tried, though i haven't applied arrays..just a slight update on a sample i found on the internet...

<html>
      <head>
            <script src="jquery.min.js"></script>
            <script>
                    $(document).ready(function(){
                     $(".explanation").hide();
                      var input1_ = document.getElementById('num1');
                      var input_ = document.getElementById('num2');
                      function sub(){
                            var answer = (parseInt(input1_.value) - parseInt(input2_.value));
                            if(answer < 0){
                                  $(".explanation").show();
                            }else $(".explanation").hide();
                       }
                       document.getElementById('num1').onkeyup = function () {
                            sub();
                    };
                      document.getElementById('num2').onkeyup = function () {
                            sub();
                   };
            });
</script>

</head>
<body>
     <input type='number'  id='num1' value="0">
     <input type='number'   id='num2' value="0">
     <input type='text' class='explanation' value="0">
</body>
</html>
2
  • 2
    Yes, it is possible. What have you tried? Commented Sep 30, 2016 at 13:04
  • i've updated the question, to show what i've done so far.. Commented Sep 30, 2016 at 13:29

6 Answers 6

2

var a = Array('10','2','3','4','5');
var b = Array('9','1','4','3','7');
var c = a.map(function(v,i) { return (v - b[i]); });
console.log(c);

Sign up to request clarification or add additional context in comments.

Comments

0

You can use a loop:

var result = [];
for (let i=0; i<arr1.length; i++) {
    result[i] = arr1[i]-arr2[i];
}

Comments

0

You may use map:

creates a new array with the results of calling a provided function on every element in this array.

The snippet:

var firstArr = ['10','2','3','4','5'];
var secondArr = ['9','1','4','3','7'];

//
// for each element in the first array
//
var result = firstArr.map(function(val, index) {
  
  // convert the current array value to number
  // subtract the value contained in the second array
  // and return the value
  return +val - +secondArr[index];
});


console.log(result);

Comments

0

As long as your arrays are the same length, this should work..

function subtrArrays(arr1[], arr2[])
{
    var final = [];
    for(var i = 0; i < arr1.length(); i++)
    {
        final[i] = arr1[i] - arr2[i];
    }
    return final;
}

Use:

var myArray = subtrArrays(array('10','2','3','4','5'),array('9','1','4','3','7'));

Comments

0

This is one of those cases which seems like Array.prototype.map() is the best fit but in fact it is not. If you consider the job as a whole Array.prototype.forEach() seems better since the return value of Array.prototype.map() is going to be useless for us. It's best to finalize the job in an iterating function which doesn't return anything.

var a = ['10','2','3','4','5'],
    b = ['9','1','4','3','7'],
 divs = [div1,div2,div3,div4,div5];
a.forEach((e,i) => e-b[i] < 0 && (divs[i].className = divs[i].className.replace(/\s*hidden\s*/,"")));
.hidden {display:none}
<div id="div1" class="hidden">div1 text</div>
<div id="div2" class="hidden">div2 text</div>
<div id="div3" class="hidden">div3 text</div>
<div id="div4" class="hidden">div4 text</div>
<div id="div5" class="hidden">div5 text</div>

Comments

0

You could use the forEach method. It applies a function to every element of an array. Let’s say subtract one from each element in the given array and push the result to a new array:

let newArr = []
const array = ['10','2','3','4','5']

array.forEach(element => {
  newArr.push(element - 1)
})
console.log(newArr)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.