0

So I am doing a an exercise in which I have to sort a given string. Each word in the string contains a number in it(written like this 'H3llo'). The number that is in each word of the string should be placed in order according to the number in the new string that is to be returned.

For example if my input is "is2 Thi1s T4est 3a", then my function should return "Thi1s is2 3a T4est".

I almost cracked it but my output is incomplete. Here is my code:

function order(words) {
    var lst = words.split(' ');
    var count = 0;
    var n = count + 1;
    var s_n = n.toString();
    var new_l = [];
    while (count < lst.length) {
        if (lst[count].includes(s_n) === true) {
            new_l.push(lst[count])
        }
        count++
    }
    return new_l.join(' ');
}

When I test it, instead of getting:

console.log(order("is2 Thi1s T4est 3a"));
>>> 'Thi1s is2 3a T4est'

I get this:

console.log(order("is2 Thi1s T4est 3a"));
>>> 'Thi1s'

Can anyone explain to me what I am doing wrong?

5
  • 2
    Sooner or later you should learn about Array#sort. Commented Nov 8, 2016 at 21:14
  • 1
    n and s_n need to be set inside the loop. Commented Nov 8, 2016 at 21:14
  • did all the suggestion so far still not working Commented Nov 8, 2016 at 21:17
  • add ; after count++ and new_l.push(lst[count]) and do what @Malk told you increase s_n inside loop Commented Nov 8, 2016 at 21:18
  • You never change s_n. It is always "1". How do you expect to get in your output any string other than that containing "1"? Commented Nov 8, 2016 at 22:09

2 Answers 2

2

You will basically need two loops - one for your current counter count i.e. the incremental number and another to iterate over the list of words to match that number. You increase the count only after you have finished iterating over the list.

function order(words) {
  var lst = words.split(' ');
  var count = 0;
  var new_l = [];
  while (count <= lst.length) {
    for (i = 0; i < lst.length; i++) {
      if (lst[i].includes(count)) {
        new_l.push(lst[i])
      }
    }
    count++;
  }
  return new_l.join(' ');
}
console.log(order("is2 Thi1s T4est 3a"));

Notice too that you don't need s_n -- the conversion is implicit, and you don't need === true as this is implicit in the if statement.

Sign up to request clarification or add additional context in comments.

Comments

-1

The main thing you are doing wrong is that you assign s_n to the string '1' before your loop, but you never update it within the loop. At the same time as you update count, you need to update s_n to the string of the next integer.

So you 'fixed' the part where you weren't updating the value of n or s_n within your outer loop, but the code still wont't work because you are now using count both to increment the digit you are looking for (within a word) and to increment the search through the list of words. You need an inner loop (and another variable) to increment the search.

2 Comments

I put 'n++' , 'count++' and 's_n = n.toString();' in the while loop and I got worse results.
Yes it still won't work unless you change the other things that are wrong with your code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.