Friday, March 17, 2023

Coding Challenge #24

// from https://www.codecademy.com/resources/blog/20-code-challenges/
// Printing numbers 1 through 100 (inclusive), but for multiples of 3, we will print "Fizz" instead. If it's a multiple of 5: "Buzz". If it's a multiple of both 3 and 5: "FizzBuzz".
// We will detect the multiples of 3 and/or 5 by seeing if the quotient of the current number divided by three changes when floored (to determine whether it went into it fully, producing an integer output).
function count(){
    var o = "";
    for(var xint = 1;xint < 101;xint++){
        if(xint / 3 == Math.floor(xint / 3)
        && xint / 5 == Math.floor(xint / 5)) o += "FizzBuzz\n"; // both 3 & 5
        else if(xint / 3 == Math.floor(xint / 3)) o += "Fizz\n"; // 3
        else if(xint / 5 == Math.floor(xint / 5)) o += "Buzz\n"; // 5
        else o += xint + "\n";
    }
    console.log(o);
}

No comments:

Post a Comment

Coding Challenge #54 C++ int to std::string (no stringstream or to_string())

Gets a string from an integer (ejemplo gratis: 123 -> "123") Wanted to come up with my own function for this like 10 years ago ...