Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
All The below codding snippets works perfectly. Need to make sure that the below snippets of code can be simplified using any of an advanced ECMA standards

JavaScript
let pauseAllcount = 0, resumeAllcount = 0;
    this.xUserDetails.forEach((el, indx) => {
        if (this.xUserDetails[indx].userProctorSignal === 1) {
            pauseAllcount++;
        } else if (this.xUserDetails[indx].userProctorSignal === 0) {
            resumeAllcount++;
        }
    });


The above snippets could be written as

JavaScript
let pauseAllcount = 0, resumeAllcount = 0;
    pauseAllcount = this.xUserDetails.filter(x => x.userProctorSignal === 1).length;
    resumeAllcount = this.xUserDetails.filter(x => x.userProctorSignal === 0).length;


Results will be one and same. But I'm afraid that iterating over the same array twice, I might risk the performance overhead cost by O(2n). Will this same block of code couldn't be written to reduce no of lines and replaced by advanced ECMA approach giving optimistic solutions?...

Likewise showing all the below 5 coding snippets could be simplified with advanced ECMA

I Code:
JavaScript
if (this.pageReqDetails.RowTot === this.xUserDetails.length) {
        if (this.userdetails.every(x => x.extensionTime === this.userdetails[0].extensionTime)) {
            this.timeForallUser = this.userdetails[0].extensionTime;
        }
        if (pauseAllcount === this.xUserDetails.length) {
            this.toggleButtonDisplayType = 1;
        } else if (resumeAllcount === this.xUserDetails.length) {
            this.toggleButtonDisplayType = 0;
            this.timeForallUser = 0;
        } else {
            this.toggleButtonDisplayType = -1;
            this.timeForallUser = 0;
        }
    } else {
        this.timeForallUser = 0;
    }


II Code:
JavaScript
let flagPauseAll = false, flagResumeAll = false;
    if (this.pageReqDetails.RowTot === this.xUserDetails.length) {
        this.xUserDetails.forEach((el, indx) => {
            if (type === 0) {
                if (el.extensionTime === time) {
                    this.timeForallUser = 0;
                    this.toggleButtonDisplayType = 0;
                } else {
                    flagResumeAll = true;
                }
            } else if (type === 1) {
                if (el.extensionTime === time) {
                    this.timeForallUser = time;
                    this.toggleButtonDisplayType = 1;
                } else {
                    flagPauseAll = true;
                }
            }
        });
    }
    if (flagPauseAll || flagResumeAll) {
        this.timeForallUser = 0;
        this.toggleButtonDisplayType = -1;
    }


III Code:
JavaScript
let countAllResume = 0, countAllPause = 0;
    this.userdetails.forEach(x => {
        if (type === 1) {
            x.userProctorSignal = 1;
            x.extensionTime = time;
        } else if (type === 0) {
            x.userProctorSignal = 0;
            x.extensionTime = 0;
            x.xextensionTime = 0;
        }
    });
    this.xUserDetails.forEach((el, indx) => {
        if (type === 1 && el.userProctorSignal === 1) {
            el.userProctorSignal = type;
            el.extensionTime = time;
            el.xextensionTime = time;
            countAllPause++;
        }
        if (type === 0 && el.userProctorSignal === 0) {
            el.userProctorSignal = 0;
            el.extensionTime = 0;
            el.xextensionTime = 0;
            countAllResume++;
        }
    })
    if (this.xUserDetails.length === countAllPause) {
        this.timeForallUser = time;
        this.toggleButtonDisplayType = 1;
    } else if (this.xUserDetails.length === countAllResume) {
        this.timeForallUser = 0;
        this.toggleButtonDisplayType = 0;
    }


IV Code:
JavaScript
this.userdetails.forEach((el, indx) => {
        if (el.examUserId === examUserDetail.examUserId) {
            this.userdetails[indx] = examUserDetail;
            el.xextensionTime = time;
            el.extensionTime = time;
        }
    });


V Code:
JavaScript
if (this.userdetails[i].extensionTime !== null && this.userdetails[i].extensionTime > 0) {
        this.userdetails[i].extensionTime /= 60;
        this.userdetails[i]['xextensionTime'] = this.userdetails[i].extensionTime;
    } else if (this.userdetails[i].extensionTime === null || this.userdetails[i].extensionTime === 0) {
        this.userdetails[i].extensionTime = 0;
        this.userdetails[i]['xextensionTime'] = 0;
    }

The above 5 coding snippets were written in a old school manner with vanilla JS. As I require further assistance of converting this into advanced & much smaller lines of code that would suffice a lot.
Any help with single or partial snippets which could be simplified would be great and are always welcome to post..

What I have tried:

Tried to make use of Advanced ECMA. But result wouldn't fit to the above shown code
Any Help..
Posted
Updated 3-Jan-21 5:45am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900