-2

I am struggling here. My JS is below. The objective here - user selects type for course - WS or AC. If Onsite is checked update the AC value to be 1.5 under course weight.

Number of people has differing values depending if AC or WS was selected. Hence in Calculate Total function there is an if statement reviewing the course type value - AC or WS.

JSHint is reporting on Line 257 - Expected a conditional expression and instead saw an assignment.

                 //Set up an associative array
                 //The keys represent the size / weightof the course type
                 //The values represent the weight of the course type
                 var course_weight = [];
                 course_weight["AC"]=1;
                 course_weight["WS"]=1;

                 //Set up an associative array
                 //The keys represent the size / weightof the course type
                 //The values represent the weight of the course type
                 var course_type = new Array();
                 course_type["AC"]="AC";
                 course_type["WS"]="WS";


                 //Set up an associative array 
                 //The keys represent the number of days
                 //The value represents the number of days
                 //We use this this array when the user set the number of days from the form
                 var number_days= new Array();
                 number_days["None"]=0 ;
                 number_days["1"]=1 ;
                 number_days["2"]=2 ;
                 number_days["3"]=3 ;
                 number_days["4"]=4 ;
                 number_days["5"]=5 ;


                 //Set up an associative array 
                 //The keys represent the number of AC people
                 //The value represents thenumber of people
                 //We use this this array when the user set number of people from the form 
                 var ACnumber_people= new Array();
                 ACnumber_people["None"]=0;
                 ACnumber_people["1"]=1;
                 ACnumber_people["2"]=2;
                 ACnumber_people["3"]=3;
                 ACnumber_people["4"]=4;
                 ACnumber_people["5"]=5; 
                 ACnumber_people["6"]=6;
                 ACnumber_people["7"]=7;
                 ACnumber_people["8"]=8; 

                  //Set up an associative array Workshop
                 //The keys represent the number of WS people
                 //The value represents thenumber of people
                 //We use this this array when the user set number of people from the form 
                 var WSnumber_people= new Array();
                 WSnumber_people["None"]=0;
                 WSnumber_people["1"]=4;
                 WSnumber_people["2"]=4;
                 WSnumber_people["3"]=4;
                 WSnumber_people["4"]=4;
                 WSnumber_people["5"]=4; 
                 WSnumber_people["6"]=5;
                 WSnumber_people["7"]=5;
                 WSnumber_people["8"]=5; 


                  //Onsite course delivery 
                 //getTrainerExp finds the trainer expenses based on a check box selection
                function getOnsite()

                {
                    var onsite="no";
                    //Get a reference to the form id="courseform"
                    var theForm = document.forms["courseform"];
                    //Get a reference to the checkbox id="includeexp"
                    var onSite = document.getElementById('onsite');

                    //If they checked the box set the course type value for Academy to 1.5 
                    if(onSite.checked==true)
                    {
                         course_weight["AC"]=1.5;
                    }
                    //return the trainer expenses
                    return onsite;
                }


                // getCourseWeight() finds the course weight for integer based on the slection.
                //  take user's the selection from radio button selection
                function getCourseWeight()
                {  
                    var courseweight=0;
                    //Get a reference to the form id="courseform"
                    var theForm = document.forms["courseform"];
                    //Get a reference to the typre the user Chooses name=selectedcoursetype":
                    var selectedCourseWeight = theForm.elements["selectedcoursetype"];
                    //Here since there are 4 radio buttons selectedCourseType.length = 4
                    //We loop through each radio buttons
                    for(var i = 0; i < selectedCourseWeight.length; i++)

                   {

                        //if the radio button is checked
                        if(selectedCourseWeight[i].checked)
                        {
                            //we set coursetype to the value of the selected radio button
                            //i.e. if the user choose the WS we set it to 1
                            //We get the selected Items value
                            //For example course_prices["WS".value]"
                            courseweight = course_weight[selectedCourseWeight[i].value];
                            //If we get a match then we break out of this loop
                            //No reason to continue if we get a match
                            break;
                        }
                    }

                    //We return the coursetype
                    return courseweight;
                }


                // getCourseType() finds the course type - AC or WS based on the selection.
                // take user's the selection from radio button selection
                function getCourseType()
                {  
                    var coursetype="none";
                    //Get a reference to the form id="courseform"
                    var theForm = document.forms["courseform"];
                    //Get a reference to the typre the user Chooses name=selectedcoursetype":
                    var selectedCourseType = theForm.elements["selectedcoursetype"];
                    //Here since there are 4 radio buttons selectedCourseType.length = 4
                    //We loop through each radio buttons
                    for(var i = 0; i < selectedCourseType.length; i++)
                    {
                        //if the radio button is checked
                        if(selectedCourseType[i].checked)
                        {
                            //we set coursetype to the value of the selected radio button
                            //i.e. if the user choose the WS we set it to 1
                            //We get the selected Items value
                            //For example course_prices["WS".value]"
                            coursetype = course_type[selectedCourseType[i].value];
                            //If we get a match then we break out of this loop
                            //No reason to continue if we get a match
                            break;
                        }
                    }

                    if 
                    //We return the coursetype
                    return coursetype;
                }


                //This function finds the number of days based on the 
                //drop down selection
                function getNumberDays()
                {
                    var numberdays=0;
                    //Get a reference to the form id="courseform"
                    var theForm = document.forms["courseform"];
                    //Get a reference to the select id="days"
                     var selectedDays = theForm.elements["days"];

                    //set Days equal to value user chose
                    //For example number_days["Lemon".value] would be equal to 5
                    numberdays = number_days[selectedDays.value];

                    //finally we return numberdays
                    return numberdays;
                }


                //This function finds the number of AC people based on the 
                //drop down selection
                function getACNumberPeople()
                {
                    var ACnumberpeople=0;
                    //Get a reference to the form id="courseform"
                    var theForm = document.forms["courseform"];
                    //Get a reference to the select id="people"
                     var selectedPeople = theForm.elements["people"];

                    //set AC People equal to value user chose
                    //For example number_days["Lemon".value] would be equal to 5
                    ACnumberpeople = ACnumber_people[selectedPeople.value];

                    //finally we return numberdays
                    return ACnumberpeople;
                }

                //This function finds the number of WS people based on the 
                //drop down selection
                function getWSNumberPeople()
                {
                    var WSnumberpeople=0;
                    //Get a reference to the form id="courseform"
                    var theForm = document.forms["courseform"];
                    //Get a reference to the select id="people"
                     var selectedPeople = theForm.elements["people"];

                    //set WSPeople equal to value user chose

                    WSnumberpeople = WSnumber_people[selectedPeople.value];

                    //finally we return numberdays
                    return WSnumberpeople;
                }


                //getTrainerExp finds the trainer expenses based on a check box selection
                function getTrainerExp()

                {
                    var trainerexp="None";
                    //Get a reference to the form id="courseform"
                    var theForm = document.forms["courseform"];
                    //Get a reference to the checkbox id="includeexp"
                    var includeExp = document.getElementById('includeexp');

                    //If they checked the box set trainerexp to ACMEEXP
                    if(includeExp.checked==true)
                    {
                        trainerexp=" - ACM-EXP";
                    }
                    //return the trainer expenses
                    return trainerexp;
                }

                //getTrainerKit finds the kit shipping based on a check box selection
                function getTrainerKit()

                {
                    var trainerkit="None";
                    //Get a reference to the form id="courseform"
                    var theForm = document.forms["courseform"];
                    //Get a reference to the checkbox id="includekit"
                    var includeKit = document.getElementById('includekit');

                    //If they checked the box set trainerexp to ACMEEXP
                    if(includeKit.checked==true)
                    {
                        trainerkit="  ACM-SHP";
                    }
                    //return the trainer expenses
                    return trainerkit;
                }

                function calculateTotal()
                {
                    //Here we get the total price by calling our function
                    //Each function returns a number so by calling them we add the values they return together
                    var pointstotal = 

                    if (course_type = AC)   
                    {  
                        getCourseWeight() * getNumberDays() * getACNumberPeople ()  
                    }  
                    else  if  (course_type = WS)  
                    {  
                        getCourseWeight() * getNumberDays() * getWSNumberPeople ()
                    }  

                    else 
                        {  

                    } ;


                    //display the result
                    var divobj = document.getElementById('totalPoints');
                    divobj.style.display='block';
                    divobj.innerHTML = "Points Total = " +pointstotal;

                }


                function calculateExp()
                {
                     var expense = getTrainerExp() ;
                     var days = getNumberDays() ;

                     document.getElementById("trainerexp").innerHTML = "Please include Qty " +days  +expense;
                     document.getElementById("trainertra").innerHTML = "Please include Qty 1 - ACM-TRA "
                }

                function calculateKit()
                {
                     var kit = getTrainerKit() ;

                     document.getElementById("trainerkit").innerHTML = "Please include Qty 1 - " +kit;

                }

                function hideTotal()
                {
                    var divobj = document.getElementById('totalPrice');
                    divobj.style.display='none';
                }

                function btns()
                {
                    window.location.replace('http://btnsdownloads.co.uk/site/btns8/index08.html');

                }
  • 4
    Check out the *minimal* in [mcve] – j08691 Aug 03 '17 at 14:07
  • 4
    You also have an assignment in `if (course_type = AC)` – j08691 Aug 03 '17 at 14:08
  • Apart from the lint error, are you trying to use `if` statement as a right-hand side expression? I mean `var pointstotal = if(...) {expression}` doesn't make any sense. It should throw you an error when running the script. – Teemu Aug 03 '17 at 14:12
  • Ok I have an assignment in if (course_type = AC), but I need the calculate function to be used if the course type is = AC – DudeNamedBen Aug 03 '17 at 14:13
  • @T.J.Crowder There seems to be a more serious problem in the code, than the comparison operator, please check my comment above. – Teemu Aug 03 '17 at 14:15
  • 1
    While the OP certainly had that error, that was not the answer to his problem, what he wanted to know! Your function caluclateTotal() is wrong, because u can't set the 'pointstotal' variable like that, what u could do is use ternary operator like this ` var pointstotal = course_type ===AC? "GET AC POINTS" : course_type === WS? "GET WS POINTS" : "ELSE DO SMTN ELSE" ` – high incompetance Aug 03 '17 at 14:17
  • This question and the immediately jumping to conclusions by SO's moderators shows everything I dislike about this site, you can't even ask a question, immediately some moderator already knows all your problems and can read your mind, we cannot even answer this question properly now, thanks for nothing – high incompetance Aug 03 '17 at 14:23

1 Answers1

0

You have a random if statement in your code:

if
//We return the coursetype
return coursetype;
R Pelzer
  • 1,188
  • 14
  • 34