Skip to content

Instantly share code, notes, and snippets.

@ilhamsj
Last active August 14, 2021 13:56
Show Gist options
  • Save ilhamsj/b8b453166da52bcb101462e68845541d to your computer and use it in GitHub Desktop.
Save ilhamsj/b8b453166da52bcb101462e68845541d to your computer and use it in GitHub Desktop.

SECTION 1

Write a program that prints the numbers from 1 to 100. But for multiples of three print "Fizz" instead of the number and for the multiples of five print "Buzz". For numbers which are multiples of both three and five print "FizzBuzz".

"""
x mod 3 = Fizz
x mod 5 = Buzz
x mod 15 = FizzBuzz
else print x
"""

for x in range(1,101):
    if x % 15 == 0:
        print "FizzBuzz" # Catch multiples of both first.
    elif x % 3 == 0:
        print "Fizz"
    elif x % 5 == 0:
        print "Buzz"
    else:
        print x
        

SECTION 1

Q1/5. Write a function that takes in an integer as input and returns a boolean. Returns true when the said integer is an odd number.

/*
Habis di bagi 2 = even -> ex : 2, 4, 6
odd -> 1,3,5,7
*/

function oddEvenNumberChecker(number) {
    
    // habis di bagi 2
    if(number % 2 === 0)
    {
        return false;
    }  else {
        return true;
    }
}


oddEvenNumberChecker(10);
oddEvenNumberChecker(11);

SECTION 2

1/5. Write a function that takes in an integer as input and returns a boolean. Returns true when the said integer is an odd number.

const palindromChecker = (param) => {
    var words, text = '';

    if (typeof param === "string") {
        words = param.split('');
        text = param.toLowerCase();
    } else if (typeof param === "object") {
        words = param;
        text = param.join('').toLowerCase();
    }

    var palindrom = words.reverse().join('');

    if (text === palindrom.toLowerCase()) {
        console.log(`${palindrom} is palindrom`);
        return palindrom;
    }
}

palindromChecker('revIveR');
palindromChecker(['L', 'e', 'v', 'e', 'l']);
palindromChecker(['L', 'e', 'v', 'e', 'l', 'i', 'n', 'g']);

SECTION 3

Q3/5. Given an unsorted array of integers, how do you find the largest and smallest number?

/*
SORT ASC
SMALLEST = first
LARGEST = last
*/

const smallLargestChecker = (number) => {
    var ascending = number.sort((a, b) => a - b);

    var smallest = ascending[0];
    var largest = ascending[ascending.length - 1];

    console.log(`smallest = ${smallest}`);
    console.log(`largest = ${largest}`);

    return {
        smallest: smallest,
        largest: largest,
    }
}


smallLargestChecker([10, 4, 8, 3, 2000, 100]);

Q4/5. List all the sorts algorithm that you can think off (minimum 4), then do a time-complexity analysis on each methods.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment