7 kyu

RuplesJS #3: String EachChar

274 of 646NateBrady23

Description:

Your team is really excited with all the contributions you've made to the RuplesJS library. They feel the work you're doing will truly help Ruby developers transition to Javascript! They've assigned you another task.

String.eachChar()

In ruby you can add something after each character in a string like so:

"hello".each_char {|c| print c, ' ' } -> "h e l l o " 

In the spirit of polymorphism, our eachChar method will allow one of two arguments, a callback function or a string. If a string is presented, it will be added after each character of the original string and return the new string. If a function is presented, it will perform a manipulation of each character in the string.

Example:

"hello".eachChar(" ");
-> "h e l l o "

"hello all".eachChar(function(char) {
  if (char == "l") {
    return char.toUpperCase();
  } else {
    return char;
  }
});
-> "heLLo aLL"
each_char("hello"," ")
-> "h e l l o "

def func(c):
    return 'L' if c=='l' else c
    
each_char("hello all", func)
-> "heLLo aLL"
"hello".each_char(" ")
-> "h e l l o "

def func(c)
    return c=='l' ? 'L' : c
end
"hello all".each_char(:func)
-> "heLLo aLL"

Please add your contribution to RuplesJS!

Check out my other RuplesJS katas:
Fundamentals
Language Features

Stats:

CreatedDec 28, 2015
PublishedDec 28, 2015
Warriors Trained1311
Total Skips70
Total Code Submissions1846
Total Times Completed646
JavaScript Completions356
Python Completions274
Ruby Completions48
Total Stars12
% of votes with a positive feedback rating90% of 169
Total "Very Satisfied" Votes141
Total "Somewhat Satisfied" Votes21
Total "Not Satisfied" Votes7
Ad
Contributors
  • NateBrady23 Avatar
  • GiacomoSorbi Avatar
  • Blind4Basics Avatar
  • FArekkusu Avatar
  • Just4FunCoder Avatar
  • saudiGuy Avatar
Ad