-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.js
167 lines (123 loc) · 5.26 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// var topic = 'data%20structures';
var searchFor = function search(){
var topic = document.getElementById('searchBox').value;
topic = getTag(topic).toLowerCase();
topic.replace(/ /g, '+');
//console.log(topic);
axios.get(`http://codeforces.com/api/problemset.problems?tags=${topic}`)
.then(function(response){
var problems = response.data.result.problems;
//console.log(problems.length);
//console.log(document.getElementById('listBox').classList);
if(problems.length===0){
throw new Error();
}
getVideos(topic);
var ul = document.getElementById('listBox');
document.getElementById('qList').classList.remove('hide-display');
document.getElementById('videoBox').classList.remove('hide-display');
document.getElementById('heading').innerText = `Problems on ${topic} :`;
ul.innerHTML = "";
// console.log(response);
for(var i=0;i<8;i++){
var problemBox = problemBoxCreator(problems[i]);
ul.appendChild(problemBox);
}
}).catch(function(response){
document.getElementById('qList').classList.add('hide-display');
document.getElementById('videoBox').classList.add('hide-display');
document.getElementById('searchBox').value = "";
document.getElementById('searchBox').placeholder = 'Sorry! Topic not available rn.';
});
};
function getTag(topic){
if(topic==='stacks'||topic==='queues'){
return 'data+strcutures';
}else if(topic==='dynamic programming'){
return 'dp';
}else if(topic==='sorting'){
return 'sortings';
}else if(topic==='maths'){
return 'math';
}
return topic;
}
function problemBoxCreator(problem){
var problemBox = document.createElement('li');
problemBox.classList.add('problem-box');
problemBox.innerHTML = `<p>${problem.name}</p><a class='btn btn-primary' href='http://www.codeforces.com/problemset/problem/${problem.contestId}/${problem.index}'>Solve!</a>`
return problemBox;
}
document.getElementById('timeRem').classList.add('hide-display');
function codeChallengesInitiator(button){
button.classList.add('fade-display')
axios.get('https://cors-anywhere.herokuapp.com/https://leetcode.com/api/problems/algorithms/')
.then(function(response){
button.classList.add('hide-display');
button.classList.remove('fade-display');
document.getElementById('timeRem').classList.toggle('hide-display');
var leetAlgoContainer = document.getElementById('ques');
var time_in_minutes = 45;
var current_time = Date.parse(new Date());
var deadline = new Date(current_time + time_in_minutes*60*1000);
ques.innerHTML = "";
run_clock('clockdiv',deadline);
for(var i=0;i<2;i++){
var index = Math.floor((Math.random()*1000)%996);
var challenge = challengeCreator(response.data.stat_status_pairs[index]);
leetAlgoContainer.appendChild(challenge);
}
});
}
function challengeCreator(challengeQues){
var div = document.createElement('div');
div.innerHTML = `<h2>${challengeQues.stat.question__title}</h2><p>Difficulty: ${challengeQues.difficulty.level}</p><a target="_blank" class="btn btn-info" href='https://leetcode.com/problems/${challengeQues.question__title_slug}/'>Let's solve it!</a>`;
div.classList.add('challenge-cards');
return div;
}
function getVideos(searchString){
var outerDiv = document.getElementById('videoBox');
outerDiv.innerHTML="";
outerDiv.innerHTML = '<div cl><p id="heading" class="display-3">Video References: </p><hr></div>';
searchString+='+computer+science';
console.log(searchString);
axios.get(`https://www.googleapis.com/youtube/v3/search?part=snippet&q="${searchString}"&type=video&key=AIzaSyAqwA4O_fmm8ThUT9k6sXwA-X-B6DsPf1g`)
.then(function(response){
console.log(response);
var videoList = response.data.items;
for(let i =0;i<2;i++){
var iFrame = getIFrame(response.data.items[i].id.videoId);
outerDiv.appendChild(iFrame);
}
})
}
function getIFrame(videoId){
var frame = document.createElement('iframe');
frame.setAttribute('src',`http://www.youtube.com/embed/${videoId}`);
frame.setAttribute('height','315');
frame.setAttribute('width','420');
frame.classList.add('video-frame');
return frame;
}
function time_remaining(endtime){
var t = Date.parse(endtime) - Date.parse(new Date());
var seconds = Math.floor( (t/1000) % 60 );
var minutes = Math.floor( (t/1000/60) % 60 );
var hours = Math.floor( (t/(1000*60*60)) % 24 );
var days = Math.floor( t/(1000*60*60*24) );
return {'total':t, 'days':days, 'hours':hours, 'minutes':minutes, 'seconds':seconds};
}
function run_clock(id,endtime){
var clock = document.getElementById(id);
function update_clock(){
var t = time_remaining(endtime);
clock.innerHTML = '<div class="display-4">'+t.minutes+' m : '+t.seconds+' s</div>';
if(t.total<=0){ clearInterval(timeinterval);
//codeChallengesInitiator();
clock.innerHTML = `<div class="display-4 danger">Time's up!</div>`;
document.getElementById('ques').classList.add('disable-click');
}
}
update_clock();
var timeinterval = setInterval(update_clock,1000);
}