javascript

FizzBuzz HackerRank Challenge

Solve the fizzbuzz hackerrank challenge question:

Given a number n, for each integer i in the range from 1 to n inclusive, print out one value per line as follows:

  • if i is a multiple of both 3 and 5, print FizzBuzz
  • if i is a multiple of 3(but not 5), print Fizz
  • if i is a multiple of 5(but not 3), print Buzz
  • if i is not a multiple of 3 or 5, print the value of i

Java Solution

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;



class Result {

    /*
     * Complete the 'fizzBuzz' function below.
     *
     * The function accepts INTEGER n as parameter.
     */
    public static void fizzBuzz(int n) {
        for(int i=1; i<=n; i++){
            if(i%3 == 0 && i%5 == 0){
                System.out.println("FizzBuzz");
            }else if(i%3 == 0){
                System.out.println("Fizz");
            }else if(i%5 == 0){
                System.out.println("Buzz");
            }else{
                System.out.println(i);
            }
        }
    }

}

public class Solution {
    public static void main(String[] args) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));

        int n = Integer.parseInt(bufferedReader.readLine().trim());

        Result.fizzBuzz(n);

        bufferedReader.close();
    }
}

JavaScript Fizzbuzz Solution

'use strict';

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', function(inputStdin) {
    inputString += inputStdin;
});

process.stdin.on('end', function() {
    inputString = inputString.split('\n');

    main();
});

function readLine() {
    return inputString[currentLine++];
}



/*
 * Complete the 'fizzBuzz' function below.
 *
 * The function accepts INTEGER n as parameter.
 */
function fizzBuzz(n) {
    // Write your code here
    for(let i=1; i<=n; i++){
            if(i%3 == 0 && i%5 == 0){
                console.log("FizzBuzz");
            }else if(i%3 == 0){
                console.log("Fizz");
            }else if(i%5 == 0){
                console.log("Buzz");
            }else{
                console.log(i);
            }
        }

}

function main() {
    const n = parseInt(readLine().trim(), 10);

    fizzBuzz(n);
}

Earn Quiz Instant Certificates Here

codefussion quiz certificate

Input(stdin):

15

Output (stdout):

1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz

This is how you solve the fizzbuzz hackerrank challenge in both Java and JavaScript.

HackerRank is a platform that offers online coding challenges to test and improve programming skills.

It provides a variety of programming challenges that cover different topics such as algorithms, data structures, math, SQL, and more. These challenges can be solved in a variety of programming languages, including Java, Python, C++, and more.

The challenges on HackerRank come in different levels of difficulty, ranging from beginner to advanced. The platform offers a gamified approach to learning programming, allowing users to earn points, badges, and even compete with other users in coding contests.

If you are looking to learn software development or a competent software development training institute? Codeflare offers unique software development training programs and courses in Abuja.

Download the Codeflare Mobile App and start learning how to code at your own pace.

Recent Posts

Instagram Extends Reels Duration to 3 Minutes

Regardless of whether TikTok faces a U.S. ban, Instagram is wasting no time positioning itself…

2 days ago

AWS Expands Payment Options for Nigerian Customers, Introducing Naira (NGN) for Local Transactions

Amazon Web Services (AWS) continues to enhance its customer experience by offering more flexible payment…

6 days ago

Why JavaScript Remains Dominant in 2025

JavaScript, often hailed as the "language of the web," continues to dominate the programming landscape…

7 days ago

Amazon Moves to Upgrade Alexa with Generative AI Technology

Amazon is accelerating efforts to reinvent Alexa as a generative AI-powered “agent” capable of performing…

1 week ago

Smuggled Starlink Devices Allegedly Used to Bypass India’s Internet Shutdown

SpaceX's satellite-based Starlink, which is currently unlicensed for use in India, is reportedly being utilized…

1 week ago

Why Netflix Dumped React For its Frontend

Netflix, a pioneer in the streaming industry, has always been at the forefront of adopting…

1 week ago