Home > Software engineering >  How to compare a string with number
How to compare a string with number

Time:04-15

I have a span id with textContent which have their own time(hour/minutes)

<div >
<span  >8 min</span>
</div>
<div >
<span  >22 min</span>
</div>
<div >
<span  >38 min</span>
</div>
<div >
<span  >1 hour</span>
</div>
<div >
<span  >1 day</span>
</div>

How can we show the span text whose time is less then 60 min, i don't want to show the text of span which contains 1 hour or 1 day any way to do that and yes string with number is necessary for my project.

CodePudding user response:

const els = document.querySelectorAll(".time")
els.forEach(el => {
  if (/(hour)|(day)/.test(el.textContent)) el.style.display = "none"
})
<div >
  <span >8 min</span>
</div>
<div >
  <span >22 min</span>
</div>
<div >
  <span >38 min</span>
</div>
<div >
  <span >1 hour</span>
</div>
<div >
  <span >1 day</span>
</div>

CodePudding user response:

for(var i=0;i<5;i  ){
    if(document.querySelectorAll(".time")[i].textContent.includes("min")){
        console.log(document.querySelectorAll(".time")[i].textContent);
    }
} 
}

This works too

  • Related