index.js 5.94 KB
Newer Older
1
// Object which stores all the user selected/entered values
2
let inputObj = {
3
4
    textChoice: 'manual',   // choice of text entry initially set as manual. Can have values manual, calculated
    text: '',               // User entered text in case of manual  
Ratnadeep Rajendra Kharade's avatar
Ratnadeep Rajendra Kharade committed
5
    textLength: '',         // number of characters in text. Either from user entered text or user entered number
6
7
    schoolLevel: '',        // Value of school level selected from dropdown
    category: ''            // Value of category selected from dropdown
8
};
9

10
11

// function handleTextRadioClick: responsible for showing or hiding text input / text length input based on selected option
12
function handleTextRadioClick() {
13
14
    let textChoice = document.querySelector('input[name="text-input-option"]:checked').value;
    inputObj.textChoice = textChoice;
15
16
17
18
    toggleElement('character-calculation-wrapper');
    toggleElement('manual-character-input');
}

19
20
21

// function toggleElement: hides/shows the html element based on current visibility. 
// accepts value of id attribute of element as parameter
22
function toggleElement(id) {
23
24
25
    let element = document.getElementById(id);
    if (element.style.display === "none") {
        element.style.display = "block";
26
    } else {
27
        element.style.display = "none";
28
29
30
    }
}

31
32
33

// function handleTextAreaChange: captures the changes as user enters the text and gets the length
// and sets number of characters to property textLength on inputObj
34
35
function handleTextAreaChange() {
    let value = document.getElementById("manual-text-entry").value;
36
    inputObj.text = value;
37
38
    value = removeSpecialCharacters(value);
    let characters = value.length;
39
    setCharacterLength(characters);
40
41
42
43
44
45
46
47
    document.getElementById("calculatedTextCharacters").setAttribute('value', characters);
}

// function removeSpecialCharacters: removes punctuation marks and other special characters
// but allows numbers and alphabets along with language specific characters German: äöüÄÖÜß and French: ùûüÿàâæéèêëïîôœÙÛÜŸÀÂÆÉÈÊËÏÎÔŒ
function removeSpecialCharacters(str) {
    return str.replace(/(?!\w|||||||||||\ÿ|||||||||||||||||||||||||\Œ)./g, '')  // regex to remove special characters 
        .replace(/^(\s*)([\W\w]*)(\b\s*$)/g, '$2');   // regex to trim the string to remove any whitespace at the beginning or the end
48
49
}

50
51
52

// function handleCharacterInputChange: captures changes in number of characters input field
// and sets number of characters to property textLength on inputObj
53
54
55
56
57
function handleCharacterInputChange() {
    let value = document.getElementById("inputTextCharacters").value;
    setCharacterLength(value);
}

58
// function setCharacterLength: sets number of characters to property textLength on inputObj
59
function setCharacterLength(characters) {
60
    inputObj.textLength = parseInt(characters);
61
62
63
}


64
65
66
// function isFormValid: checks for mandatory input fields and validates te form 
// returns true if all values are filled else returns false
// also shows error messages for respective inputs fields
67
function isFormValid() {
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
    let invalidFields = 0;
    if (inputObj.textChoice === 'manual' && inputObj.text === '') {
        invalidFields++;
        document.getElementById('textarea-validation').style.display = 'block';
    } else {
        document.getElementById('textarea-validation').style.display = 'none';
    }

    if (inputObj.textChoice === 'calculated' && (inputObj.textLength === '' || inputObj.textLength === 0)) {
        invalidFields++;
        document.getElementById('textCharacters-validation').style.display = 'block';
    } else {
        document.getElementById('textCharacters-validation').style.display = 'none';
    }

83
    if (inputObj.schoolLevel === '' || inputObj.schoolLevel === 0) {
84
        invalidFields++;
85
        document.getElementById('schoolLevel-validation').style.display = 'block';
86
    } else {
87
        document.getElementById('schoolLevel-validation').style.display = 'none';
88
89
    }

Ratnadeep Rajendra Kharade's avatar
Ratnadeep Rajendra Kharade committed
90
    if (inputObj.category === '' || inputObj.category === 0) {
91
        invalidFields++;
92
        document.getElementById('category-validation').style.display = 'block';
93
    } else {
94
        document.getElementById('category-validation').style.display = 'none';
95
    }
96
97

    return invalidFields < 1
98
99
}

100
101

// function showFeedbackForm: displays feedback form when calculation is done
102
function showFeedbackForm() {
103
104
    let element = document.getElementById('feedback-form');
    element.style.display = "block";
105
}
106

107

108
109
110
111
// function schoolLevelChangeEvent: listens for change in school level dropdown
// and sets selected value to property schoolLevel on inputObj
function schoolLevelChangeEvent(event) {
    inputObj.schoolLevel = parseInt(event.target.value);
112
113
}

114
115
116
117
// function categoryChangeEvent: listens for change in category dropdown
// and sets selected value to property category on inputObj
function categoryChangeEvent(event) {
    inputObj.category = parseInt(event.target.value);
118
}
119
120
121
122
123
124
125
126
127
128
129
130
131


// function calculateReadingTime: This function is responsible for calculating reading time
// reading time can be calculated if all inputs are valid
function calculateReadingTime() {

    if (isFormValid()) {

        //variable x which holds final reading time in minutes
        let x = 0;

        console.log(inputObj);

Ratnadeep Rajendra Kharade's avatar
Ratnadeep Rajendra Kharade committed
132
        // TODO: logic or algorithm for calculating reading time goes here
133
        // inputObj.textLength has number of characters (number data type)
134
135
        // inputObj.schoolLevel has level of school (number data type)
        // inputObj.category has category of reader (number data type)
136

137

138
        // example calculation
139
        x = (inputObj.textLength * inputObj.schoolLevel * inputObj.category);
140
141


142

143
        // set value to html element
144
        var s = document.getElementById('reading-time-element');
145
        s.innerHTML = x;
146
147
148
149
150
        // display html element which shows time
        let element = document.getElementById('calculate-time-element');
        element.style.display = "block";
    }
}