1

I am trying to get it so that the result is the rest of the word + first letter + 'ay'; right now my code is giving me the total number of letters + first letter + 'ay'. I believe my third variable is what is causing this to happen but I don't know how to fix it.

function PigLatin()
// assumes: word is given by user
// results: word reshaped in piglatin
{
  var word, firstletter, restofword

  word = document.getElementById('wordbox').value;
  firstletter = word.charAt(0);
  restofword = (1, word.length);

  document.getElementById('outputDiv').innerHTML = (restofword + firstletter + 'ay');
}
<h1>Pig Latin</h1>
<p>What is your word? <input type="text" id="wordbox" value="" ;></p>
<input type="button" value="Click here to convert" onclick="PigLatin();">
<hr>
<div id=outputDiv></div>
mplungjan
  • 134,906
  • 25
  • 152
  • 209
  • 1
    What are you expecting `restofword=(1,word.length);` to do? – takendarkk Dec 04 '20 at 21:02
  • it looks like, you are looking for [`String#slice`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice). – Nina Scholz Dec 04 '20 at 21:04
  • Now you just have a [comma operator](https://stackoverflow.com/questions/9579546/when-is-the-comma-operator-useful) statement – mplungjan Dec 04 '20 at 21:05
  • Please pay attention when tagging. This question has nothing to do with Apache Pig, and nothing to do with Latin. It really doesn't have to do with pig Latin. That just happens be the exercise you're doing. – Heretic Monkey Dec 04 '20 at 21:05
  • 1
    `restofword = word.substring(1, word.length);` will work – mplungjan Dec 04 '20 at 21:06
  • Voting to close as _Not reproducible or was caused by a typo. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers._ – mplungjan Dec 04 '20 at 21:07

1 Answers1

0

(1, word.length) doesn't do what you think. What you have here is the comma operator called on 1 and word.length, surrounded by parentheses, which don't do much here since there are no other parts to this expression. This operator evaluates all its operands, and returns the last one, which is word.length.

In seems as though you meant to extract the substring of the word:

restofword = word.substring(1, word.length);

Which could also be shortened to

restofword = word.substring(1);
Mureinik
  • 252,575
  • 45
  • 248
  • 283