Jump to content

simple pig latin codewars kata problem

shivajikobardan

I'm doing this kata.
https://www.codewars.com/kata/520b9d2ad5c005041100000f/train/javascript

function pigIt(str) {
  const words = str.split(" ");
  for (var i = 0; i < words.length; i++) {
    if (words[i] === "!") {
      return "!";
    }
    var firstLetterSliced = words[i].slice(0, 1);
    var remainingLetterSliced = words[i].slice(1, words[i].length);
    return (remainingLetterSliced + firstLetterSliced + "ay");

  }

}
pigIt('Pig latin is cool'); // igPay atinlay siay oolcay

My code isn't passing a test. Please help fix the issue.

is-aYVaXkqegGCcGFrGp0cu9uJjnSX3KjaXyQTQXBC1P_EWcjA6oenKrdRIDXXxOtyWH-efZdAOkGOhpMv8PiiTOppRapORfcUmfjXouBK8RgrwELQ6O9g_COB-uHzdFieoWriLBheMWv68mCMW7IrF7LP2uq_KavVMVhXYLyLOf-RaQ6msuCGvZRMkGoA

 

Link to comment
Share on other sites

Link to post
Share on other sites

when you say return, the function returns right there. So your code returns the first word or whatever. Or returns "!" .

You should store the intermediate results in a string (add to a string variable) and only when you're done parsing the string return the string you stored in that variable. 

 

edit: 

You'll probably want to not test for "!" , but rather for words that are one letter only to include ? as well .... and "a" as in "John has a car"

 

code would be something like this (pseudocode , no particular programming language)

 

words = split  ( input , " ") 

output = ""  // empty sting 

foreach (words as word)  /// or a for  i = 0 ; i< count(words); i++ 

   if  (length ( word) <2)   

       output = output + " " + word

   else 

      output = output + " " + substring(word,1)  + substring(word,0,1)  + "ay"

   end if 

end foreach/for 

// remove the first space added to the output variable

output = substring(output,1)

return output 

 

Link to comment
Share on other sites

Link to post
Share on other sites

function pigIt(str){
   return str.split(' ').map((word)=>{
    if(['!', '?'].includes(word)){
      return word;
    } else {
      return word.slice(1) + word[0] + "ay";
    }    
  }).join(' ');
}

ಠ_ಠ

Link to comment
Share on other sites

Link to post
Share on other sites

On 1/25/2023 at 11:33 PM, shadow_ray said:
function pigIt(str){
   return str.split(' ').map((word)=>{
    if(['!', '?'].includes(word)){
      return word;
    } else {
      return word.slice(1) + word[0] + "ay";
    }    
  }).join(' ');
}

I barely understand a single word here although I'm aware of evrything you wrote here like what includes mean, what map means, what slice means. how do you guys think like this? What sort of practice do you do to achieve this step?

Link to comment
Share on other sites

Link to post
Share on other sites

It's Javascript ... 

 

In Javascript, you can call functions on variables by just saying dot and name of the function after the variable

So  str is a variable name, and str.split( ' ' )  will call function split  that takes the content of variable str  and splits it based on where the space character is detected, and it returns an array of strings. 

Now, on each of the elements of the array of strings, the function .map()  is called  which does something else.

 

In Javascript, you can create temporary functions just by writing   ( parameters of the function )  =>  { what the function does  }

So

(word)=>{
    if(['!', '?'].includes(word)){
      return word;
    } else {
      return word.slice(1) + word[0] + "ay";
    }    
}

is actually an in-line  function without  a name, it doesn't need one because it's only used by the map function 

He could have written the code like this for more readability:

 

function Check(word) {
	if(['!', '?'].includes(word)){
		return word;
	} else {
		return word.slice(1) + word[0] + "ay";
	}    
}

function pigIt(str){
   return str.split(' ').map(Check).join(' ');
}

The map function runs the function given to it as a parameter on each string it was called with (because it's called multiple times, for each element of the array returned by the split function previously.

The words are replaced with the result of the function check, and then finally, all the elements of the array are glued back together by calling the function .join with the character space as glue element.

 

.includes is also a function, returns true if the string contains that word ... so basically the includes is called twice, once by the ! string, then by the ? string

and it's compared against the string received by the function as parameter

.slice returns a substring, starting from the offset specified as parameter ...

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×