-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtask1.js
More file actions
94 lines (82 loc) · 2.06 KB
/
task1.js
File metadata and controls
94 lines (82 loc) · 2.06 KB
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
// Write a function called FooBar that takes integer n and prints all the numbers from 1 upto n. If the number is divisible by 3 then print "Foo", if the number is divisible by 5 then print "Bar" and if the number is divisible by both 3 and 5, print "FooBar". Otherwise, just print the number.
// Ex:
// Input: n = 15
// Output: 1 2 Foo 4 Bar Foo 7 8 Foo Bar 11 Foo 13 14 FooBar
// Solutions
// Naveed
function FooBar() {
let userNumber = document.getElementById("userNumber").value;
for(let i = 1; i <= userNumber; i++) {
if ((i % 3 == 0) && (i % 5 == 0)) {
console.log("FooBar");
}else if (i % 5 == 0) {
console.log("Bar");
} else if (i % 3 == 0) {
console.log("Foo");
} else {
console.log(i);
}
}
}
// Sachin
function fooBar(n){
for(i=1;i<=n;i++) {
if (i%3==0 && i%5==0) string = 'FooBar';
else if (i%3==0) string = 'Foo';
else if (i%5==0) string = 'Bar';
else string = i;
console.log(string);
}
}
//Raj Bhat
function foobar(n) {
if(n>0){
for(var i= 1; i<=n; i++) {
if((i%3== 0)&&(i%5==0)){
console.log("foobar");}
else if(i%5==0){
console.log("bar");}
else if(i%3==0){
console.log("foo");}
else{
console.log(i);
}
}
}
else{
console.log("invalid number");}
}
//Kuldip Mochi
function fooBar(n){
if(n>0){
for(let i=1;i<=n;i++) {
(i%3==0 && i%5==0) ? str = 'FooBar' : ((i%3==0) ? str = 'Foo' : ((i%5==0) ? str = 'Bar' : str = i))
console.log(str)
}
}
else{
console.log("Invalid numebr")
}
}
foobar(5);
//Anusha
function FooBar(n) {
if(n>0){
for (let i = 1; i <= n; i++) {
// console.log(i)
if (i % 3 == 0 && i % 5 == 0) {
console.log('foobar')
}
else if (i % 3 == 0) {
console.log('foo')
}
else if (i%5==0) {
console.log('bar')
}
else{
console.log(i)
}
}
}
}
console.log(FooBar(20))