index.js 5.33 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
    readerLevel: '',        // Value of reader level selected from dropdown
    readerCategory: ''      // Value of reader 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
36
function handleTextAreaChange() {
    let value = document.getElementById("manual-text-entry").value;
    value = value.trim();
37
    inputObj.text = value;
38
39
40
41
42
    let characters = value.split(' ').join('').length;
    setCharacterLength(characters);
    document.getElementById("calculatedTextCharacters").setAttribute('value', characters)
}

43
44
45

// function handleCharacterInputChange: captures changes in number of characters input field
// and sets number of characters to property textLength on inputObj
46
47
48
49
50
function handleCharacterInputChange() {
    let value = document.getElementById("inputTextCharacters").value;
    setCharacterLength(value);
}

51
// function setCharacterLength: sets number of characters to property textLength on inputObj
52
function setCharacterLength(characters) {
53
    inputObj.textLength = parseInt(characters);
54
55
56
}


57
58
59
// 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
60
function isFormValid() {
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
    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';
    }

    if (inputObj.readerLevel === '') {
        invalidFields++;
        document.getElementById('readerLevel-validation').style.display = 'block';
    } else {
        document.getElementById('readerLevel-validation').style.display = 'none';
    }

    if (inputObj.readerCategory === '') {
        invalidFields++;
        document.getElementById('readerCategory-validation').style.display = 'block';
    } else {
        document.getElementById('readerCategory-validation').style.display = 'none';
    }
89
90

    return invalidFields < 1
91
92
}

93
94

// function showFeedbackForm: displays feedback form when calculation is done
95
function showFeedbackForm() {
96
97
    let element = document.getElementById('feedback-form');
    element.style.display = "block";
98
}
99

100
101
102

// function readerLevelChangeEvent: listens for change in reader level dropdown
// and sets selected value to property readerLevel on inputObj
103
function readerLevelChangeEvent(event) {
104
    inputObj.readerLevel = parseInt(event.target.value);
105
106
}

107
108
// function readerCategoryChangeEvent: listens for change in reader category dropdown
// and sets selected value to property readerCategory on inputObj
109
function readerCategoryChangeEvent(event) {
110
    inputObj.readerCategory = parseInt(event.target.value);
111
}
112
113
114
115
116
117
118
119
120
121
122
123
124


// 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
125
        // TODO: logic or algorithm for calculating reading time goes here
126
            // inputObj.textLength has number of characters (number data type)
Ratnadeep Rajendra Kharade's avatar
Ratnadeep Rajendra Kharade committed
127
128
            // inputObj.readerLevel has level of reader (number data type)
            // inputObj.readerCategory has category of reader (number data type)
129

130
131
132
133
134
135
136
137
138
            
        // example calculation
        x = (inputObj.textLength * inputObj.readerLevel * inputObj.readerCategory);


    
        // set value to html element
        var s= document.getElementById('reading-time-element');
        s.innerHTML = x;
139
140
141
142
143
        // display html element which shows time
        let element = document.getElementById('calculate-time-element');
        element.style.display = "block";
    }
}