Home > front end >  if else statement not working(nodejs,html)
if else statement not working(nodejs,html)

Time:07-26

I have this code that will get the year from a html form and im trying to set an argument for it but the else statement never seem to execute. Any clue as to why?

exports.printyear = function(request, response) {
        let form = new formi.IncomingForm();
        form.parse(request, function(error, field, file) {
            let year = field.year;
            let currenturl = "";
            console.log("YEAR");
            console.log(year);
            if (year === 2007 | 2008 | 2009) {
                currenturl = "websiteurl"   year   ".xml";
            } else {
                currenturl = "websiteurl"   year   ".json";
            }
            console.log(currenturl);
            response.end();
        });

CodePudding user response:

The right way of using or in conditions is to separate them.

Change

if (year===2007|2008|2009) {

To

if (year===2007 || year===2008 || year===2009) {

CodePudding user response:

The issues seems to be this line:

if (year===2007|2008|2009) {

You need to compare each value one by one like this:

if (year===2007||year===2008||year===2009)

What you're doing right now it a bitwise operator |

(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_OR)

CodePudding user response:

Unless I'm missing something, the syntax in the if statement is is not valid JS. Try the following:

if (year === 2007 || year === 2008 || year === 2009) {
    currenturl = "websiteurl"   year   ".xml";
} else {
    currenturl = "websiteurl"   year   ".json";
}
  • Related