0

Why am I getting a type error Cannot read property '0' of undefined here? Also how can I have full control of whether I am passing things by reference or by value? I am not able to find how to do this anywhere? There must be some flip I have to switch to allow this. Having to remember whether it is passed by reference or value by default is getting really annoying.

function lineOfWines(wines) {
    let memo = new Array(wines.length)
    for (let i = 0; i < memo.length; i++) {
        memo[i] = new Array(wines.length).fill(null);
    }

    return lineOfWinesHelper(0, 0, wines, memo);
}

function lineOfWinesHelper(L, R, wines, memo) {

    if (L + R == wines.length) {
        return 0;
    }
    
    if (memo[L][R] !== null) {
        return memo[L][R];
    }

    

    sellLeft = (L + R + 1) * wines[L] + 
    lineOfWinesHelper(L + 1, R, wines, memo);

    sellRight = (L + R + 1) * wines[wines.length - 1 - R] + 
    lineOfWinesHelper(L, R + 1, wines, memo);

    memo[L][R] = Math.max(sellLeft, sellRight);

    return memo[L][R];
}

function main() {

    console.log(lineOfWines([1, 1]));
}

main();
  • `TypeError: memo[L] is undefined` is what I get in the snippet. So, I guess, the problem is - what is the value of `L`? – VLAZ Jan 08 '20 at 06:39
  • @VLAZ Can I not pass a value L + 1 as a function argument like that? – Joshua Anderson Jan 08 '20 at 06:40
  • You are trying to access `null` with `memo[L]` and `memo[L][R]` will try to access by index inside null which doesn't make any sense – Ramesh Reddy Jan 08 '20 at 06:41
  • @MiteshKumar you *can* pass it, however, you're going out of bounds because at one point you have `L = 2` and `memo` only has 2 items, so you get `memo[2]` which is `undefined` and try to get index `R` from that which fails. – VLAZ Jan 08 '20 at 06:44
  • How? IlineOfWines() I fill an n by n array with null. And then I only access it if that data is not null. – Joshua Anderson Jan 08 '20 at 06:44
  • Oh i'm sorry, I forgot to translate the entire code. I didn't think type error meant out of bounds. – Joshua Anderson Jan 08 '20 at 06:44
  • 1
    inside `lineOfWinesHelper` you do a recursive `lineOfWinesHelper(L + 1, ...` without any fool-guard against L + 1 becoming higher than wines.length. – Kaiido Jan 08 '20 at 06:45
  • @MiteshKumar you only check if `memo[L][R]` is `null` but the problem is with the check. I'm not going to debug the application for you - I'm telling you what the problem is. Walk through it with a debugger or put some print statements. I just did `console.log(L, memo)` and saw the problem - you get `L = 2` and `memo = [ [null, null], [null, null] ]`. Your logic is wrong somewhere when you recursively call the `lineOfWinesHelper` function but you should be in the best position to know why and how to fix it. – VLAZ Jan 08 '20 at 06:46
  • I forgot to copy one of my lines of code. I am still getting the same issue. – Joshua Anderson Jan 08 '20 at 06:50
  • VLAZ I don't think you understand what I am saying. If L + R = length, then that is the stopping condition. The exact same code works correctly in python. Nothing Is wrong with my logic here. There is a problem with how function parameters are treated in js that is different from python. I am just trying to learn this language. – Joshua Anderson Jan 08 '20 at 06:52
  • "*If L + R = length, then that is the stopping condition*" that code wasn't there before and it's what was causing the problem. The current code says `max is not defined`. Changing `max` for `Math.max` results in no errors being thrown. – VLAZ Jan 08 '20 at 06:55

0 Answers0