Welcome! This the homework to accompany the Girl Develop It JavaScript 101 class. You can always reference the slides if you get stuck. Commit to spend at least 20 minutes trying to figure out a problem before you look at the answers. It helps you learn!

1. The Fortune Teller

Why pay a fortune teller when you can just program your fortune yourself?


var numKids  = 2;
var partner  = 'Jon Hamm';
var residence = 'Bora Bora';
var jobTitle = 'first class web developer';

var future = 'You will be a ' + jobTitle + ' in ' + residence + ', and married to ' + partner + ' with ' + numKids + ' kids.';

console.log(future);
				

2. The Calculator


function squareNumber(num) {
	var squaredNumber = num * num;
	console.log('The result of squaring the number ' + num + ' is ' + squaredNumber);
	return squaredNumber;
}

squareNumber(3);

function halfOf(num) {
	var half = num / 2;
	console.log('Half of ' + num + ' is ' +  half);
	return half;
}

halfOf(5);

function percentOf(num1, num2) {
	var percent = (num1/num2) * 100;
	console.log(num1 + ' is ' + percent + '% of ' + num2);
	return percent;
}

percentOf(5, 10);

function areaOfCircle(radius) {
	var area = Math.PI * squareNumber(radius);
	console.log('The area of circle with radius ' + radius + ' is ' + area);
	return area;
}

areaOfCircle(2);

function doCrazyStuff(num) {
	var half    = halfOf(num);
	var squared = squareNumber(half);
	var area    = areaOfCircle(squared);
	var result  = percentOf(squared, area);
}

doCrazyStuff(5);
				

3. What number is bigger?

Write a function that compares two numbers and returns the larger one. Be sure to try it our with some different numbers. Bonus: add error messages if the numbers are equal or cannot be compared.

Don't forget to test it!


function greaterNum(num1, num2) {
	if (num1 === num2) {
		return 'Those numbers are equal! They\'re both ' + num1;
	} else if (num1 > num2) {
		return num1;
	} else if (num2 > num1) {
		return num2;
	} else {
		return 'Those are simply incomparable!';
	}
}

console.log(greaterNum(3, 3));
console.log(greaterNum(7, -2));
console.log(greaterNum(5, 9));
console.log(greaterNum(5, 'dog'));
				

4. Pluralize

Write a function pluralize that takes in two arguments, a number and a word, and returns the plural. For example:

pluralize(5, 'cat'): '5 cats'
pluralize(7, 'turtle'): '7 cats'
			

Bonus: Make it handle a few irregular plural nouns like "deer" and "geese".


function pluralize(number, noun) {
	if ( noun === 'goose' && number != 1) {
		return number + ' ' + 'geese';
	} else if ( noun === 'mouse' && number != 1) {
		return number + ' ' + 'mice';
	} else if (number != 1 && noun != 'sheep' && noun != 'deer' && noun != 'moose') {
		return number + ' ' + noun + 's';
	} else {
		return number + ' ' + noun;
	}
}
console.log('I have ' + pluralize(0, 'cat'));
console.log('I have ' + pluralize(1, 'cat'));
console.log('I have ' + pluralize(2, 'mouse'));
console.log('I have ' + pluralize(6, 'goose'));
console.log('I have ' + pluralize(3, 'moose'));
console.log('I have ' + pluralize(7, 'deer'));
				

5. Even/Odd Counter

Write a for loop that will iterate from 0 to 20. For each iteration, it will check if the current number is even or odd, and report that to the screen (e.g. "2 is even")


for (var i = 0; i <= 20; i++) {
	if (i % 2 === 0) {
		console.log(i + ' is even');
	} else {
		console.log(i + ' is odd');
	}
}
				

6. Top Choice

Create an array to hold your top choices (colors, presidents, whatever). For each choice, log to the screen a string like: "My #1 choice is blue." Bonus: Change it to log "My 1st choice, "My 2nd choice", "My 3rd choice", picking the right suffix for the number based on what it is.


var choices = ['red', 'orange', 'pink', 'yellow'];
for (var i = 0; i < choices.length; i++) {
	console.log('My #' + (i + 1) + ' choice is ' + choices[i]);
}

for (var i = 0; i < choices.length; i++) {
	var choiceNum = i + 1;
	var choiceNumSuffix;
	if (choiceNum == 1) {
		choiceNumSuffix = 'st';
	} else if (choiceNum == 2) {
		choiceNumSuffix = 'nd';
	} else if (choiceNum == 3) {
		choiceNumSuffix = 'rd';
	} else {
		choiceNumSuffix = 'th';
	}
	console.log('My ' + choiceNum + choiceNumSuffix + ' choice is ' + choices[i]);
}
				

Challenge Question!: MixUp

Create a function called mixUp. It should take in two strings, and return the concatenation of the two strings (separated by a space) slicing out and swapping the first 2 characters of each. You can assume that the strings are at least 2 characters long. For example:

mixUp('mix', pod'): 'pox mid'
mixUp('dog', 'dinner'): 'dig donner'
			

Hint: You'll need to use some of the built-in JavaScript methods for strings. Methods are pre-written functions that are built into the language.


//This function uses the slice() method. It extracts a part of a string and returns a new string
function mixUp(string1, string2) {
  return string1.slice(0, 2) + string2.slice(2) + " " + string2.slice(0, 2) + string1.slice(2);
}

console.log(mixUp('dogs', 'cats'));