Skip to content

Instantly share code, notes, and snippets.

View mojenmojen's full-sized avatar

mojen mojenmojen

View GitHub Profile
@mojenmojen
mojenmojen / .gitignore
Last active August 14, 2016 19:45 — forked from salcode/.gitignore
.gitignore file for WordPress - Bare Minimum Git
# -----------------------------------------------------------------
# .gitignore for WordPress
# Bare Minimum Git
# http://ironco.de/bare-minimum-git/
# ver 20150227
#
# This file is tailored for a WordPress project
# using the default directory structure
#
# This file specifies intentionally untracked files to ignore
@mojenmojen
mojenmojen / isogram.js
Created September 24, 2016 03:01
Codewars Kata: An isogram is a word that has no repeating letters, consecutive or non-consecutive. Implement a function that determines whether a string that contains only letters is an isogram. Assume the empty string is an isogram. Ignore letter case.
function isIsogram(str){
console.log( 'string: ' + str );
// length of the string
var stringLength = str.length;
console.log( 'stringLength: ' + stringLength );
// if the string is empty then it is considered an isogram
if ( stringLength == 0 ) {
@mojenmojen
mojenmojen / xo.js
Created September 24, 2016 03:26
Codewars Kata: Check to see if a string has the same amount of 'x's and 'o's. The method must return a boolean and be case insensitive. The string can contains any char.
function XO(str) {
// make the string lowercase because we are case insensitive
str = str.toLowerCase();
// put the string into an array
var arrayOfCharacters = str.split("");
//count the x's
var countX = arrayOfCharacters.reduce( function( n, val ) {
@mojenmojen
mojenmojen / findNb.js
Created September 24, 2016 03:40
Codewars Kata: Your task is to construct a building which will be a pile of n cubes. The cube at the bottom will have a volume of n^3, the cube above will have volume of (n-1)^3 and so on until the top which will have a volume of 1^3. You are given the total volume m of the building. Being given m can you find the number n of cubes you will have…
function findNb(m) {
// create an endless loop that increments n, the cube number, starting with a value of 1
for ( var n = 0;;n++ ) {
if ( m > 0 ) {
// if m, the total volume, is not 0, we will subtract the volume of the current cube from it
// first calculate the volume of the current cube
var currCubeVol = Math.pow( n+1, 3);
@mojenmojen
mojenmojen / calculateYears.js
Created September 24, 2016 03:51
Codewars Kata: Mr. Scrooge has a sum of money 'P' that wants to invest, and he wants to know how many years 'Y' this sum has to be kept in the bank in order for this sum of money to amount to 'D'. The sum is kept for 'Y' years in the bank where interest 'I' is paid yearly, and the new sum is re-invested yearly after paying tax 'T' Note that the …
function calculateYears(principal, interest, tax, desired) {
// create an endless loop that will increment the number of years
for ( var year = 0;; year++ ) {
// check if the principal has reached the desired amount
if ( principal >= desired ) {
return year;
}
@mojenmojen
mojenmojen / fizzbuzz.js
Last active December 17, 2023 21:55
Write a program that uses console.log to print all the numbers from 1 to 100, with two exceptions. For numbers divisible by 3, print "Fizz" instead of the number, and for numbers divisible by 5 (and not 3), print "Buzz" instead. When you have that working, modify your program to print "FizzBuzz", for numbers that are divisible by both 3 and 5 (a…
// make a loop that goes from 1 to 100
for ( var num = 1; num < 101; num ++ ) {
// check if the number is divisible by 3 or 5
var checkForThree = num % 3;
var checkForFive = num % 5;
// if the number is divisible by both 3 and 5, then print FizzBuzz
if ( (checkForThree == 0) && (checkForFive == 0) )
console.log( "FizzBuzz");
@mojenmojen
mojenmojen / grid.js
Created September 25, 2016 16:05
Write a program that creates a string that represents an 8×8 grid, using newline characters to separate lines. At each position of the grid there is either a space or a “#” character. The characters should form a chess board. Passing this string to console.log should show something like this: # # # # # # # # # # # # # # # # # # # # # # # # # # #…
// Set the grid size
var size = 8;
// set a variable for the grid output string
var gridOutput = "";
// set a variable for the row output string
var rowOutput = "";
// set variables for the things that make up the grid
@mojenmojen
mojenmojen / min.js
Created September 25, 2016 16:40
Write a function min that takes two arguments and returns their minimum.
// compare firstNum and secondNum and return the smaller of the two
function min( firstNum, secondNum ) {
if ( firstNum < secondNum )
return firstNum;
else
return secondNum;
}
console.log(min(0, 10));
// → 0
@mojenmojen
mojenmojen / iseven.js
Created September 25, 2016 16:59
We’ve seen that % (the remainder operator) can be used to test whether a number is even or odd by using % 2 to check whether it’s divisible by two. Here’s another way to define whether a positive whole number is even or odd: Zero is even. One is odd. For any other number N, its evenness is the same as N - 2. Define a recursive function isEven co…
// Recursive function to determine if a number is even
function isEven( checkNum ) {
// if the number is negative then convert it to positive
if ( checkNum < 0 )
checkNum *= -1;
// if the number is zero then it's even
if ( checkNum == 0 ) {
result = true;
@mojenmojen
mojenmojen / range.js
Created September 25, 2016 18:42
Write a range function that takes two arguments, start and end, and returns an array containing all the numbers from start up to (and including) end. Next, write a sum function that takes an array of numbers and returns the sum of these numbers. Run the previous program and see whether it does indeed return 55. As a bonus assignment, modify your…
// range function
function range( start, end, increment ) {
// create the result array
var result = [];
// test to see if we have an increment, otherwise set it to 1
if ( increment == undefined )
increment = 1;