Silver Challenge Solutions

import Cocoa

for i in 1…100 {

if i % 3 == 0 && i % 5 == 0 {
    print("FIZZ BUZZ")
    continue
}

if i % 3 == 0 {
    print("FIZZ")
    continue
}

if i % 5 == 0 {
    print("BUZZ")
    continue

} if else {
print (i)
}
}

This is what I came up with but I cannot figure out what’s wrong with this code… help!

“if else” doesn’t make any sense. Change that to “else”.

Jim, never do performance in a playground. They’re not optimized for that and more than likely will give you false positives. You really need to do it in a dedicated program, and set it to run a few 100K times. I can help you walk through how to do that if you want.

this i a good answer, maybe l’m smart too… but …

import Cocoa
var count = 1;
for _ in 1 … 15 {
count += 1 ;
if count % 3 == 0 && count % 5 == 0 {
print(“fizz buzz”);
}else if count % 3 == 0 {
print (“fizz”);
}else if count % 5 == 0{
print(“buzz”);
}else{
print(count);
}
}

Bit late to the game with my switch statement solution:

for i in 0...100 {
    
    let fizz = i % 3
    let buzz = i % 5
    let divTest = (fizz, buzz)
    
    switch divTest {
    case (0, 0):
        print ("i = \(i) - fizz buzz")
        
    case (0, _):
        print ("i = \(i) - fizz")
        
    case(_, 0):
        print ("i = \(i) - buzz")
        
    default:
        print("i = \(i)")
    }
    
}

I have to be honest here the switch solution took some time as i was confused of how to make the tuple matching condition , i like these challenges as they are very good to test basic understanding that we take for granted many times –

Solution with if-else

for i in 1...100
{
    if i % 3 == 0 && i % 5 == 0
    {
        print("FizzBuzz")
    }

    else if i % 3 == 0
    {
        print("Fizz")
    }

        else if i % 5 == 0
        {
            print("Buzz")
        }



    else
    {
        print(i)
    }
}

Solution with switch –

var i: Int = 1

while i <= 100
{
    let tupleTest = (i % 3, i % 5)
    switch tupleTest
    {
    case (0, 0):
        print("FizzBuzz")
        
    case (0, _):
        print("Fizz")
    case (_, 0):
        print("Buzz")
    default:
        print(i)
    }
    
    i += 1
}