-1

I am trying to use a pure function that is using For Each, the function will change input array to return 'x'. Can someone help me explain why I am getting this error?

Functions:

let functions = {
    helper: (x) => {
        return x;
    },

    changeArray: (x) => {
        let arr1 = [x];
        arr1.forEach(functions.helper);
        return arr1[0];
    }
};

Test file:

test('For Each', () => {
    expect(syntax.changeArray(['hey', 'hi']).toBe(['x','x']));
})

Result/Error:

    TypeError: _syntax.default.changeArray is not a function

      73 | 
      74 | test('For Each', () => {
    > 75 |     expect(syntax.changeArray(['hey', 'hi']).toBe(['x','x']));
         |                   ^
      76 | })

CHANGES:

const syntax{    
    helper: (x) => x,

    changeArray: (arr) => {
        return arr.map(syntax.helper);
    }
}

TEST FILE:

    test('For Each', () => {
       expect(syntax.changeArray(['hey', 'hi'])).toBe(['x','x']);
    })

RESULT:

    expect(received).toBe(expected) // Object.is equality

    - Expected
    + Received

      Array [
    -   "x",
    -   "x",
    +   "hey",
    +   "hi",
      ]
skyboyer
  • 22,209
  • 7
  • 57
  • 64
waterMelon
  • 15
  • 5
  • I just edited, but realized I can't tell if the "helper" and "changeArray" functions are defined inside of an object named "functions" or "syntax". It looks like you reference it by both those names. – Gershom Maes Feb 26 '20 at 16:59
  • Can you include your code where you define `syntax`? – Gershom Maes Feb 26 '20 at 19:39
  • Your `helper` function still has an Error. It is currently returning `x`, a variable. It needs to return `'x'`, a string – Gershom Maes Feb 26 '20 at 19:40
  • Your `changeArray` function still has an Error. It's missing a `return` statement, and will always return `undefined` instead of the mapped `Array` - or you can just copy-paste the exact function I've already written for you – Gershom Maes Feb 26 '20 at 19:41
  • That's because you didn't properly copy my code. Your `changeArray` function is erroneously always returning `undefined` – Gershom Maes Feb 26 '20 at 19:43
  • There is a big difference between `arr => arr.map(fn)`, and `arr => { arr.map(fn); }` – Gershom Maes Feb 26 '20 at 19:44
  • Is there a way I can keep it in the curly brackets since I am calling changeArray function in the test file so I can return the correct values? I have edited the updates. – waterMelon Feb 26 '20 at 19:57
  • If for some reason you need those curly brackets, you can use `arr => { return arr.map(fn); }` (this is 100% equivalent to `arr => arr.map(fn)`) – Gershom Maes Feb 26 '20 at 20:31

2 Answers2

0

There are multiple problems with this

Main one is, what is syntax.changeArray ? Your function is in functions.changeArray.

When you run a function through Array.forEach, the forEach function literally doesn't do anything with the returned value. I think what you want is Array.map

Also, your helper function returns x, not 'x' -- it will return whatever its given, so if you DID pass that helper function to array.map, it would just return the same unchanged value you send it.

This bit of code might hopefully give you an idea of what you should be doing.

function runMap(arr) {
  return arr.map(val => 'x');
}

var testArray = [1,2,3];
console.log(runMap(testArray));
TKoL
  • 13,158
  • 3
  • 39
  • 73
0

Why are we seeing references to both functions and syntax, which seem identical? Let's stick to one, and delete the other. I'll use syntax here.

Here's a definition of syntax that should fix your problems:

let syntax = {

  // This change to the "helper" function solves problem #1;
  // simply return the string 'x' regardless of the parameter
  helper: x => 'x',

  // This "changeArray" function return a new `Array` where
  // every item has been mapped using `syntax.helper` (which
  // will map every Array item to the string 'x'):
  changeArray: arr => arr.map(syntax.helper)

};

Fix the logic error in your test suite. Change:

expect(syntax.changeArray(['hey', 'hi']).toBe(['x','x']))

To:

expect(syntax.changeArray(['hey', 'hi'])).toBe(['x','x']);
Gershom Maes
  • 7,358
  • 2
  • 35
  • 55
  • I see! Thanks, I've tried to adjust the code now but I am receiving Cannot read property 'toBe' of undefined error in test. helper: (x) => x, changeArray: (arr) => { arr.map(syntax.helper); } – waterMelon Feb 26 '20 at 19:34