0

function f() {
  var string1 = "Cyan | Magenta | Yellow | Cut | Information | Pink | Lila | White";

  var string2 = "Color | Color | Color | Technical | Technical | Color | Color | Color";

  var firstlength = string1.split("|").length;
  var secondlength = string2.split("|").length;
  var maxilength = firstlength;
  if (secondlength > firstlength)
    maxilength = secondlength;

  var string1split = string1.split("|");
  var string2split = string2.split("|");

  var concatstr = "";
  for (var i = 0; i < maxilength; i++) {
    if (string1split.length > i) {
      concatstr += string1split[i];
    }
    if (string2split.length > i) {
      concatstr += string2split[i];
    }
    if (i < maxilength - 1) {
      concatstr += "|";
    }
  }


  {

    var colors = concatstr;

    var colorsarray = colors.split("|")
    var realcolorsarray = []

    for (i = 0; i < colorsarray.length; i++) {
      if (realcolorsarray.indexOf(colorsarray[i]) == -1) {
        realcolorsarray.push(colorsarray[i])

      }

    }

    var colorline = realcolorsarray.join("|")

    return colorline;

  }

}

f();

This is my result:

Cyan Color | Magenta Color | Yellow Color | Cut Technical | Information Technical | Pink Color | Lila Color | White Color

But i don't want the Color-Information behind the Technical Colors:

Cyan Color | Magenta Color | Yellow Color

3
  • I only need the information before the Technical then why include Cut Technical in the result? Also, share your current approach. Commented Aug 24, 2022 at 6:39
  • 1
    Hi Heike, what is your question? How did you try it so far? Can you please share some code? Commented Aug 24, 2022 at 6:39
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. Commented Aug 24, 2022 at 6:39

1 Answer 1

0

"But i don't want the Color-Information behind the Technical Colors"

What I got from this line and the example result you gave after is you want only the colors before the ones including Technical so here it is:

You need a break in first for if the current item from string2 is "Technical"

function f() {
  var string1 = "Cyan | Magenta | Yellow | Cut | Information | Pink | Lila | White";

  var string2 = "Color | Color | Color | Technical | Technical | Color | Color | Color";

  var firstlength = string1.split("|").length;
  var secondlength = string2.split("|").length;
  var maxilength = firstlength;
  if (secondlength > firstlength)
    maxilength = secondlength;

  var string1split = string1.split("|");
  var string2split = string2.split("|");

  var concatstr = "";
  for (var i = 0; i < maxilength; i++) {
    if (string2split[i].trim() == "Technical") {
      break;
    }
    if (string1split.length > i) {
      concatstr += string1split[i];
    }
    if (string2split.length > i) {
      concatstr += string2split[i];
    }
    if (i < maxilength - 1) {
      concatstr += "|";
    }
  }


  {

    var colors = concatstr;

    var colorsarray = colors.split("|")
    var realcolorsarray = []

    for (i = 0; i < colorsarray.length; i++) {
      if (realcolorsarray.indexOf(colorsarray[i]) == -1 && colorsarray[i] != "") {
        realcolorsarray.push(colorsarray[i])

      }

    }

    var colorline = realcolorsarray.join("|")

    return colorline;

  }

}

console.log(f());

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

Comments

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.