The array is one-dimensional, the numbers are entered randomly in a certain range.

  1. An array of integers is given. Find out whether the pth element of the array is positive.

    I don’t know how to request processing of a specific (indexed element) from the keyboard to check for positivity at the moment it displays all the positive numbers in the array.

    <form name="numbers"> <input type="text" name="age"> <input type="button" value="Расчет" onclick="calculate( );"> </form> <script language="JavaScript"> function calculate( ) { n=document.numbers.age.value; //n=Number(n); var a = new Array(n); //var a = []; for (i=0; i<n; i++){ a[i] = Math.round(Math.random()*120-20); document.write(a[i]+"<br>"); } function condition(value, index, array) { var result = false; if (value >0) { result = true; } //else { //alert('Error'); //} return result; }; var filteredA = a.filter(condition); for(var i=0; i < filteredA.length; i++) document.write(' POlozh Chisla Massiva A ->'+filteredA[i] + "<br/>"); } </script> 
  2. An array of integers is given. Print all even items. The Russian-language text loops in response, generally works, but the answer does not like looping. 'Chrtnie Chisla Massiva A ->' writes next to each number, you need to find 1 time a below even numbers. I'm missing out on how to fix it.

     <form name="numbers"> <input type="text" name="age"> <input type="button" value="Расчет" onclick="calculate( );"> </form> <script language="JavaScript"> function calculate( ) { n=document.numbers.age.value; //n=Number(n); var a = new Array(n); //var a = []; for (i=0; i<n; i++){ a[i] = Math.round(Math.random()*120-20); document.write(a[i]+"<br>"); } function condition(value, index, array) { var result = false; if (value %2 == 0) { result = true; } //else { //alert('Error'); //} return result; }; var filteredA = a.filter(condition); for(var i=0; i < filteredA.length; i++) document.write(' Chrtnie Chisla Massiva A ->'+filteredA[i] + "<br/>"); } </script> 

Closed due to the fact that the question is too general for participants Dmitriy Simushev , cheops , aleksandr barakin , Denis , αλεχολυτ 25 Sep '16 at 19:05 .

Please correct the question so that it describes the specific problem with sufficient detail to determine the appropriate answer. Do not ask a few questions at once. See “How to ask a good question?” For clarification. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • four
    here do not solve the problem for you. Show what you did yourself. - Alexey Prokopenko
  • I vote for closing this question as removed by the author - cheops

1 answer 1

You are welcome.

 var arr = [-1, 3, -5, 6, 7, -10]; for (var i = 0; i < arr.length; i++) if (arr[i] >= 0) alert('Положительное число: ' + arr[i] + ', индекс ' + i); arr = [1, 3, 7, 8, 9, 11, 12]; for (i = 0; i < arr.length; i++) if (arr[i] % 2 == 0) alert('Четное число:' + arr[i] + ', индекс ' + i); 

To traverse an array, use a for loop.

More about cycles - https://learn.javascript.ru/while-for

More methods for iterating over arrays - https://learn.javascript.ru/array-iteration

Operator% - returns the value of the remainder in division. Those. if the remainder when divided by 2 is 0, then the number is even.

More about operators - http://javascript.ru/arithmetic

  • Thanks, it is clear that in comparison to do and mathematical operators. It is not clear how to make it from a manual entry of a specific element to make a check for positivity. - snmplhfcnb