[Javascript] Golden Choice Error Help

Currently I am on the level “Golden Choice.” I had a bit of trouble figuring it out so I went for a more brute-force approach since I struggled to find an elegant solution. The entire code I have is very long so I’m not going to post it all here but I am having trouble with this part:

function row2(coins){
    var highest = 0; 
    for(var i = 0; i < coins.length; i ++){
        var coin = coins[i];
        if(coin.value > highest){
            highest = coin;
        }
    }
    return highest;
}

It’s telling me "TypeError: Tried to load an unsafe native value into the interpreter:object / Gold Coin 11 in reference to the line

if(coin.value > highest){

I would appreciate it if someone would tell me what this error is or how I can fix it :smiley:

1 Like

Hello and welcome to the forum. Can you please post a link to the level

That should be the link to the level. I do CodeCombat through an external organization and access the levels from their site, so I don’t know the Javascript version of this level

I think @xython might be able to help. He know javascript very well.

Can you show the entire code? I am not good with Java, but the variable or array named I could have something to do with it

Hi valoronions,

I don’t see anything wrong with your function, which suggests that it’s to do with the element that you’re putting in. Are you able to post the bit of code that’s using the function (I appreciate you not posting everything - my code for this level was ~150 lines :grinning:).

There’s a topic here where someone had the same error - it’s in python, but it might give you some clues about what’s happening.

Jenny

This is wrong:

        if(coin.value > highest){
            highest = coin; // 
        }

Possible code for searching the highest coin value and the coin with the highest value:

var coins = [{value:1, pos:{x:1, y:1}},
             {value:2, pos:{x:2, y:2}},
             {value:3, pos:{x:3, y:3}},
             {value:4, pos:{x:4, y:4}},
             {value:5, pos:{x:5, y:5}}            
            ];

function getHighestCoinValue(coins){
    var highestCoinValue = 0;
    for(var i = 0; i < coins.length; i ++){
        var coin = coins[i];
        if(coin.value > highestCoinValue){
            highestCoinValue = coin.value;
        }
    }
    return highestCoinValue;
}    

function geCoinWithHighestValue(coins){
    var highestCoinValue = 0;
    var coinWithHighestValue = null;
    for(var i = 0; i < coins.length; i ++){
        var coin = coins[i];
        if(coin.value > highestCoinValue){
            highestCoinValue = coin.value;
            coinWithHighestValue = coin
        }
    }
    return coinWithHighestValue;
} 

console.log(getHighestCoinValue(coins));
console.log(geCoinWithHighestValue(coins));

use pythontutor to paste the code and see it working step by step

your original function creates something that doesn’t work in codecombat and that I cannot exactly understand what is going on …

function getCoin(coins){
    var highest  = 0;
    for(var i = 0; i < coins.length; i ++){
        var coin = coins[i];
        if(coin.value > highest){
            highest  = coin; // assigning an object to a number var 
                             // and then comparing the object to a number var
        }
    }
    return highest ;
}  
console.log(getCoin(coins));

/////////////////////////////////// Edit //////////////////////////////////////////////////////////////
Your idea can work if you create an object with a value field:

// Test data
var coins = [{value:1, pos : {x: 1, y:1}},
             {value:2, pos : {x: 2, y:2}}, 
             {value:5, pos : {x: 5, y:5}},             
             {value:3, pos : {x: 3, y:3}},
             {value:4, pos : {x: 4, y:4}}
            
            ];
// code
function getCoin(coins){
    var highest  = {};
    highest.value = 0;
    for(var i = 0; i < coins.length; i ++){
        var coin = coins[i];
        if(coin.value > highest.value){
            highest  = coin;
        }
    }
    return highest ;
}    

var highestCoin = getCoin(coins);
console.log(highestCoin);

Visualization:

1 Like

Thank you! This really helped :smile:

1 Like