skip to Main Content

Strange one here, I’ve got a variable racks_value which has it’s value altered on the page, depending on various calculations (yes it’s an integer and not a string). I’ve got a series of IF statements that updates the value of another variable, depending on the value of racks_value.

Now here’s the strange part – even though I’ve manually declared here in the example below that racks_value = 2 and had a console log verify this, after the IF statements have been run, the second console log shows racks_value to be 20, when it should still be 2. You can copy/paste the code below into your console of any page and it will behave the same way.

There must be something simple and obvious that I’ve missed here, but I really can’t see it, as the code looks so straightforward and simple.

var racks_value = 2;
console.log('racks value is ' + racks_value);

if (racks_value = 1){ var upfront_racks = 1000; }
if (racks_value = 2){ var upfront_racks = 2000; }
if (racks_value = 3){ var upfront_racks = 3000; }
if (racks_value = 4){ var upfront_racks = 4000; }
if (racks_value = 5){ var upfront_racks = 5000; }
if (racks_value = 6){ var upfront_racks = 6000; }
if (racks_value = 7){ var upfront_racks = 7000; }
if (racks_value = 8){ var upfront_racks = 8000; }
if (racks_value = 9){ var upfront_racks = 9000; }
if (racks_value = 10){ var upfront_racks = 10000; }
if (racks_value = 11){ var upfront_racks = 11000; }
if (racks_value = 12){ var upfront_racks = 12000; }
if (racks_value = 13){ var upfront_racks = 13000; }
if (racks_value = 14){ var upfront_racks = 14000; }
if (racks_value = 15){ var upfront_racks = 15000; }
if (racks_value = 16){ var upfront_racks = 16000; }
if (racks_value = 17){ var upfront_racks = 17000; }
if (racks_value = 18){ var upfront_racks = 18000; }
if (racks_value = 19){ var upfront_racks = 19000; }
if (racks_value = 20){ var upfront_racks = 20000; }
    
console.log('racks value is ' + racks_value);

I’ve tried changing the = to ===, but it doesn’t appear to help. If another new line for the IF statement series is added, the lower console log message will then return the new one.

2

Answers


  1. Please try changing the last line of the code to console.log('racks value is ' + upfront_racks); and this is going to tell you, if your code works. Also, change every if statememt line to the structure of this if (racks_value == x){ var upfront_racks = 1000 * x;}, but of course replace x with the value you want to check and change 1000 * x to the value you want to save in upfront_racks variable.

    Login or Signup to reply.
  2. I have copy pasted your code, it shows me ‘racks value is 20’.
    Reason is on your last line you are assigning "racks_value = 20". Which is correct output

    Issue is all your if conditions should use "==" (not "="). If you change your assignment code to == then the result will be ‘racks value is 2’

    after running the script

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search