0

I am sorry for the very newbie question, but this is driving me mad.

I have a word. For each letter of the word, the characters position in one array is found and then returns the character at the same position found in a parallel array (basic cipher). This is what I already have:

*array 1 is the array to search through*
*array 2 is the array to match the index positions*

var character
var position
var newWord 

for(var position=0; position < array1.length; position = position +1) 
{
    character = array1.charAt(count);     *finds each characters positions*
    position= array1.indexOf(character);  *index position of each character from the 1st array*
    newWord = array2[position];           *returns matching characters from 2nd array*
}

document.write(othertext + newWord);      *returns new string*

The problem I have is that at the moment the function only writes out the last letter of the new word. I do want to add more text to the document.write, but if I place within the for loop it will write out the new word but also the other text inbetween each word. What i actually want to do is return the othertext + newWord rather than document.write so that I can use it later on. (just using doc.write to text my code) :-)

I know its something really simple, but I cant see where I am going wrong. Any advice? Thanks Issy

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
user753420
  • 225
  • 1
  • 2
  • 5
  • Hi Issy, welcome to SO. Unfortunately if most users get a sniff of 'homework' style questions, they're not very likely to respond. Also, what language is this in? – Ben May 14 '11 at 08:31
  • Hi Steve, Thanks for your reply, yep I know its quite difficult, I really want to work this out for myself, but I just need a point in the right direction as i am completely stuck as to where to go. its in JavaScript. Thanks Issy – user753420 May 14 '11 at 08:50

2 Answers2

1

The solution is to build newWord within the loop using += instead of =. Just set it to an empty string before the loop.

There are other problems with this code. Variable count is never initialized. But let's assume that loops should be using count instead of position as it's principal counter. In that case, if I am not mistaken, this loop will just generate array2 as newWord. First two lines of loop's body cancel each other in a matter of speaking, and position will always be equal to count, so letters from array2 will be used sequentially from beginning to the end.

Could you provide one example of input and desired output, so that we understand what you actually want to accomplish?

Dialecticus
  • 16,400
  • 7
  • 43
  • 103
  • Hi Yep sorry it should be count rather than position. I have 2 arrays one with 'a,b,c,d,e,f,g,h and another that is 'd,e,f,g,h,a,b,c' my word could be 'ace'. This word will be looked up against the first array to get their index numbers, it will then use these index numbers to look against the 2nd array and return the string 'deh' . The example above (when I use the correct loop word ;-) ) does write out the string, but only because its within the loop - I need to get it to write it out-outside of the loop. Have I made any sense?, excuse my rambling – user753420 May 14 '11 at 09:26
  • Shouldn't the returned string actually be 'dfh'? Also, there is no mention of input string in your code. I suppose that first line of loop body should have `oldWord` instead of `array1`. And like I said, solution is to use += instead of = in third line. – Dialecticus May 14 '11 at 09:34
0

A good way of structuring your code and your question is that you define a function that you need to implement. In your case this could look like:

function transcode(sourceAlphabet, destinationAlphabet, s) {
  var newWord = "";

  // TODO: write some code

  return newWord;
}

That way, you clearly state what you want and which parameters are involved. It is also easy to write automatic tests later, for example:

function testTranscode(sourceAlphabet, destinationAlphabet, s, expected) {
  var actual = transcode(sourceAlphabet, destinationAlphabet, s);
  if (actual !== expected) {
    document.writeln('<p class="error">FAIL: expected "' + expected + '", got "' + actual + '".</p>');
  } else {
    document.writeln('<p class="ok">OK: "' + actual + '".');
  }
}

function test() {
  testTranscode('abcdefgh', 'defghabc', 'ace', 'dfh');
}

test();
Roland Illig
  • 40,703
  • 10
  • 88
  • 121
  • Thanks this is GREAT :-) the problem was that I didnt initalise the new Word - and now I can test it properly. Issy :-) – user753420 May 14 '11 at 12:44