Code Challenge Solutions: Nov 1, 2017

Here are my code solutions for last week. I was focused on Javascript (as I will be this week), so all code blocks are in Javascript.

Count the instances of a character in a string and return a character to a new string based on the result
function duplicateEncode(word) {
  var word = word.toLowerCase();
  var result = [];
  for (var i = 0; i < word.length; i++) {
    if ((word.split(word.charAt(i)).length - 1) > 1) {
      result.push(')')
    } else {
      result.push('(')
    }
  }
  return result.join("");
};
Check to see if a set of three numbers can make a triangle
function isTriangle(a,b,c)
{ if (a + b > c && a + c > b && b + c > a === true) {
 console.log(Boolean(a + b > c), Boolean(a + c > b), Boolean(b + c > a));
 return true;
 } else {
 return false;
 }
};
Loop through an array and return only the first instance of a sequentially repeated variable (variable can repeat non-sequentially)
var uniqueInOrder=function(iterable){
  var iterTemp = [];
  for (var i = 0; i < iterable.length; i++) {
    if(iterable[i] !== iterable[i+1]) {
    iterTemp.push(iterable[i]);
    } else {
    }
  }
  return iterTemp;
};
Square every digit of an integer
function squareDigits(num){
  var num = (""+num).split("");
  for(var i = 0; i < num.length; i++) {
    num[i] = Math.pow(num[i], 2);
  }
  return parseInt(num.join(""));
}

Leave a Reply

Your email address will not be published. Required fields are marked *