PandaDeTapas
PandaDeTapas

Reputation: 516

How to make it impossible to change variables throught browser console

I am writing a program where one is supposed to answer questions. The number of correct answers is held by a variable called:

correctAnswers

But as the case is now, I can change the value of

correctAnswers

by simply typing in

correctAnswers = (new Value)

in the console. I don't want this to be possible, is there a way I can disable the possibility to alter a variables value through the console?

Upvotes: 1

Views: 612

Answers (2)

Hoyen
Hoyen

Reputation: 2519

Try using closure.

var Test = (function(){
    var correctAnswers=0;

    return {
        getCorrectAnswers: function(){ return correctAnswers; }
    }
})();

// Enter the following in console
correctAnswers = 2;
Test.getCorrectAnswers(); // will always return 0;

Upvotes: 2

yotamN
yotamN

Reputation: 771

There is no possible way to stop someone from changing the variable value, you can only make it harder. For start, put the variable in function so you can't access it outside the function like this: (function(){}());

Another option you have is to encrypt the code but I'm not sure how much it's effective, never tried that. Any way all these options just make it harder but not impossible, you always need to confirm the data in the server-side.

TL;DR: You can't do it on the client side.

Upvotes: 2

Related Questions