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"
}
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... Commented Oct 24, 2017 at 13:09

2 Answers 2

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.

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

2 Comments

this will iterate the array twice and doesn't reference isBigEnough which would be more reusable
@Roman, right, but i think it is more the problem to rewriting filter.
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
    }
}

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.