简体   繁体   中英

How to shorten this if statement?

I have an if statement like this:

if(this.exercisesDoneArray[ind][i].done && this.exercisesDoneArray[ind][1].done && this.exercisesDoneArray[ind][i].exercise === this.slides.clickedSlide.nextElementSibling.nextElementSibling.id || this.exercisesDoneArray[ind][1].exercise === this.slides.clickedSlide.nextElementSibling.nextElementSibling.id){

But I want wondering how can I shorten this..

How about using some variables?

var ex1 = this.exercisesDoneArray[ind][i],
    ex2 = this.exercisesDoneArray[ind][1],
    nextSlide = this.slides.clickedSlide.nextElementSibling.nextElementSibling.id;

if(ex1.done && ex2.done && ex1.exercise === nextSlide || ex2.exercise === nextSlide){

Use few variables and then put outside the global if another if where you check if your main excercise has been done, because it has always to be done so no need to check always inside the other if with an and

var currentExcercise = this.exercisesDoneArray[ind][i];
var baseExcercise = this.exercisesDoneArray[ind][1];
var slideId = this.slides.clickedSlide.nextElementSibling.nextElementSibling.id;

if (!baseExcercise.done) {
    return false;
}
if(currentExcercise.done && currentExcercise.exercise === slideId  || baseExcercise .exercise === slideId ){

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM