Skip to content

Commit c0374e4

Browse files
committed
convert strings to template strings
1 parent ae9d5c0 commit c0374e4

File tree

1 file changed

+32
-37
lines changed

1 file changed

+32
-37
lines changed

src/index.js

Lines changed: 32 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
(function($, JSON, localStorage){
22
const {url} = options = {
3-
url: 'data/quiz.json?' + Date.now()
3+
url: `data/quiz.json?${Date.now()}`
44
}
55

66
$.ajax({ url }).done(function(data) {
@@ -16,13 +16,13 @@
1616

1717
// Append the progress bar to DOM
1818
$('body')
19-
.append('<div style="position: fixed; bottom: 0; background: #eee; width: 100%; height: 6px; ">' +
20-
'<div id="progress" style="background: #1678c2; width: 1%;">&nbsp;</div>' +
21-
'</div>')
19+
.append(`<div style="position: fixed; bottom: 0; background: #eee; width: 100%; height: 6px; ">
20+
<div id="progress" style="background: #1678c2; width: 1%;">&nbsp;</div>
21+
</div>`)
2222

2323
// Append title and form to quiz
2424
$('#quiz')
25-
.append('<h1 class="ui header">' + data.title + '</h1>')
25+
.append(`<h1 class="ui header">${data.title}</h1>`)
2626
.append('<form id="quiz-form" class="ui form"></form>')
2727

2828
// For each question of the json,
@@ -43,59 +43,55 @@
4343
const {[j]:option} = options
4444
const checked = !!responses[i] && responses[i].includes(option.label) ? 'checked' : ''
4545

46-
inputHtml += '<div class="field">' +
47-
'<div class="ui checkbox ' + type + '">' +
48-
'<input type="' + type + '" ' + checked + ' name="question_' + i + '" id="question_' + i + '_' + j + '" value="' + option.label + '">' +
49-
'<label for="question_' + i + '_' + j + '">' + option.label + '</label>' +
50-
'</div>' +
51-
'</div>'
46+
inputHtml += `<div class="field">
47+
<div class="ui checkbox ${type}">
48+
<input type="${type}" ${checked} name="question_${i}" id="question_${i}_${j}" value="${option.label}">
49+
<label for="question_${i}_${j}">${option.label}</label>
50+
</div>
51+
</div>`
5252
}
5353
inputHtml += '</div>'
5454
break
5555

5656
// Set of inputs (composed response)
5757
case 'inputs':
5858
inputHtml = '<table>'
59-
for (j = 0; j < options.length; j++) {
59+
for (let j = 0; j < options.length; j++) {
6060
const {[j]:option} = options
6161
const value = responses[i] && responses[i][j] || ''
6262

63-
inputHtml += '<tr>' +
64-
'<td><label for="question_' + i + '_' + j + '">' + option.label + '</label></td>' +
65-
'<td width="15px"></td>' +
66-
'<td><div class="ui input">' +
67-
'<input type="text" placeholder="Response..." name="question_' + i + '" id="question_' + i + '_' + j + '" value="' + value + '" />' +
68-
'</div></td>' +
69-
'</tr>' +
70-
'<tr><td colspan="3">&nbsp;</tr></tr>'
63+
inputHtml += `<tr>
64+
<td><label for="question_${i}_${j}">${option.label}</label></td>
65+
<td width="15px"></td>
66+
<td><div class="ui input">
67+
<input type="text" placeholder="Response..." name="question_${i}" id="question_${i}_${j}" value="${value}" />
68+
</div></td>
69+
</tr>
70+
<tr><td colspan="3">&nbsp;</tr></tr>`
7171
}
7272
inputHtml += '</table>'
7373
break
7474

7575
// Default: simple input
7676
default:
7777
const value = responses[i] || ''
78-
inputHtml = '<div class="ui input fluid">' +
79-
'<input type="text" placeholder="Response..." name="question_' + i + '" value="' + value + '" />' +
80-
'</div>'
78+
inputHtml = `<div class="ui input fluid">
79+
<input type="text" placeholder="Response..." name="question_${i}" value="${value}" />
80+
</div>`
8181
}
8282

83-
$question = $('<div id="question-' + i + '" class="ui card" style="width: 100%;">' +
84-
'<div class="content">' +
85-
'<div class="header">' + problem + '</div>' +
86-
'</div>' +
87-
'<div class="content">' +
88-
inputHtml +
89-
'</div>' +
90-
'</div>'
83+
$question = $(`<div id="question-${i}" class="ui card" style="width: 100%;">
84+
<div class="content"><div class="header">${problem}</div></div>
85+
<div class="content">${inputHtml}</div>
86+
</div>`
9187
).css('display', 'none')
9288

9389
$('#quiz-form')
9490
.append($question)
9591

9692
// Show current question
9793
$('#quiz-form')
98-
.find('#question-' + currentQuestion)
94+
.find(`#question-${currentQuestion}`)
9995
.css('display', 'block')
10096

10197
// Update progress bar
@@ -124,7 +120,7 @@
124120

125121
// Actions on every response submission
126122
$('#submit-response').on('click', function() {
127-
const $inputs = $('[name^=question_' + currentQuestion + ']')
123+
const $inputs = $(`[name^=question_${currentQuestion}`)
128124
const question = questions[currentQuestion]
129125
let response = responses[currentQuestion]
130126

@@ -133,7 +129,7 @@
133129
case 'checkbox':
134130
case 'radio':
135131
response = []
136-
$('[name=' + $inputs.attr('name') + ']:checked').each(function(i, input) {
132+
$(`[name=${$inputs.attr('name')}]:checked`).each(function(i, input) {
137133
response.push(input.value)
138134
})
139135
response = response.length ? response : null
@@ -190,11 +186,11 @@
190186

191187
// Display next question
192188
$('#quiz-form')
193-
.find('#question-' + currentQuestion).css('display', 'none')
189+
.find(`#question-${currentQuestion}`).css('display', 'none')
194190

195191

196192
$('#quiz-form')
197-
.find('#question-' + ++currentQuestion).css('display', 'block')
193+
.find(`#question-${++currentQuestion}`).css('display', 'block')
198194

199195
// If it was the las question, display final message
200196
if (responseCount === questions.length) {
@@ -204,7 +200,6 @@
204200
}
205201
}
206202

207-
208203
// Save current state of the quiz
209204
localStorage.setItem('quiz', JSON.stringify({responses, responseCount, currentQuestion}))
210205
})

0 commit comments

Comments
 (0)