How to check for this condition?

I need to check if for every number of x y[i] - number of x is less than zero. So in the example below it should return false, since 2 - 1 > 0 but it’s returning true. How should the conditional statement be worded?

let x = [1]
let y = [0, 0, 2]
function test(x, y) {
for (let n of x) {
    for (let i = 0; i < y.length; i++) {
        if (x.every(n => y[i] - n < 0)) {
            return true
        } else {
            return false
        }
    }
}
}

console.log(test(x, y))

Could you clarify what is the problem you’re trying to solve? Is there a link to a challenge or the text of the question you’re trying to answer? What is xy[i]? Is it a two dimensional array(or matrix)?

No, all I’m trying to check for is whether after substracting x[0] from every number of y would be less than zero. Which it isn’t since one of the numbers in y is 2. But the conditional statement is only checking for the first number of y

Oh never mind i solved it, needed to put y.every instead

The problem is you return prematurely. You need to find the exact moment in the loop you need to return true/false. A return inside a loop, would quit the loop and return a value from the function directly, stoping the function execution as well. You code is written so true is returned as soon as you enter the loops. For your condition to saticfy, you need to veryfy all elements answer it. If any of them is wrong(falsy), it should return false immediately.

here is an optional solution without using loops, since i see every method is in the scope. It would be more fun to do it all loops however and not use advanced array methods, which is a challenge id go for, if i were you.

let x = [1]
let y = [0, 0, 2]

console.log(x.every(n => y.every(num => num - n < 0)))

The thing is for each value of x the index which i want to start checking at is different. Here’s the full code Edit fiddle - JSFiddle - Code Playground
4 of change should start checking for the condition at index 4 of register, 0.5 of change should start checking at index 5 of register, 0.05 of change should start checking at index 7 of register . How do I achieve this?

Well that depends if you want to use every, or loops, or another approach…
Array methods do provide a second parameter, which represents the current element index. If i were to write x.every((num, index) => ..., num would represent the current element and index its index. If i say anotherArr[index] it will give me the element on the same index on that other array, if we were to make a connection between the same indices between the “change” and “idx” arrays from your example. We can also slice a piece of array, if we want only to look within the “register” array, from index 4, we can say register.slice(4). We then have the elements which we narrow our search and can work directly with them. But again, this could also be achieved with some more basic conditions and comparisons using regular loops.

What do you think about this solution using slice()? Edit fiddle - JSFiddle - Code Playground

dont use for...of if you require the index. Use regular for loop instead. You could also use for...in to work with the indices instead, but i just read in the docs, it can be inconsistent. array.indexOf(element) is another method i wouldnt use in your case. indexOf will return the first place the element is encountered in the array. If you had two identical elements in the same array, it would always return the index of the first. Also, if you use the value returned from indexOf(or any other function) twice(or more times), its better to store it in a variable and reuse that instead.
reg.slice(idx[ch.indexOf(x)] this is also not a good practice. Its not nice to insert such calculations in spot where simple value is required. Better make the calculations elsewhere, name them with well descriptive name and put on the place, to be easily understood. For example:

const chIndex = ch.indexOf(x)
reg.slice(idx[chIndex])
// or ...
const sliceIndex = idx[ch.indexOf(x)]
reg.slice(sliceIndex)

It just makes things easier for you and anyone reviewing your code, understand what its meant to do and debug if needed. I didnt look if your code actually works, and if do, thats alright, but its nice to keep those details in consideration.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.