FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
JavaScript-Interview-Question/FizzBuzz.js at master · plopfizz/JavaScript-Interview-Question · GitHub
plopfizz
/
JavaScript-Interview-Question
Public
forked from
namitmalasi/JavaScript-Interview-Question
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
JavaScript-Interview-Question
/
FizzBuzz.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
42 lines (36 loc) · 823 Bytes
Breadcrumbs
JavaScript-Interview-Question
/
FizzBuzz.js
Copy path
File metadata and controls
42 lines (36 loc) · 823 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
// Approach 1(Naive)
var
fizzBuzz
=
function
(
n
)
{
let
resultArray
=
[
]
;
for
(
let
i
=
1
;
i
<=
n
;
i
++
)
{
if
(
i
%
3
===
0
&&
i
%
5
===
0
)
{
resultArray
[
i
-
1
]
=
"FizzBuzz"
;
}
else
if
(
i
%
5
===
0
)
{
resultArray
[
i
-
1
]
=
"Buzz"
;
}
else
if
(
i
%
3
===
0
)
{
resultArray
[
i
-
1
]
=
"Fizz"
;
}
else
{
resultArray
[
i
-
1
]
=
i
.
toString
(
)
;
}
}
return
resultArray
;
}
;
// Approach2(optimised)
let
fizzBuzz
=
(
n
)
=>
{
let
hashMap
=
new
Map
(
)
;
let
arr
=
[
]
;
hashMap
.
set
(
3
,
"Fizz"
)
;
hashMap
.
set
(
5
,
"Buzz"
)
;
for
(
let
i
=
1
;
i
<=
n
;
i
++
)
{
let
output
=
""
;
for
(
let
key
of
hashMap
.
keys
(
)
)
{
if
(
i
%
key
==
0
)
{
output
+=
hashMap
.
get
(
key
)
;
}
}
if
(
output
==
""
)
{
output
=
i
.
toString
(
)
;
}
arr
.
push
(
output
)
;
}
return
arr
;
}
;
Back
|
FazBrowse Home
|
New Git URL