0

I need help with coding.

I need to filter 14 values from an array. The values are created dynamically. I want to compare values to 20.0 I only need two of the values to be higher than 20.0

I put my hopes into filter method as Switch did not work. Thank you in advance!

if (!Array.prototype.filter) {
    Array.prototype.filter = function (fun /*, thisp*/) {
        var len = this.length;

        if (typeof fun != "function")
            throw new TypeError();

        var res = new Array();
        var thisp = arguments[1];

        for (var i = 0; i < len; i++) {
            if (i in this) {
                var val = this[i]; // in case fun mutates this
                if (fun.call(thisp, val, i, this))
                    res.push(val);
            }
        }
        return res;
    };
}

function isBigEnough(element, index, array) {
    return (filtered >= 20.0);
}

var filtered = [
    (vol1l * 100).toFixed(1),
    (vol2l * 100).toFixed(1),
    (vol3l * 100).toFixed(1),
    (vol4l * 100).toFixed(1),
    (vol5l * 100).toFixed(1),
    (vol6l * 100).toFixed(1),
    (vol7l * 100).toFixed(1),
    (vol1r * 100).toFixed(1),
    (vol2r * 100).toFixed(1),
    (vol3r * 100).toFixed(1),
    (vol4r * 100).toFixed(1),
    (vol5r * 100).toFixed(1),
    (vol6r * 100).toFixed(1),
    (vol7r * 100).toFixed(1)
].filter(isBigEnough);

if (filtered) {
    testText.textContent = "JA! Gerät"
} else {
    testText.textContent = "NO! Nein"
}
Roman
  • 4,922
  • 3
  • 22
  • 31
MajaV
  • 1
  • 1
  • 1
    Not sure I understood what you want to archieve but I guess you want to check `element => 20.0` and not (the array) `filtered`... – Werner Oct 24 '17 at 13:09

2 Answers2

2

Why not just map the variables, get the adjusted values and filter it.

const
    factor100 = v => 100 * v,
    isBigEnough = v => v >= 20;

var filtered = [vol1l, vol2l, vol3l, vol4l, vol5l, vol6l, vol7l, vol1r, vol2r, vol3r, vol4r, vol5r, vol6r, vol7r]
        .map(factor100)
        .filter(isBigEnough);

This proposal works with built in prototypes of Array.

I suggest to use a better iterable data structure for using an array directly.

Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
1

The function isBigEnough try to check filtered with 20.0. This will allways return false because filtered is undefined

Code

function isBigEnough(element, index, array) {
    return (element >= 20.0);
}

Example

function isBigEnough(element, index, array) {
    return (element >= 20.0);
}

var filtered = [1.0.toFixed(1), 2.0.toFixed(1), 20.0.toFixed(1), 21.0.toFixed(1)].filter(isBigEnough)

console.log(filtered)

More Reusable Ways

Call isBigEnough in a other Funtion

var values = [1, 2, 10, 11, 20, 22].filter(biggerThan10)
var moreValues = values.filter(biggerThan20)

console.log(values, moreValues)

function isBigEnough(value, compareValue) {
    return (value >= compareValue);
}

function biggerThan10(value) {
    return isBigEnough(value, 10)
}

function biggerThan20(value) {
    return isBigEnough(value, 20)
}

Use Currying

var biggerThan10 = isBigEnough(10)
var biggerThan20 = isBigEnough(20)
var values = [1, 2, 10, 11, 20, 22].filter(biggerThan10)
var moreValues = values.filter(biggerThan20)

console.log(values, moreValues)

function isBigEnough(value, compareValue) {
    return (value >= compareValue);
}

function isBigEnough(compareValue) {
    return function(value) {
        return compareValue <= value
    }
}
Roman
  • 4,922
  • 3
  • 22
  • 31