Print a new array, where all elements will be greater/less than a specific number

I changed my question using code, which I take from comment bellow.I added 2 conditions and have another one problem, I think the problem is in indents!Please check and say me if you see in my code mistake !

function filterNums (array, num, condition) {

var outputarray = [];
for (let i = 0; i < array.length; i = i + 1) {
  if (condition == "greater"){
    if (array[i] > num) {
      outputarray.push(array[i]); // put the array number in to the output array
    }
  } else if (condition == "less") {
    if (array[i] < num) {
      outputarray.push(array[i]); // put the array number in to the output array
    }
}   else if (condition!=='undefined'){
    if (array[i] > num){
        outputarray.push(array[i])
    }
    }   else if (num!=='undefined' && condition!=='undefined')
            if (array[i] > 0){
                outputarray.push(array[i])
    }

  
}

return outputarray;

}

console.log(filterNums([-3, 3, 4, 0, 44, -11, 5]));

The result of output must be : [ 3, 4, 44, 5 ] But I got []. What is the problem in my code? So in input data we don't have num and condition, just array.It must return all positive numbers from array.

Upvotes: 0

Views: 728

Answers (3)

Peter Seliger
Peter Seliger

Reputation: 13377

Everything that had to be said can be found in the comments which discuss the OP's approach. Therefore an implementation is the only thing left which still could be provided ...

// quote from the OP's first provided question / version ...
// "by default, the number is 0, and the last parameter is 'greater'"

function filterNumberValues(arr, val, condition) {

  // be failsave, sanitize the number value.
  val = Number(val);
  val = Number.isNaN(val) ? 0 : val;

  const lookupTable = {

    // discouraged non standard usage
    'GREATER': num => num > val,
    'LOWER': num => num < val,
    'LESS': num => num < val,

    // encouraged standard usage
    'GTE': num => num >= val,
    'GT': num => num > val,
    'EQ': num => num === val,
    'LT': num => num < val,
    'LTE' : num => num <= val,
  }
  // be failsave, sanitize and transform the filter condition.
  condition = String(condition).toUpperCase();
  condition = lookupTable[condition] || lookupTable['GT'];

  // use an array's native `filter` method.
  return arr.filter(condition);
}

console.log(
  filterNumberValues([-1, 2, 4, 0, 55, -12, 3], 4, 'greater')
); // [ 55 ]
console.log(
  filterNumberValues([-1, 2, 4, 0, 55, -12, 3], 4, 'gt')
); // [ 55]
console.log(
  filterNumberValues([-1, 2, 4, 0, 55, -12, 3], 4, 'gte')
); // [ 4, 55]
console.log('\n');

console.log(
  filterNumberValues([-2, 2, 3, 0, 43, -13, 6], 2, 'less')
); // [-2, 0, -13]
console.log(
  filterNumberValues([-2, 2, 3, 0, 43, -13, 6], 2, 'lt')
); // [-2, 0, -13]
console.log(
  filterNumberValues([-2, 2, 3, 0, 43, -13, 6], 2, 'lte')
); // [-2, 2, 0, -13]
console.log('\n');


console.log(
  filterNumberValues([-2, 2, 3, 0, 43, -13, 6], -13, 'lt')
); //  []
console.log(
  filterNumberValues([-2, 2, 3, 0, 43, -13, 6], -13, 'lte')
); //  [-13]
console.log(
  filterNumberValues([-2, 2, 3, 0, 43, -13, 6], -13, 'eq')
); //  [-13]
.as-console-wrapper { min-height: 100%!important; top: 0; }

In case of not being allowed to utilize the native filter method one could implement an own process via e.g. a while loop ...

// quote from the OP's first provided question / version ...
// "by default, the number is 0, and the last parameter is 'greater'"

function filterNumberValues(arr, val, condition) {

  // sanitize all arguments.
  arr = Array.isArray(arr) && arr || [];

  val = Number(val);
  val = Number.isNaN(val) ? 0 : val;

  condition = String(condition).toUpperCase();
  condition = (
    (condition === 'GREATER') ||
    (condition === 'LESS')
  ) && condition || 'GREATER';

  // pepare filter process.
  const result = [];
  let number;

  const len = arr.length;
  let idx = -1;

  // do filter.
  while (++idx < len) {
    if (idx in arr) { // skip "holes" in array.

      number = arr[idx];
      if (
        (condition === 'GREATER') 
        ? (number > val)
        : (number < val)
      ) {
        result.push(number);
      }
    }
  }
  return result;
}

console.log(
  filterNumberValues([-1, 2, 4, 0, 55, -12, 3], 4, 'greater')
); // [ 55 ]
console.log(
  filterNumberValues([-1, 2, 4, 0, 55, -12, 3], 54, 'GREATER')
); // [ 55]
console.log(
  filterNumberValues([-1, 2, 4, 0, 55, -12, 3], 55, 'greater')
); // []
console.log('\n');

console.log(
  filterNumberValues([-2, 2, 3, 0, 43, -13, 6], 2, 'less')
); // [-2, 0, -13]
console.log(
  filterNumberValues([-2, 2, 3, 0, 43, -13, 6], 0, 'LESS')
); // [-2, -13]
console.log(
  filterNumberValues([-2, 2, 3, 0, 43, -13, 6], -2, 'LESS')
); // [-13]
console.log(
  filterNumberValues([-2, 2, 3, 0, 43, -13, 6], -13, 'less')
); // []
.as-console-wrapper { min-height: 100%!important; top: 0; }

Upvotes: 0

Taha Halabi
Taha Halabi

Reputation: 171

I will write a simple Javascript basic code for this nice idea. You can use a built-in function like the previous answer (filter), but first try to make it without the help of built-in functions to gain more experience.

/*
 filterNums([-1, 2, 4, 0, 55, -12, 3], 11, 'greater');  //[ 55]
filterNums([-2, 2, 3, 0, 43, -13, 6], 6, 'less'); // [-2, 2, 3, 0, -13]
filterNums([-2, 2, 3, 0, 43, -13, 6], -33, 'less'); //  []*/

function filterNums (array, num, condition) {

  var outputarray = [];
  for (let i = 0; i < array.length; i = i + 1) {
  
    if (num != undefined) {
  
      if (condition == undefined || condition == "greater") {
        if (array[i] > num) {
          outputarray.push(array[i]); // put the array number in to the output array
        }
      } else if (condition == "less") {
        if (array[i] < num) {
          outputarray.push(array[i]); // put the array number in to the output array
        }
      }
    } else {
      if (array[i] > 0) {
        outputarray.push(array[i]); // put the positive array number in to the output array
      }
    }
  }
  
  return outputarray;
}

console.log(filterNums([-1, 2, 4, 0, 55, -12, 3], 11, 'greater'))
console.log(filterNums([-2, 2, 3, 0, 43, -13, 6], 6, 'less'))
console.log(filterNums([-2, 2, 3, 0, 43, -13, 6], -33, 'less'))

// New Edit
console.log(filterNums([-3, 3, 4, 0, 44, -11, 5]));

// New Edit
console.log(filterNums([-3, 3, 4, 0, 44, -11, 5], 9));

If you have any question, please comment.

Upvotes: 0

michael
michael

Reputation: 4173

You can implement this by using the Array.prototype.filter built-in function:

const filterNums = (arr, val, condition) =>
  arr.filter(item => (condition === 'greater' ? item > val : item < val));

console.log(filterNums([-1, 2, 4, 0, 55, -12, 3], 11, 'greater')); //[ 55]
console.log(filterNums([-2, 2, 3, 0, 43, -13, 6], 6, 'less')); // [-2, 2, 3, 0, -13]
console.log(filterNums([-2, 2, 3, 0, 43, -13, 6], -33, 'less')); //  []

Upvotes: 1

Related Questions