0

I'm trying to do this following javascript exercise here: Create a function called mixUp. It should take in two strings, and return the concatenation of the two strings (separated by a space) slicing out and swapping the first 2 characters of each. You can assume that the strings are at least 2 characters long.

and here's my code:

var mixUP = function(a, b) {
  var sliceA = a.slice(0,2);
  var sliceAa = a.slice(2);
  var sliceB = b.slice(0,2);
  var sliceBb = b.slice(2);
  var string = sliceA + sliceBb + " " + sliceB + sliceAa;
  console.log(string);
};
mixUp(apple, pear);

Could anyone please help me out here coz it's not working for me. Thanks heaps!

4
  • Add quotes around strings: mixUp('apple', 'pear'); Commented Jul 27, 2016 at 12:42
  • Are apple and pear strings? I don't see quotes and I don't see their definition. Commented Jul 27, 2016 at 12:42
  • 2
    Last character of MixUp shall be small in the definition... Commented Jul 27, 2016 at 12:43
  • @Redu Great catch! Commented Jul 27, 2016 at 12:44

4 Answers 4

1

The way I approached it was:

    function mixUp(stringA, stringB) {
        var sliceA = stringA.slice(0,2),
          sliceB = stringB.slice(0,2);
        return (sliceB + stringA.substring(2) + " " + sliceA +
          stringB.substring(2));
    }

Which gives you the desired output

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

Comments

0

If you define mixUP call it, not mixUp last p is uppercase.

When you use strings, you need to add quotes around them:

var mixUp = function  (a, b) {
    var sliceA = a.slice(0, 2);
    var sliceAa = a.slice(2);
    var sliceB = b.slice(0, 2);
    var sliceBb = b.slice(2);
    var string = sliceA + sliceBb + " " + sliceB + sliceAa;
    console.log(string);
};
mixUp('apple', 'pear');

OUTPUT

apar peple

Comments

0

Typo's:

mixUp(apple, pear);

should be:

mixUP('apple', 'pear');
    ^ ^     ^  ^    ^

You called mixUp instead of mixUP, and both apple and pear should be strings (Unless they are variables already)

That'll give you the desired result:

var mixUP = function(a, b) {
  var sliceA = a.slice(0,2);
  var sliceAa = a.slice(2);
  var sliceB = b.slice(0,2);
  var sliceBb = b.slice(2);
  var string = sliceA + sliceBb + " " + sliceB + sliceAa;
  console.log(string);
};

mixUP('apple', 'pear');

Comments

0

Either change the function calling or the function definition to same name as you have taken different names for both.

  var mixUP = function(a, b) {
      var sliceA = a.slice(0,2);
      var sliceAa = a.slice(2);
      var sliceB = b.slice(0,2);
      var sliceBb = b.slice(2);
      var string = sliceA + sliceBb + " " + sliceB + sliceAa;
      console.log(string);
    };
    mixUP("apple", "pear"); // apar peple

1 Comment

apple is not defined.

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.