# \[Javascript\] Golden Choice Error Help

**URL:** <https://discourse.codecombat.com/t/javascript-golden-choice-error-help/27594>\
**Category:** Level Help\
**Created:** [February 23, 2021, 11:59pm UTC](https://discourse.codecombat.com/t/javascript-golden-choice-error-help/27594 "2021-02-23T23:59:58Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![valoronions](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/valoronions/32/20854_2.png) [@valoronions](https://discourse.codecombat.com/u/valoronions)\
**Post date:** [February 23, 2021, 11:59pm UTC](https://discourse.codecombat.com/t/javascript-golden-choice-error-help/27594/1 "2021-02-23T23:59:58Z")

</div>

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:

```python
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

```python
if(coin.value > highest){

```

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

---

<div class="post-metadata">

**Author:** ![milton.jinich](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/milton.jinich/32/21419_2.png) [@milton.jinich](https://discourse.codecombat.com/u/milton.jinich)\
**Post date:** [February 24, 2021, 12:17am UTC](https://discourse.codecombat.com/t/javascript-golden-choice-error-help/27594/2 "2021-02-24T00:17:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![valoronions](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/valoronions/32/20854_2.png) [@valoronions](https://discourse.codecombat.com/u/valoronions)\
**Post date:** [February 24, 2021, 12:22am UTC](https://discourse.codecombat.com/t/javascript-golden-choice-error-help/27594/3 "2021-02-24T00:22:38Z")

</div>

> **[CodeCombat - Coding games to learn Python and JavaScript](https://codecombat.com/play/level/golden-choice)**
>
> Learn typed code through a programming game. Learn Python, JavaScript, and HTML as you solve puzzles and learn to make your own coding games and websites.

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

---

<div class="post-metadata">

**Author:** ![milton.jinich](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/milton.jinich/32/21419_2.png) [@milton.jinich](https://discourse.codecombat.com/u/milton.jinich)\
**Post date:** [February 24, 2021, 12:27am UTC](https://discourse.codecombat.com/t/javascript-golden-choice-error-help/27594/4 "2021-02-24T00:27:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![lukas31](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/lukas31/32/22892_2.png) [@lukas31](https://discourse.codecombat.com/u/lukas31)\
**Post date:** [February 24, 2021, 5:52am UTC](https://discourse.codecombat.com/t/javascript-golden-choice-error-help/27594/5 "2021-02-24T05:52:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jka2706](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/jka2706/32/13636_2.png) [@jka2706](https://discourse.codecombat.com/u/jka2706)\
**Post date:** [February 24, 2021, 8:47am UTC](https://discourse.codecombat.com/t/javascript-golden-choice-error-help/27594/6 "2021-02-24T08:47:19Z")

</div>

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 😀).

There’s a topic [here](https://discourse.codecombat.com/t/solved-typeerror-tried-to-load-an-unsafe-native-value-into-the-interpreter-object/14986) where someone had the same error - it’s in python, but it might give you some clues about what’s happening.

Jenny

---

<div class="post-metadata">

**Author:** ![xython](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/xython/32/7269_2.png) [@xython](https://discourse.codecombat.com/u/xython)\
**Post date:** [February 24, 2021, 9:05pm UTC](https://discourse.codecombat.com/t/javascript-golden-choice-error-help/27594/7 "2021-02-24T21:05:39Z")

</div>

This is wrong:

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

```

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

```python
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](http://www.pythontutor.com/javascript.html#mode=edit) to paste the code and see it working step by step

 ![image](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/3X/c/4/c44981824eef0e2fbff53b37fde9ac14eb25039e.png)

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

```python
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:

```python
// 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:

 ![image](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/3X/1/9/19feb9e6c2c037e99dd8b45d0fcb226e357bbbab.png)

---

<div class="post-metadata">

**Author:** ![valoronions](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/valoronions/32/20854_2.png) [@valoronions](https://discourse.codecombat.com/u/valoronions)\
**Post date:** [February 25, 2021, 8:15pm UTC](https://discourse.codecombat.com/t/javascript-golden-choice-error-help/27594/8 "2021-02-25T20:15:24Z")

</div>

Thank you! This really helped 😄
