-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjavascriptpractice.html
67 lines (63 loc) · 1.94 KB
/
javascriptpractice.html
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
<input type="text" id="text">
<input type="button" id="clear" value="clear">
<input type="button" id="showeven" value="show even">
<input type="button" id="factorial" value="factorial">
<input type="button" id="removeduplicates" value="remove duplicates">
<input type="button" id="FizzBuzz" value="FizzBuzz">
<p id="para"></p>
<script>
document.getElementById("clear").onclick = function(){
document.getElementById('para').innerHTML = "";
document.getElementById("text").value = "";
}
document.getElementById("showeven").onclick = function(){even(document.getElementById("text").value)};
document.getElementById("factorial").onclick = function(){factorial(document.getElementById("text").value)};
document.getElementById("removeduplicates").onclick = function(){removeDuplicates(document.getElementById("text").value)};
document.getElementById("FizzBuzz").onclick = function(){fizzbuzz(document.getElementById("text").value)};
function even(text){
while(text>0){
if(text%2==0){
document.getElementById('para').innerHTML=text + "<br>" + document.getElementById('para').innerHTML;
}
text--;
}
}
function factorial(text){
var fact=1;
while(text>1){
fact *= text;
text--;
}
document.getElementById('para').innerHTML="Factorial: " +fact;
}
function removeDuplicates(text){
var str;
var final = [];
var duplicate=0;
str=text.split(" ");
for (var i = 0; i < str.length; i++) {
duplicate=0;
if(i>0){
for (var j = 0; j < i; j++) {
if(str[i]==str[j]){
duplicate=1;
}
}
}
if (duplicate==0) {
final[final.length]=str[i];
}
}
document.getElementById('para').innerHTML=final;
}
function fizzbuzz(text){
var str = "";
for(var i=1;i<=text;i++){
if(i%15==0){str +="FizzBuzz"+"<br>";}
else if(i%3==0){str +="Fizz"+"<br>";}
else if(i%5==0){str +="Buzz"+"<br>";}
else {str += i+"<br>";}
}
document.getElementById("para").innerHTML += str;
}
</script>