Codewars

These days I was playing with codewars and I am going to continue my participation on this site for many reasons,

but the most valid reason is because you sharpen your programming skills with funny way!



So I will navigate you through some of my completed katas that I solved the days that past.



Let's see some katas:

I will write the specification first and after the solution.

Lets start!

Write a method smash that takes an array of words and smashes them together into a sentence and returns the sentence. You can ignore any need to sanitize words or add punctuation, but you should add spaces between each word. Be careful, there shouldn't be a space at the beginning or the end of the sentence!


function smash (words) {
 "use strict";
 return words.join(' ');     
};
Your task is to sort a given string. Each word in the String will contain a single number. This number is the position the word should have in the result.

Note: Numbers can be from 1 to 9. So 1 will be the first word (not 0).

If the input String is empty, return an empty String. The words in the input String will only contain valid consecutive numbers.

For an input: "is2 Thi1s T4est 3a" the function should return "Thi1s is2 3a T4est"

function order(words){
  //sort a given string
  //each word of str will contain a number
  //the number is theposition
  //number from one to nine
  //if input str is empty return empty string
 
 //find the index of numbers

 var arr = words.split(' ');
  var compare = function(a, b) {
      if (Number(a.match(/\d+/)[0]) < Number(b.match(/\d+/)[0])) {
        return -1;
        }
      if (Number(a.match(/\d+/)[0]) > Number(b.match(/\d+/)[0])) {
          return 1;
      }

      // a must be equal to b
      return 0;
  }
  arr.sort(compare);
  return arr.join(' ');
}
Check to see if a string has the same amount of 'x's and 'o's. The method must return a boolean and be case insensitive. The string can contains any char.

function XO(str) {
  //code here
  var strConv =  str.toLowerCase();
  var x1 = strConv.split('x').length - 1
  var y1 = strConv.split('o').length - 1

  if (x1 === y1) {
    return true;
  } else {
    return false;
  }
}
Write function avg which calculates average of numbers in given list.

function find_average(array) {
 var total = 0;
 for(var i = 0; i < array.length; i++) {
    total += array[i];
    }
    var avg = total / array.length;
    return avg;
 }

Σχόλια