Photo of a worn house with random colored numbers hand-painted on the side

My first JavaScript program!

Well, that’s not entirely accurate. I’ve written scripts before, but they’ve all been fairly specific in their scope (hiding DOM elements on a particular page for my convenience, for example), and it’s been years. So I’m basically starting over.

In today’s exploration, I came across this list of questions a front-end developer might reasonably be expected to answer in an interview, so I decided to start working through them. The first problem is to develop a function which checks whether an inputted number is prime. All things considered, it’s a pretty basic problem, but it’s been a long time since I even thought about prime numbers, so it took some minor mental gymnastics to get to the solution. Here’s my code:

function isPrime(number) {
	var remainder;
	if (number <= 1) {
		console.log("Number must be greater than 1!");
		return false;
	}
	for (var i = 2; i < number; i++) {
		remainder = number%i;
		//console.log("Dividing " + number + " by " + i)
		if (!remainder) {
			//console.log(i + " is a factor! " + number + " is not prime. Exiting!");
			return false;
		}
		/*else {
			console.log("The remainder of " + number + " divided by " + i + " is " + remainder);
		}*/
	}
	//console.log("Looks like " + number + " is prime! Congrats!");
	return true;
}

console.log(isPrime(0)); //should return false
console.log(isPrime(1)); //should return false
console.log(isPrime(17)); //should return true
console.log(isPrime(10000000000000)); //should return false

As you can see, it’s primitive and anything but optimal. I doubt a prospective employer would be impressed by my ingenuity, but it’s a starting point nonetheless.

Before beginning, I had to understand pretty well what a prime number was. I now understand that it is any number, greater than 1 (a stipulation I initially overlooked), which is not divisible by any number other 1 or itself. Before jumping into coding, I wrote out some pseudocode in an attempt to guide my thinking. It went something like this:

  • Accept number as input parameter
  • Loop iterator from 1 through (number-1)
    • If remainder of number divided by iterator is 0, return false
  • Else, return true

To put this into action, I declare function isPrime with a single parameter, number. I then declare a variable, remainder, which I’ll use shortly. The next line checks whether number is less than or equal to 1, logging a warning to the console and returning false if this condition is true. If the number passes this test, I then proceed into a for loop. It is here that I realized I’d made an error in my pseudocode; rather than starting with 1, I needed to start with 2! So I set a for loop to run with iterator, i, from i=2 until i<number, incrementing by 1 at the end of each pass.

At the beginning of each pass, I divide number (the input) by i using the modulo operator, storing the modulus as the remainder (though I learned while typing up this blog post, that modulus and remainder areĀ not the same thing…for the purposes of this example, I’ll go ahead and use them interchangeably). If remainder is zero, that means that we’ve found ourselves a factor which divides evenly into number, so number is not prime – thus the function returns false. If remainder is not zero, the loop continues, checking the remainder with each pass until the condition is met. If, during the execution of the loop, false is not returned, then the number must be prime, thus true is returned.

I called the function against four test cases mentioned in the website – 0, 1, 17, andĀ 10000000000000 – expecting the function to return false, false, true, and false, respectively. And my program passed the test each time! It’s a minor victory, but I’m proud to have solved the problem without cheating or asking for external help (save for some cursory research into the concept of prime numbers to make sure I had a clear understanding).

If I were to improve this program, I would probably remove the console.log statements I put in initially to debug. I would also like to figure out how to write the results to the barebones HTML document I included the script in to run it. As it stands right now, the results are posted only to the JavaScript console in my browser, but not to the page itself. I intend to figure out how to make this happen soon. As for now, it’s onto the next problem.

Advertisements

4 thoughts on “My first JavaScript program!

  1. What a fantastic list and article. I like the idea of the working through a set of real-life problems (or at least questions that you may get asked in real life!) and finding a solution at whatever level. I shall attempt to do the same, although my code knowledge is quite minimal at the moment. Nice one.

    Liked by 1 person

    1. How exciting to get feedback on my post! I didn’t think anyone would read these, so it’s nice to know I’m not just shouting into the void. Thanks for your feedback, and I’m glad you enjoyed reading my words! Good luck on your own journey!

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s