0

I'm working on a problem where the task is to write a program which reconstructs each sentence out of a set of words and prints out the original sentences.

INPUT SAMPLE:

2000 and was not However, implemented 1998 it until;9 8 3 4 1 5 7 2

And the answer is:

However, it was not implemented until 1998 and 2000

So far I got to the point where I have combined the words and number hints together as a pair value in an object. The only problem I am running into is that there is actually a missing number hint, thus one of the words has an undefined value.

How can I fill in this value?

I have tried to use .HasOwnProperty() and for-looping through to see if one of the values equals to undefined, but neither has worked. Any input would be greatly appreciated!

function encyrption(str){
  var string = str.split(";");
  var words = string[0].split(" ");
  var hints = string[1].split(" "); 
  var object = {};


  for(var i = 0; i < words.length; i++){
     if(object[hints[i]] === undefined){
      /////???
    }else
    object[hints[i]] = words[i];
  }
   
return object;

}
console.info(encyrption("2000 and was not However, implemented 1998 it until;9 8 3 4 1 5 7 2"));

8
  • object[hints[i]] = whatever value you want Commented Jun 26, 2017 at 4:41
  • object[hints[i]] will always be undefined because object is always {}. Commented Jun 26, 2017 at 4:41
  • @Bernard - depends on the values in hints? i.e. if there's duplicates, then it won't always be undefined Commented Jun 26, 2017 at 4:42
  • @JaromandaX I meant that the else clause will never be executed because object[hints[i]] === undefined will always be true since object was initialized with {} and is never modified. Commented Jun 26, 2017 at 4:44
  • yes, I just realised the logic of the code :p Commented Jun 26, 2017 at 4:45

1 Answer 1

1

I'd do something like that, just guessing that the missing hint is the last word, and that will always be the sixth position. If that's not the case I'd need more information about the problem test cases to solve it.

function encyrption(str){
  var string = str.split(";");
  var words = string[0].split(" ");
  var hints = string[1].split(" ");
  var hints_sorted = hints.concat().sort();
  var missing_hint;
  var object = {};
  
  for(var i = 0; i < words.length; i++) {
    if(hints_sorted[i] != i+1) {
      missing_hint = (i+1).toString();
      break;
    }
  }
  
  hints.push(missing_hint);
  
  for(var i = 0; i < words.length; i++){
    object[hints[i]] = words[i];
  }
  
  return object;
}

console.info(encyrption("2000 and was not However, implemented 1998 it until;9 8 3 4 1 5 7 2"));

//Result: However, it was not implemented until 1998 and 2000

There you have a small explanation:

I created the hints_sorted array, which is a copy of the hints one, but sorted, so, in our example:

hints = ['9','8','3','4','1','5','7','2']; 

hints_sorted = ['1','2','3','4','5','7','8','9'];

Then, inside the for, I'm comparing the value with the index + 1 (since the index inside the loop starts at zero):

1 -> 1
2 -> 2
3 -> 3
4 -> 4
5 -> 5
7 -> 6

On the sixth element, we have 7 on our array and we are expecting 6, so it goes inside the if, we set 6 as our missing hint, and we break; the loop so it doesn't continue checking values.

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

4 Comments

You can look at more test cases here: codeeval.com/browse/140. Yes, in this case the missing number is "6", but if you look at the third case the missing number is "10". I checked it does seem that the missing number is always at the end. How can i tell hint[i] to equal the appropriate number? Thank you!
I see. I've edited my answer with a solution to dynamically get the missing hint number. check it out :)
Hi! It definitely worked! But could you explain your IF condition? I dont understand the conditions you have stated and the executions inside the if statement as well. Thank you so much! :)
happy to hear that!! I've edited my answer with a small explanation, hope you find it useful :)

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.