I will have a look again. Thanks.
I am not getting that error:
ReferenceError: assignment to undeclared variable secondFunction
With the following code:
function addTogether() {
let arg1 = typeof arguments[0];
let arg2 = typeof arguments[1];
if (arg2 === 'undefined') {
return (secondFunction = (arg1) => {
let arg0 = typeof arguments[0];
console.log(arg0);
if (arg0 === 'undefined') {
return undefined;
}
console.log(arg1 + arguments[0]);
return arg1 + arguments[0];
});
}
if (arg1 === 'undefined') {
return undefined;
}
console.log(arguments[0] + arguments[1]);
return arguments[0] + arguments[1];
}
Are you running the test in the fCC console? Your environment is letting you get away with undefined variables because you do not have strict mode enabled.
I’ll try to enable strict mode on my side but when I run the last version of the code posted on here.
This is what I get from the FCC console:
// running tests
addTogether(5)(7) should return 12.
addTogether("https://www.youtube.com/watch?v=dQw4w9WgXcQ") should return undefined.
addTogether(2, "3") should return undefined.
addTogether(2)([3]) should return undefined.
// tests completed
// console output
5
5
53
23
5
AhHa! Your variable shadowing is hiding the fact that arrow functions have no binding of arguments
.
Again, wacky to figure out because your variable names are confusing both of us about your logic.
1 Like
When you copy-paste your exact code above into fCC and add the exact line I gave you above, then you get:
console.log(addTogether(5)(7));
1 Like
I get the error that way yes!
The variable secondFunction
is never defined, and is never actually needed.
I went ahead and refactored the naming convention. I hope this helps.
function addTogether() {
let argumentAddTogether0 = arguments[0];
let argumentAddTogether1 = arguments[1];
if (typeof argumentAddTogether1 !== 'number') {
//
//
return (argumentAddTogether1) => {
let argumentAnonymousArray = arguments[0];
if (typeof argumentAnonymousArray !== 'number') {
console.log('argumentAnonymousArray Not a Number');
return undefined;
}
//
//
console.log('Result sent');
return argumentAddTogether1 + arguments[0];
};
}
if (typeof argumentAddTogether0 !== 'number') {
return undefined;
}
return argumentAddTogether0 + argumentAddTogether1;
}
addTogether(5)(7) works now but all of the remaning test return [Function (anonymous)].
Getting closer. I think there are a few relevant cases to consider
First argument is not a number
First argument is a number, no second argument
Second argument is not a number
Second argument is a number
How does your code handle each of these?
I was just reading your comments again:
" Arrow functions do not have their own arguments
object . Thus, in this example, arguments
is a reference to the arguments of the enclosing scope:"
Yeah this is obviously an issue in my code will look into this before reading the hints you just posted now. Thanks a lot.
After some tinkering I got most of them working.
The scenario I was most interested in most was when there are two parameters in a single parameter set where the second parameter is not a number.
I updated my test since chai doesn’t seem to have much options in terms of undefined.
I use ‘use strict’ in my local setup.
const addTogether = require('../addTogether');
const chai = require('chai');
const expect = chai.expect;
const should = require('should');
describe('addTogether Test suite', () => {
it('addTogether(2,3) shoud return 5', () => {
const result = addTogether(2, 3);
expect(result).to.be.eql(5);
});
it('addTogether(23, 30) should return 53', () => {
const result = addTogether(23, 30);
expect(result).to.be.eql(53);
});
it('addTogether(5)(7) should return 12', () => {
const result = addTogether(5)(7);
expect(result).to.be.eql(12);
});
it('addTogether() should return undefined', () => {
const result = addTogether('https://www.youtube.com/watch?v=dQw4w9WgXcQ');
expect(result).to.be.eql(undefined);
});
it('addTogether(2, "3") should return undefined', () => {
const result = addTogether(2, '3');
should(result).undefined;
});
it('addTogether(2)([3]) should return undefined', () => {
const result = addTogether(2, [3]);
should(result).undefined;
});
});
This is my last iteration:
'use strict';
function addTogether() {
let argumentAddTogether0 = arguments[0];
let argumentAddTogether1 = arguments[1];
if (typeof argumentAddTogether0 !== 'number') {
return undefined;
}
if (typeof argumentAddTogether1 !== 'number') {
return (argumentAddTogether1) => {
if (typeof argumentAddTogether1 !== 'number') {
return undefined;
}
return argumentAddTogether1 + arguments[0];
};
}
return argumentAddTogether0 + argumentAddTogether1;
}
For some reason this one single test doesn’t pass.
addTogether(5)(‘3’)
Locally it returns undefined;
How is this differentiating between
only one argument, a valid number
two arguments, the first a valid number but the second not a valid number
It’s not much but it’s honest work.
'use strict';
function addTogether() {
let argumentAddTogether0 = arguments[0];
let argumentAddTogether1 = arguments[1];
if (typeof argumentAddTogether0 !== 'number') {
return undefined;
}
if (arguments.length > 1 && typeof argumentAddTogether1 !== 'number') {
return undefined;
}
if (typeof argumentAddTogether1 !== 'number') {
return (argumentAddTogether1) => {
if (typeof argumentAddTogether1 !== 'number') {
return undefined;
}
return argumentAddTogether1 + arguments[0];
};
}
return argumentAddTogether0 + argumentAddTogether1;
}
I think it is one of the trickiest challenges in the JavaScript section.
system
Closed
April 2, 2022, 3:40am
35
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.