As I did on my previous post I will continue to show you some of my solution on various kata that I solved. Let's start:
Count cubes in a Menger Sponge
Task
In this kata you will create a function that takes non negative integers (from 0 to n) and return the amount of cubes that the Menger Sponge would have in that specific iteration
Example
calc_ms(0) == 1
calc_ms(1) == 20
calc_ms(2) == 400
calc_ms(3) == 8000
calc_ms(4) == 160000
calc_ms(5) == 3200000
calc_ms(6) == 64000000
My solution was an online solution:
function calc_ms(n) {
return Math.pow(20,n)
}
Remove First and Last Character
Task
It's pretty straightforward. Your goal is to create a function that removes the first and last characters of a string.
My solution:
function removeChar(str){
if(str.length<=2){ return str}
else{
var sub = str.slice(1, -1);
return sub;
};
};
Count the smiley faces!
Task
Given an array (arr) as an argument complete the function countSmileys that should return the total number of smiling faces.
Valid smiley face examples:
:) :D ;-D :~)
Invalid smiley faces:
;( :> :} :]
function countSmileys(faces) {
if (faces.length === 0) return 0;
var smiles = faces.filter(function(face) {
var parts = face.split('');
var hasEyes = parts[0].includes(':') || parts[0].includes(';');
var hasNose = parts[1].includes('-') || parts[1].includes('~');
var hasSmile = function hasSmile(pos) {
return parts[pos].includes(')') || parts[pos].includes('D');
};
if (parts.length === 2) {
if (hasEyes && hasSmile(1)) return face;
}
if (parts.length === 3) {
if (hasEyes && hasNose && hasSmile(2)) return face;
}
});
return smiles.length;
}
Grasshopper - Terminal game move function
Task
In this game, the hero moves from left to right. The player rolls the dice and moves the number of spaces indicated by the dice two times.
Create a function for the terminal game that takes the current position of the hero and the roll (1-6) and return the new position.
My solution:
function move (position, roll) {
// return the new position
return (roll * 2) + position
}
Write a function called whatday() which returns the day according to the number
1 returns 'Sunday', 2 returns 'Monday' etc
Return the day
Task
Write a function called whatday() which returns the day according to the number
1 returns 'Sunday', 2 returns 'Monday' etc...
My solution:
function whatday(num) {
var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
if(num >= 1 && num <= 7){
return days[num - 1 ]
}
else{
return 'Wrong, please enter a number between 1 and 7'
}
}
Is my string repeating the same character over and over ?
Task
The title is self explanatory, write a function hasOneChar returning:
true if the given string contains the same character repeated all along the string false otherwise.
My solution:
function hasOneChar(s) {
var pattern = new RegExp(/^(.)\1{0,}$/);
var testString = pattern.test(s);
return testString ;
}
Changing letters
Task
When provided with a String, capitalize all vowels
My solution:
function swap(st){
s =st.replace(/[aeiou]/gi, function(x){return x.toUpperCase();})
return s
}
Moving Zeros To The End
Task
Write an algorithm that takes an array and moves all of the zeros to the end, preserving the order of the other elements.
My solution
var moveZeros = function(arr) {
//algorithm that takes an array and moves the zero
//so it searches for zeros
//if a zero exists then push it so the zero goes to the end
//and after it deletes the zeros from the existed position
for (var i = arr.length; i--;) {
if (arr[i] === 0) {
arr.splice(i, 1);
arr.push(0)
}
}
return arr;
};
Alphabetti Spaghetti
Task
You will be given a string of letters. The string must be returned in alphabetical order.
function alphabetti(str) {
return str.split('').sort(caseInsensitiveSort).join('');
function caseInsensitiveSort(a, b) {
var a = a.toLowerCase(),
b = b.toLowerCase();
return ret1 = a > b ? true : false;
}
}
Count cubes in a Menger Sponge
Task
In this kata you will create a function that takes non negative integers (from 0 to n) and return the amount of cubes that the Menger Sponge would have in that specific iteration
Example
calc_ms(0) == 1
calc_ms(1) == 20
calc_ms(2) == 400
calc_ms(3) == 8000
calc_ms(4) == 160000
calc_ms(5) == 3200000
calc_ms(6) == 64000000
My solution was an online solution:
function calc_ms(n) {
return Math.pow(20,n)
}
Remove First and Last Character
Task
It's pretty straightforward. Your goal is to create a function that removes the first and last characters of a string.
My solution:
function removeChar(str){
if(str.length<=2){ return str}
else{
var sub = str.slice(1, -1);
return sub;
};
};
Count the smiley faces!
Task
Given an array (arr) as an argument complete the function countSmileys that should return the total number of smiling faces.
Valid smiley face examples:
:) :D ;-D :~)
Invalid smiley faces:
;( :> :} :]
function countSmileys(faces) {
if (faces.length === 0) return 0;
var smiles = faces.filter(function(face) {
var parts = face.split('');
var hasEyes = parts[0].includes(':') || parts[0].includes(';');
var hasNose = parts[1].includes('-') || parts[1].includes('~');
var hasSmile = function hasSmile(pos) {
return parts[pos].includes(')') || parts[pos].includes('D');
};
if (parts.length === 2) {
if (hasEyes && hasSmile(1)) return face;
}
if (parts.length === 3) {
if (hasEyes && hasNose && hasSmile(2)) return face;
}
});
return smiles.length;
}
Grasshopper - Terminal game move function
Task
In this game, the hero moves from left to right. The player rolls the dice and moves the number of spaces indicated by the dice two times.
Create a function for the terminal game that takes the current position of the hero and the roll (1-6) and return the new position.
My solution:
function move (position, roll) {
// return the new position
return (roll * 2) + position
}
Write a function called whatday() which returns the day according to the number
1 returns 'Sunday', 2 returns 'Monday' etc
Return the day
Task
Write a function called whatday() which returns the day according to the number
1 returns 'Sunday', 2 returns 'Monday' etc...
My solution:
function whatday(num) {
var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
if(num >= 1 && num <= 7){
return days[num - 1 ]
}
else{
return 'Wrong, please enter a number between 1 and 7'
}
}
Is my string repeating the same character over and over ?
Task
The title is self explanatory, write a function hasOneChar returning:
true if the given string contains the same character repeated all along the string false otherwise.
My solution:
function hasOneChar(s) {
var pattern = new RegExp(/^(.)\1{0,}$/);
var testString = pattern.test(s);
return testString ;
}
Changing letters
Task
When provided with a String, capitalize all vowels
My solution:
function swap(st){
s =st.replace(/[aeiou]/gi, function(x){return x.toUpperCase();})
return s
}
Moving Zeros To The End
Task
Write an algorithm that takes an array and moves all of the zeros to the end, preserving the order of the other elements.
My solution
var moveZeros = function(arr) {
//algorithm that takes an array and moves the zero
//so it searches for zeros
//if a zero exists then push it so the zero goes to the end
//and after it deletes the zeros from the existed position
for (var i = arr.length; i--;) {
if (arr[i] === 0) {
arr.splice(i, 1);
arr.push(0)
}
}
return arr;
};
Alphabetti Spaghetti
Task
You will be given a string of letters. The string must be returned in alphabetical order.
function alphabetti(str) {
return str.split('').sort(caseInsensitiveSort).join('');
function caseInsensitiveSort(a, b) {
var a = a.toLowerCase(),
b = b.toLowerCase();
return ret1 = a > b ? true : false;
}
}
Σχόλια
Δημοσίευση σχολίου