More Practice

Begin Programming with JavaScript More Practice

We’ve seen enough rules and concepts, but for a beginner to fully grasp all the fundamental concepts, more practice and problem solving are needed. Here’s a set of problems to improve your problem solving skills.

Searching for information online is an essential part of these exercises. Search as much as you need to.

Exercise 1

Write a function that accepts a string of numbers as parameter. It will return a list of only the odd numbers, as a string.

Things you might need:

  • The string method split
  • The array method join
  • The modulo operator % for checking whether a number is odd or even

Exercise 2

Write a function that accepts a string. It will return the same string with the first character capitalized.

Things you might need:

  • The string method split
  • The string method toUpperCase
  • The array method join

Exercise 3

Write a function that accepts a string. It will return only the words that are capitalized.

Things you might need:

  • The brackets [] can be used to get the first character of a string.
// e.g.
let name = "Jim";
let firstChar = name[0];

Exercise 4

There’s a string method called repeat that returns a string that gets repeated a number of times.

let str = "Hello";
let longStr = str.repeat(3);

unpack:

longStr will be HelloHelloHello

Try creating a function like that. Since it’s not a method, you would have to pass the str as a parameter to the function.

let str = "Hello";
let longStr = repeat(str, 3);

Things you might need:

  • Loop
  • The += operator

Exercise 5

Write a function that can create an abbreviation of a string.

Solutions

Solution for Exercise 1:
function getOddNums(input){
  let nums = input.split(" ");
  let oddNums = [];
  for(let i=0; i<nums.length; i++){
    let num = parseInt(nums[i]);
    if(num % 2 === 1){
      oddNums.push(num);
    }
  }
  let output = oddNums.join(" ");
  return output;
}

unpack:

The modulo operation num % 2 will return either 0 or 1. If num is odd, it will return 1, otherwise, 0.

join is used for converting the array into a string of numbers.

 

Alternate Solution for Exercise 1:
function getOddNums(input){
  let nums = input.split(" ");
  let oddNums = [];
  for(let i=0; i<nums.length; i++){
    let str = nums[i];
    let lastChar = str[str.length-1];
    if('13579'.indexOf(lastChar) > -1){
      oddNums.push(str);
    }
  }
  let output = oddNums.join(" ");
  return output;
}

unpack:

We didn’t convert each item into a number here. We’re using a different technique to check whether a string represents an odd number or not.

 

Solution for Exercise 2:
function capitalize(str){
  let chars = str.split("");
  chars[0] = chars[0].toUpperCase();
  let output = chars.join("");
  return output;
}

 

Alternate Solution for Exercise 2:
function capitalize(str){
  let firstChar = str.slice(0, 1);
  let otherChars = str.slice(1);
  let output = firstChar.toUpperCase() + otherChars;
  return output;
}

unpack:

Instead of breaking the string into an array of characters, we are splitting it into two parts, the firstChar and the otherChars.

 

Solution for Exercise 3:
function getNames(str){
  let words = str.split(" ");
  let output = [];
  for(let i=0; i<words.length; i++){
    let word = words[i];
    let firstChar = word[0];
    if(firstChar === firstChar.toUpperCase()){
      output.push(word);
    }
  }
  return output;
}

unpack:

The condition is comparing the first character of a word to the uppercase version of that same character. So if the character is uppercase to begin with, the comparison will be true. Basically an uppercase character should be equal to the uppercase version of itself, and on the other hand, a lowercase character should not be equal to the uppercase version of itself.

 

Alternate Solution for Exercise 3:

function getNames(str){
  let words = str.split(" ");
  let output = [];
  let reference = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  for(let i=0; i<words.length; i++){
    let word = words[i];
    let firstChar = word[0];
    if(reference.indexOf(firstChar) > -1){
      output.push(word);
    }
  }
  return output;
}

unpack:

Check the firstChar against all the uppercase characters stored in reference. indexOf will return a number between 0 to 25 if the character is one of the uppercase characters.

This technique is similar to the alternate solution of Exercise 1.

 

Solution for Exercise 4
function repeat(str, count){
  let output = "";
  for(let i=0; i<count; i++){
    output += str;
  }
  return output;
}

unpack:

output += str is a shorthand of writing output = output + str.

 

Alternate Solution for Exercise 4
function repeat(str, count){
  let output = "";
  let i=0;
  while(i<count){
    output += str;
    i++;
  }
  return output;
}

unpack:

Use a while loop instead of a for loop.

 

Solution for Exercise 5
function getAbbr(str){
  let words = str.split(" ");
  let output = "";
  for(let i=0; i<words.length; i++){
    let item = words[i];
    let initial = item[0];
    let capInitial = initial.toUpperCase();
    output += capInitial;
  }
  return output;
}

 

Alternate Solution for Exercise 5

Instead of going through the string word by word, we can do it character by character.

function getAbbr(str){
  let chars = str.split("");
  let output = chars[0].toUpperCase();
  while(chars.length > 0){
    let item = chars.shift();
    if(item===" "){
      let initial = chars.shift();
      let capInitial = initial.toUpperCase();
      output += capInitial;
    }
  }
  return output;
}

This is not any more efficient than the previous solution, but it’s using a bunch of new techniques, so it’s good for learning purposes.

This solution relies on the pattern that there’s always a space right before the initial character of a word, except for the first word.

So we can make use of this pattern by breaking the string into individual characters, use a loop to check each item to see if it’s a space. If an item is a space, the item next to that will be an initial.

We used an empty string instead of a space as the parameter for split. So the return value will be an array of characters instead of an array of words:

[ "a", "s", " ", "s", "o", "o", "n", " ", "a", "s", " ", "p", "o", "s", "s", "i", "b", "l", "e" ]

↓ The array method shift is used for taking out the first item of the array. This method returns the current first item, and as a result, the array will have one less item.

↓ We use shift to take out each character one by one. If the item is a space, we will use shift a second time in the same cycle to get the next item and add it to output.

And since shift is used inside a loop, eventually, the array will be empty and the while loop will stop.

↑ Because there’s no space before the initial character of the first word, we had to put the first character in the output variable right before the loop. Basically, the first character is an exception to the pattern, so we don’t rely on the loop to detect it.

Detecting patterns and exceptions is in essence what programming is about.