4

I have a reference array of phrases like this:

const reference = ["your", "majesty", "they", "are", "ready"];

and I want to join some of the elements of above array based on another array so if another is this:

const another = ["your", "they are"];

The the result would be like :

result = ["your", "majesty", "they are", "ready"];

another situation to be more clear:

const reference = ["your", "majesty", "they", "are", "ready"];
const another = ["your majesty", "they are ready"];
result = ["your majesty", "they are ready"];

or:

const reference = ["your", "majesty", "they", "are", "ready"];
const another = ["majesty they", "ready"];
result = ["your", "majesty they", "are", "ready"];

and of course if there is an empty another we get the same reference as the result:

const reference = ["your", "majesty", "they", "are", "ready"];
const another = [];
result = ["your", "majesty", "they", "are", "ready"];
9
  • 2
    Did you have a question? What result is your current attempt giving you? Commented Oct 6, 2020 at 22:48
  • 2
    This just sounds like a FANG coding interview question; what is your current code that you have, to show that you have tried to do this already, and see what the errors are Commented Oct 6, 2020 at 23:00
  • 1
    @Samathingamajig I'm not sure what FANG is, but this question - or questions using the exact same phrase - has been asked numerous times recently. So, on that basis, I would agree that these do look like interview questions or, at least, homework. Commented Oct 7, 2020 at 18:02
  • 1
    @Samathingamajig Thanks. I'm not sure what it says about the job applicants, if they have to ask for help for interview questions. Commented Oct 7, 2020 at 18:14
  • 1
    @Samathingamajig So how do they know about the questions that they are going to be asked? It seems strange that so many people are asking the same, or very similar, questions all in a short space of time. Do the FANG companies publish the questions? Commented Oct 7, 2020 at 18:18

3 Answers 3

2

const reference = ["your", "majesty", "they", "are", "ready"];
//const another = ["your", "they are"];
//const another = ["your majesty", "they are ready"];
//const another = ["majesty they", "ready"];
const another = [];

function formatResult(reference, another) {
  //1: another is [] then return reference
  if (another.length == 0) {
    return reference;
  } else {
    //2: format the reference array with another array
    another.forEach(x => {
      var words = x.split(' ');
      if (words.length > 1) {
        var index = 0;
        var sindex = 0; // if the index are countiues
        for (var i = 0; i < words.length; i++) {
          //find this word in refference array.
          index = reference.indexOf(words[i]);
          if (i == 0) {
            sindex = index;
          } else {
            //check if the index are next to each other, if not break the loop
            if (sindex != index) {
              break;
            }
          }
          sindex++;
        }
        if (index == sindex - 1) //to check all the index in the order
        {
          var startIndex = index - (words.length - 1);
          //update reference array with another array.
          reference.splice(startIndex, words.length, x);
        }
      }
    })
  }
  return reference;
}
var result = formatResult(reference, another);
console.log(result);
Does this satisfy your question?

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

Comments

2

You can filter the first input array using Array.filter to remove the items which are included on the second input array.

And the result can be made by joining those filtered first array and second array using Array.concat.

function format(input1, input2) {
  let result = input1.filter((item) => !input2.some((item2) => item2.includes(item)));
  return result.concat(input2);
}

const reference1 = ["your", "majesty", "they", "are", "ready"];
const another1 = ["majesty they", "ready"];
const result1 = format(reference1, another1);
console.log(result1);

1 Comment

It looks like this implementation does not distinguish between they majesty and majesty they. But I don't know if the order is matter in this case.
1

My solution:

var ref = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "h"];
var ano = ["a", "b c", "e f", "d", "f", "g h i", "j"];
var res = [];

ano = ano.filter(a => a.split(" ").length>1); // remove single words from ano

while(ano.length) {
    var ano_words = ano.shift();
    var len = ano_words.split(" ").length;
  
    while(ref.length>len) {
        var ref_words = ref.slice(0,len).join(" ");
        if (ref_words == ano_words) { 
            res.push(ref_words);
            ref = ref.slice(len,ref.length);
            break;
        }
        res.push(ref.shift());
    }  
}

res = [...res, ...ref]; 

console.log(res);  // ["a", "b c", "d", "e f", "g h i", "j", "h"]

Comments

Your Answer

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