-1

Here is my code:

function lattify(id) {
  var inputTextStr = document.getElementById(id).value;
  var lattiStr = "";
  for (var i = 0; i < inputTextStr.length; i++) {
    console.log(inputTextStr[i]);
    var inputText = inputTextStr[i];
    if (inputTextStr[i + 1] !== "a" || "e" || "i" || "o" || "u") {
      lattiStr = inputTextStr.substr(2) +
        inputTextStr.substr(0, inputTextStr.length);
    }

  }
  console.log(lattiStr);
}

Here is my code:

<form>
  <input type="text" id="latin">
  <button type="button" onclick="lattify('latin'.toString())">Submit</button>
</form>

What this program is supposed to do is take the text input from the form into the function 'lattify'. That part works. The part that does not work is the part where the first two letters are added to the end if they are both of them are consonants.

  • 1
    would be helpful if you provided a few "before" and "after" examples – CrayonViolent Nov 13 '17 at 23:56
  • 1
    I don't think this does what you expect: `if(inputTextStr[i+1]!=="a"||"e"||"i"||"o"||"u")`. For instance: `inputTextStr[i+1]!=="a"` is a boolean statement. Then then statement "e" is ORd to it, which because it's non-zero is always TRUE. So your `if` statement is always true. Instead, why don't you use `indexOf`. – MFisherKDX Nov 13 '17 at 23:57
  • Also, you aren't checking the bounds of your string before you index into it. You are going to run into problems. – MFisherKDX Nov 14 '17 at 00:01
  • *"if the first 2 characters of a string are consonants, move them to the end of the string"*. An alternative solution: `"your string here".replace(/^([^aeiou]{2}).*/,function(m0,m1){return m0.substring(2)+m1});` – CrayonViolent Nov 14 '17 at 00:02

3 Answers3

1

To me this seems like a good place for a regular expression rather than looping. For example this becomes much simpler with:

function lattify(inputTextStr) {
  if (inputTextStr.match(/^[^aeiou]{2}/i)) {
    inputTextStr = inputTextStr.substring(2) + inputTextStr.substring(0, 2)
  }
  return inputTextStr
}

console.log(lattify("MMstring"))
console.log(lattify("MeMstring"))
console.log(lattify("eestring"))
console.log(lattify("eMstring"))

This is case insensitive, too.

It's also worth noting that the logic of testing for vowels, doesn't guarantee the string starts with consonants. Think about the behavior you want if the string starts with a number or punctuation.

Mark
  • 90,562
  • 7
  • 108
  • 148
0

You are not explicitly converting the input text into a char array.

From another post you can do it this way:

var strChars = myString.split("");

If in the same loop you are addressing [i] and [i+1], the loop needs to end in length - 1, otherwise you go out of bounds on the last loop.

The condition should be checked for each letter:

inputTextStr[i+1]!=="a"||
inputTextStr[i+1]!=="e"||
inputTextStr[i+1]!=="i"||
inputTextStr[i+1]!=="o"||
inputTextStr[i+1]!=="u"

The second parameter of substr should be 2, which is the number of characters you want to copy to the end.

inputTextStr.substr(0,2);

instead of

inputTextStr.substr(0,inputTextStr.length);
Juan
  • 5,525
  • 2
  • 15
  • 26
  • `inputTextStr.substr(0,2);` what happens when the string does not contain 2 characters? – MFisherKDX Nov 14 '17 at 00:13
  • @MFisherKDX it will paste 0 or 1 characters, but the assuption in the question is that it has 2 characters. – Juan Nov 14 '17 at 00:18
0

You can't check multiple values in an if statement by appending multiple ||s. The left part of the condition needs to be included each time.

You could check against an array of vowels to avoid such redundant code.

array.indexOf(val) will check for val inside of array and return -1 if it is not found.

The loop isn't necessary, you can manually check the first and second characters, moving them to the end if necessary. substr(0, 2) to get the first two chars, substr(2, [str.length]) to get everything else.

function lattify(id) {
  var vowels = ['a', 'e', 'i', 'o', 'u'];
  var inputTextStr = document.getElementById(id).value;
  var lattiStr = ""; 
  var ret = inputTextStr;

  if(inputTextStr.length > 2 && vowels.indexOf(inputTextStr[0]) === -1 && vowels.indexOf(inputTextStr[1]) === -1) {
    //first and second characters in the string are consonants.
    ret = inputTextStr.substr(2, inputTextStr.length) + inputTextStr.substr(0,2);
  } 

  console.log('returning: ' + ret);
  return ret;
}
IrkenInvader
  • 4,030
  • 1
  • 12
  • 23