Code Challenge: Consecutive

Each week, I offer up a JavaScript code challenge. Want more? You can find others here.

This week’s code challenge comes again from Coderbyte:

Using the JavaScript language, have the function Consecutive(arr) take the array of integers stored in arr and return the minimum number of integers needed to make the contents of arr consecutive from the lowest number to the highest number. For example: If arr contains [4, 8, 6] then the output should be 2 because two numbers need to be added to the array (5 and 7) to make it a consecutive array of numbers from 4 to 8. Negative numbers may be entered as parameters and no array will have less than 2 elements.

Continue reading

Code Challenge: Additive Persistence

Each week, I offer up a JavaScript code challenge. Want more? You can find others here.

First, I’ll admit that I’m a day overdue on this code challenge. Last week, I was on vacation in Austin, TX, and I’m still playing catchup. Still, that’s no excuse! After being away for a week, I felt a bit rusty. Today’s code challenge is a shorter one from Coderbyte.

Using the JavaScript language, have the function AdditivePersistence(num) take the num parameter being passed which will always be a positive integer and return its additive persistence which is the number of times you must add the digits in num until you reach a single digit. For example: if num is2718 then your program should return 2 because 2 + 7 + 1 + 8 = 18 and 1 + 8 = 9 and you stop at 9.

Here’s my solution collapsed:

[code language=”javascript” collapse=”true”]
function AdditivePersistence(num) {
// Break up the numbers into an array
var numbers = num.toString().split(”);
// Set the initial count to 0
var count = 0;

// Return the count if num is a single number
if (numbers.length <= 1) {
return count;

// If num isn’t a single number
do {
// Set the initial sum to 0
var sum = 0;
// Loop over the numbers array and add each number to the sum
for (var i = 0; i<numbers.length; i++) {
sum = sum + Number(numbers[i]);
// Set numbers equal to the new array after splitting the sum
numbers = sum.toString().split(”);
// Increment the count variable
count = count + 1;
// Do this while numbers.length is larger than 1
} while (numbers.length > 1);

return count;

First, I took the num argument passed in and split it up into an array so I could cycle over it. If the array had a length of 1, I could just return 0.

If the length of the array was larger than 1, I started a do/while loop. I cycled over each number in the array with a for loop and added it to the sum variable. At the end of the for loop, I set the numbers variable equal to the sum broken up into an array. For example, if the initial number was 2345, the sum would be 14. So, the numbers variable would be set equal to [1,4]. I repeated this process while numbers.length was greater than 1. Finally, I returned the count, which was incremented each time I went through the do/while loop.

Code Challenge: Number Search

Each week, I offer up a JavaScript code challenge. Want more? You can find others here.

This week’s code challenge again comes from Coderbyte. It’s quite a bit easier and shorter than last week mainly because I was slammed with Drink for Pink events and prepping for the Automattic Grand Meetup!

Here’s the gist:

Using the JavaScript language, have the function NumberSearch(str) take the str parameter, search for all the numbers in the string, add them together, then return that final number divided by the total amount of letters in the string. For example: if str is “Hello6 9World 2, Nic8e D7ay!” the output should be 2. First if you add up all the numbers, 6 + 9 + 2 + 8 + 7 you get 32. Then there are 17 letters in the string. 32 / 17 = 1.882, and the final answer should be rounded to the nearest whole number, so the answer is 2. Only single digit numbers separated by spaces will be used throughout the whole string (So this won’t ever be the case: hello44444 world). Each string will also have at least one letter.

Here’s a test case for example:

Input = “H3ello9-9”
Expected Output = 4

The string “H3ello9-9” contains 6 items that aren’t numbers. If you sum up the numbers (9,9,3), you’ll get 21. If you divide that by 6, you get 3.5, which rounds to 4.

Here’s another test case:

Input = “One Number1
Expected Output = 0

Here’s my solution collapsed:

[code language=”javascript” collapse=”true”]
function numberSearch(str) {
var separatedString = str.split(”);
var letters = [];
var numbers = [];
var sum = 0;

for (var i=0; i<str.length; i++) {
var currentItem = separatedString[i];
if (isNaN(parseInt(currentItem))) {
} else {

for (var i=0; i<numbers.length; i++) {
currentNumber = Number(numbers[i]);
sum = sum + currentNumber;

return (Math.round(sum/letters.length));

First, we create some variables we’ll need later on. The first (separatedString) breaks up the given string. The variables letters and numbers hold empty arrays. Then, we set the sum to 0.

Next, we cycle over the string determining whether or not currentItem is a number by using isNaN(parseInt(currentItem)). If the item isn’t a number, push it on to the letters array. If it is a number, push it on to the numbers array.

Next, we cycle over the numbers array and add the currentNumber to the sum total.

Finally, we use the Math.round() method to round up the result of sum divided by letters.length and return that number.

Code Challenge: Blackjack

Each week, I offer up a JavaScript code challenge. Want more? You can find others here.

This week’s challenge comes from Coderbyte, which has been a fun resource. The instructions are a bit of a doozy:

Have the function BlackjackHighest(strArr) take the strArr parameter being passed which will be an array of numbers and letters representing blackjack cards. Numbers in the array will be written out. So for example strArr may be [“two”,”three”,”ace”,”king”]. The full list of possibilities for strArr is: two, three, four, five, six, seven, eight, nine, ten, jack, queen, king, ace. Your program should output below, above, or blackjack signifying if you have blackjack (numbers add up to 21) or not and the highest card in your hand in relation to whether or not you have blackjack. If the array contains an ace but your hand will go above 21, you must count the ace as a 1. You must always try and stay below the 21 mark. So using the array mentioned above, the output should be below king. The ace is counted as a 1 in this example because if it wasn’t you would be above the 21 mark. Another example would be if strArr was [“four”,”ten”,”king”], the output here should be above king. If you have a tie between a ten and a face card in your hand, return the face card as the “highest card”. If you have multiple face cards, the order of importance is jack, queen, then king.

It sounds a bit confusing at first glance, but if you know how to play blackjack, the rules make sense, particularly the rules surrounding aces. Here are some test cases:

  • Input = “four”,”ace”,”ten”; Output = “below ten”
  • Input = “ace”,”queen”; Output = “blackjack ace”

Continue reading

Code Challenge: String Reduction

Each week, I offer up a JavaScript code challenge. Want more? You can find others here.

This week’s code challenge comes from Coderbyte. Here are the basic instructions:

Have the function `StringReduction(str)` take the `str` parameter being passed and return the smallest number you can get through the following reduction method. The method is: Only the letters a, b, and c will be given in str and you must take two different adjacent characters and replace it with the third.

For example, if you were provided the string “abc,” you could simplify that to “cc” but no further. On the other hand, “aba” could be simplified to “ca” and then to “b.” For simplification, here are all of the transformations:

ac = b
ca = b
cb = a
bc = a
ab = c
ba = c

Here are some test cases:

StringReduction(“abcabc”) should equal 2.
StringReduction(“cccc”) should equal 4.
StringReduction(“baccabcbc”) should equal 1.

Here’s my solution collapsed:

Continue reading

Code Challenge: Recurring Letters

This is the fourth post in a series of at least 10 JavaScript code challenges. You can find the others here.

This one was a bit of a doozy! I originally read this challenge three weeks ago and gave it a try. I couldn’t get it working so I moved on to another after an hour of frustration. Reflecting back on the “Second Greatest Length” challenge, I had a little epiphany of how it could work.

Challenge: Given a string, the function highestRecurringLetter should return the word in that string that contains the highest recurring letter. For example, if given “Trees are great,” the function should return “Trees” because the letter ‘e’ occurs twice. If no letter occurs more than once in a word (“This is fun”), the function should return the last word in the string. Assume that the string has at least one word, and words are separated by spaces.

Test cases

highestRecurringLetter("Soccer is fun") = ‘Soccer’;
highestRecurringLetter("Recurring challenges are fun") = ‘Challenges’;
highestRecurringLetter("I love them") = ‘Them’;

Here’s my solution minimized. I want to see yours though. If you give this a shot, let me know and post a link to your solution in the comments! Otherwise, just let me know what I could’ve done better!

Continue reading

Code Challenge: Reverse, Reverse!

This is the third post in a series of at least 10 code challenges. You can find the others here.

Compared to the previous two challenges, today’s challenge was short and straightforward.

Challenge: Given a string, the function reverseString should return the same string backwards. For example, reverseString(“This string”) would return “gnirts siht.” When writing your function, assume the message contains only letters and spaces, and all words are separated by one space.

Here’s my solution collapsed:

Continue reading

Code Challenge: Find the second greatest length

This is the second post in a series of at least 10 code challenges. You can find the others here.

Challenge: Given an array of at least three strings, the function secondGreatest should return the second highest length value. For example, if you were given [“Three “, “Four “, “Fifteen”], the function should return “Five,” which is the length of the second longest item in the array (“Fifteen” would be the longest). Given an array of [“One”, “Two”, “Ten”, “Eleven”], the function should return “Three.” The array will always consist of at least three items.

Continue reading

Code Challenge: What’s Wrong With This Game?

Since the start of 2015, I’ve been working on some JavaScript courses on Treehouse and elsewhere. In an effort to improve, I’m going to do a JavaScript code challenge every Monday for at least 10 weeks. Here’s the first one. I would love feedback in the comments!

Here is a link to the original problem on Interview Cake. In short, you have the following code. The syntax is correct, but the behavior is off.

[code language=”javascript”]
<button id="btn-0">Button 1!</button>
<button id="btn-1">Button 2!</button>
<button id="btn-2">Button 3!</button>

<script type="text/javascript">
var prizes = [‘A Unicorn!’, ‘A Hug!’, ‘Fresh Laundry!’];
for (var btnNum = 0; btnNum < prizes.length; btnNum++) {
// for each of our buttons, when the user clicks it…
document.getElementById(‘btn-‘ + btnNum).onclick = function() {
// tell her what she’s won!

It didn’t take me long to figure out the actual issue, but the solution was a different story. It eluded me for several minutes. Here’s my solution minimized.

Continue reading