Setting values in a function not working in last two if statements

I have a following fileUpload function and it’s triggered when a button is clicked (not shown here as it’s not needed).

I can see that the first two if statements are working fine i.e. if companyIdFromSession === null and employeIdFromSession === null`, I can see the below two console.log statements printing an empty string:

console.log ("companyIdFromSession "+companyIdFromSession);  
console.log(" employeIdFromSession "+ employeIdFromSession);

However, the last two console.log statements are not working as expected. i.e. even if there is a scenario where companyIdFromSession is null and answerToQuestion1 is Y, it’s not setting answerToQuestion1 to N. And I see Y getting printed in the following console log statement:
console.log("answerToQuestion1 "+answerToQuestion1);

Same thing is happening for the last if statement as well. I don’t see console.log("answerToQuestion1 "+answerToQuestion1); printing N.

Any idea what I am doing wrong here?

const fileUpload = (e) => {

        const growlComp = growl.current
        const analystId = JSON.parse(sessionStorage.loggedInUser).id

        let companyIdFromSession = sessionStorage.getItem("selectedCompanyId");
        let employeIdFromSession = sessionStorage.getItem("selectedEmployeeId");

        const answerToQuestion1 = sessionStorage.getItem("answerToQuestion1");
        const answerToQuestion2 = sessionStorage.getItem("answerToQuestion2");

        if(companyIdFromSession === null){
            console.log("Company ID from session is NULL and hence setting it to an empty string");
            companyIdFromSession = ''
        }
        if(employeIdFromSession === null){
            console.log("Employee ID from session is NULL and hence setting it to an empty string");
            employeIdFromSession = ''
        }
		
		if(companyIdFromSession === null && answerToQuestion1 == 'Y'){
            answerToQuestion1 = 'N'
        }
		
		 if(employeIdFromSession === null && answerToQuestion2 == 'Y'){
            answerToQuestion2 = 'N'
        }
		
		
	console.log("Testing all values from session storage when Upload button is clicked");
        console.log ("companyIdFromSession "+companyIdFromSession);
        console.log ("companyIdFromSession String Length "+companyIdFromSession.length);
        console.log(" employeIdFromSession "+ employeIdFromSession);
        console.log(" employeIdFromSession string length "+ employeIdFromSession.length);
        console.log("answerToQuestion1 "+answerToQuestion1);
        console.log("answerToQuestion2 "+answerToQuestion2);
      


        
    }

You define both variables to be const. Do you know what that means? It means you can’t change their values later. Like assigning the value N to them. :wink:

Yes, I had to change that. :slight_smile:

I got it working in my last two if statements. I was comparing it to null. I fixed it by comparing it to an empty string since it was already assigned to an empty string in the first two if statements.

i am wondering if there is a way to use ternary operator for the last two if statements?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.