Home > Software engineering >  is there away to replace a text if the year is greater than the currentYear.?
is there away to replace a text if the year is greater than the currentYear.?

Time:11-05

I wanna replace "was on " to "will be " if the year is greater than the currentYear.

let myDate = new Date();
const days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday' ];
const currentYear = myDate.getFullYear();
let tense = 'was on';
for(let year= 2020; year <= 2030; year  ){
    let d = new Date(year, 4, 27);
    if(d.getDate() === 27 && d.getMonth() === 4 ){
        const output = `my birthday in ${year} ${tense} ${days[d.getDay()]}`;
        console.log(output)
    }
}

But when I include an if statement, it's returning nothing - no error, no results.

CodePudding user response:

Try this:

const days = [
    'Sunday',
    'Monday',
    'Tuesday',
    'Wednesday',
    'Thursday',
    'Friday',
    'Saturday'
];
const now = new Date();
const currentYear = now.getFullYear();

for(let year= 2020; year <= 2030; year  ){
    const d = new Date(year, 4, 27);
    let tense = year > currentYear ? 'will be'
      : year === currentYear ? 'is on'
      : 'was on';

    if(d.getDate() === 27 && d.getMonth() === 4 ){
        const output = `my birthday in ${year} ${tense} ${days[d.getDay()]}`;
        console.log(output)
    }
}

CodePudding user response:

This works using the ternary operator in javascript.

const ripe = true;  
const egg = ripe===true? "GOOD" :"BAD";

Read more here (https://www.javascripttutorial.net/javascript-ternary-operator/)

   let myDate = new Date();
   const days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday' ];
   const currentYear = myDate.getFullYear();
   let tense1 = 'was on';
   let tense2 = "will be"
   
   for(let year= 2020; year <= 2030; year  ){
        let d = new Date(year, 4, 27);
        if(d.getDate() === 27 && d.getMonth() === 4 ){  
           // If year>currentYear then(?) tense1 else(:) tense2
           const preTense = parseInt(year)>parseInt(currentYear)? tense1 : tense2;
           
           const output = `my birthday in ${year} ${preTense} ${days[d.getDay()]}`; 
    
           console.log(output)
        } 
   }

CodePudding user response:

let myDate = new Date();
   const days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday' ];
   const currentYear = myDate.getFullYear();
   let tense = 'was on';
   for(let year= 2020; year <= 2030; year  ){
    let d = new Date(year, 4, 27);
    if(d.getDate() === 27 && d.getMonth() === 4 ){  
       
      if(year < currentYear){
        const output = `my birthday in ${year} ${tense} ${days[d.getDay()]}`;
        console.log(output)
      }else{
       let tense = 'will be';
       const output = `my birthday in ${year} ${tense} ${days[d.getDay()]}`;
       console.log(output)
      }
    } 
}
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

Here you have it :)

let myDate = new Date();
   const days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday' ];
   const currentYear = myDate.getFullYear();
   let tense = 'was on';
   for(let year= 2010; year <= 2030; year  ){
    
    if (parseInt(currentYear) < parseInt(year)) {tense = 'will be on';}
    let d = new Date(year, 4, 27);
    if(d.getDate() === 27 && d.getMonth() === 4 ){  
      const output = `my birthday in ${year} ${tense} ${days[d.getDay()]}`; 
       console.log(output)
    } 
}
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related