Javascript Uncaught ReferenceError: costs is not defined

Hi guys, I have a issue with my javascript code. It’s an exercise from a book that I’m studying and I keep getting the error “Uncaught ReferenceError: costs is not defined” at the end of the code (I use the console.log on Chrome).

I copied the code from the book and it looks identical but for some reasons it still won’t work.

This is the code:

<script>

var scores = [60, 50, 60, 58, 54, 54, 58, 50, 52, 54, 48, 69, 34, 55, 51, 52, 44, 51, 69, 64, 66, 55, 52, 61, 46, 31, 57, 52, 44, 18,
41, 53, 55, 61, 51, 44];

function printAndGetHighScore(scores) {
    var highScore = 0;
    var output;

    for (var i = 0; i < scores.length; i++) {
        output = "Bubble solution #" + i + " score: " + scores[i]; 
        console.log(output);
        if (scores[i] > highScore) {
           highScore = scores[i];
        }
    }
    return highScore;
}

function getBestResults(scores, highScore) {
    var bestSolutions = [];

    for (var i = 0; i < scores.length; i++) {
        if (scores[i] == highScore) {
        bestSolutions.push(i);
        }
    }
    return bestSolutions;
}

function getMostCostEffectiveSolution(scores, costs, highScore) {
    var cost = 100;
    var index;

    for (var i = 0; i < scores.lenght; i++); {
      if(score[i] == highScore) {
            if(cost > costs[i]) {
              index = i;
              cost = costs[i];    
            }
        }
    }
    return index;
}

var mostCostEffective = getMostCostEffectiveSolution(scores,costs,highScore); 
console.log("Bubble Solution #" + mostCostEffective + " is the most cost effective");

var highScore = printAndGetHighScore(scores);
console.log("Bubbles tests: " + scores.length); 
console.log("Highest bubble score: " + highScore);

var bestSolutions = getBestResults(scores,highScore);
console.log("Solutions with the highest score: " + bestSolutions);

</script>

variable costs is not defined friend.

1 Like

Hey there,

nice to meet you! :wave:

As Saruul already mentioned, you there is no variable with the name costs.

If you show us your code, we can have a look at it.

Hey there,

sorry for the problems,

I just saw that you pasted code, but it wasn’t visible.

I’ve edited your post so that we can see it.

You can use the “preformatted text” tool in the editor ( </> ) to add code.

So your function looks like this:

function getMostCostEffectiveSolution(scores, costs, highScore) {...}

You run it here:

var mostCostEffective = getMostCostEffectiveSolution(scores, costs, highScore);

But there is no costs variable you can pass to the function.

That’s right…I forgot to add the var costs. Thanks a lot!

One last thing…after I enter the new code with the var costs, the console.log gives me “Bubble Solution #undefined is the most cost effective”.

Is there anything wrong with the function?

var costs = [.25, .27, .25, .25, .25, .25, .33, .31, .25, .29, .27, .22, .31, .25, .25, .33, .21, .25, .25, .25, .28, .25, .24, .22, .20, .25, .30, .25, .24, .25,
.25, .25, .27, .25, .26, .29];

function getMostCostEffectiveSolution(scores, costs, highScore) {
    var cost = 100;
    var index;

    for (var i = 0; i < scores.lenght; i++); {
      if(scores[i] == highScore) {
            if(cost > costs[i]) {
              index = i;
              cost = costs[i];    
            }
        }
    }
    return index;
}

var mostCostEffective = getMostCostEffectiveSolution(scores, costs, highScore); 
console.log("Bubble Solution #" + mostCostEffective + " is the most cost effective");

Maybe this typo! :slightly_smiling_face:

1 Like

I tried to fix the typo but still says undefined. Thanks anyway

If cost > costs[i] is never true,
then index will never get set,
so your returned index will be undefined,
because of var index;