Skip to content

Instantly share code, notes, and snippets.

@edr3x
Last active April 29, 2023 12:46
Show Gist options
  • Save edr3x/4673bf9202685bfcbdc0b9b1cf073bcc to your computer and use it in GitHub Desktop.
Save edr3x/4673bf9202685bfcbdc0b9b1cf073bcc to your computer and use it in GitHub Desktop.
fizzbuzz

JavaScript

function fizzBuzz(num) {
    for (let i = 1; i < num; ++i) {

        let out = "";

        if (i % 3 === 0)
            out += "Fizz";

        if (i % 5 === 0)
            out += "Buzz";

        console.log(out || i);
    }
}

fizzBuzz(50);

RUST

fn fizzbuzz(n: i64) -> Result<Vec<String>, Box<dyn std::error::Error>> {
    let mut out: Vec<String> = vec![];

    for i in 1..n {
        if i % 3 == 0 {
            out.push("Fizz".to_string());
        }

        if i % 5 == 0 {
            out.push("Buzz".to_string());
        }

        out.push(i.to_string());
    }

    Ok(out)
}

fn main() {
    let res = match fizzbuzz(100) {
        Ok(res) => res,
        Err(e) => panic!("Error: {}", e),
    };

    println!("{:?}", res);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment