Fun with ES6 Classes #6 - Fake Files (Basic) Codewars
-
Reference to: https://www.codewars.com/kata/5784c8116211383b5f0001d3
Write the following code for the above task:class File { constructor(fullName, contents){ const fullNameArray = fullName.split('.'); const fileNameArray = fullNameArray.filter((el,i)=> i !== fullNameArray.length-1);
this._fullName = fullName; this._filename = fileNameArray.join('.'); this._contents = contents; this._extension = fullNameArray[fullNameArray.length-1]; this._currentLineToRead = 0; this._currentCharToRead = 0; } get fullName(){ return this._fullName; } set fullName(fullName){ return; } get filename(){ return this._filename; } set filename(filename){ return; } get extension(){ return this._extension; } set extension(extension){ return; } getContents(){ return this._contents; } write(str){ this._contents = `${this._contents}\n${str}`; } gets(){ if(this._contents.length === 0){ return ''; } const res = this._contents.split('\n')[this._currentLineToRead]; this._currentLineToRead++; return res; } getc(){ if(this._contents.length === 0){ return ''; } const res = this._contents[this._currentCharToRead]; this._currentCharToRead++; return res; }
}
https://i.stack.imgur.com/jrdeL.png ♪
But after the click on the attempt, I'm pointing,
https://i.stack.imgur.com/3Wmmu.png ♪
I understand that wrong is in the code, but I can't find it. I'll be grateful for your help.
-
There's two mistakes on the picture.
extra
\n
at the beginning of the line. The obvious cause of error.write
which always adds to the row. It is sufficient for the decision to verify that there is already something in the contents and to add a transfer only in this case, for example:this._contents = `${this._contents.length ? this._contents+'\n':''}${str}`;
The second error indicates a misrepresentation of methods
gets
♪getc
♪ Test results are empty lines. The result is that_contents
starting\n
after adding a new line. And the mistake will be made after the previous rectification. However, attention should be drawn to the incorrect logic with an empty line in these functions. Conditionif(this._contents.length === 0){
in this case is less than expected
undefined
He'll return the empty line.
If the existing code is followed, the following improvements may also be made:
A scattering for expansion and file name. To obtain the last element of the mass, you can use the method https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/pop in this case the element will be removed from the reference mass. Thus, there is an immediate expansion and a mass for the name:
this._extension = fullNameArray.pop(); this._filename = fullNameArray.join('.');
The empty grids can be removed.
use the postfix operator directly in terms. Since the post-fix operator returns the previous value, it can be used as soon as the row or symbol is obtained:
return this._contents.split('\n')[this._currentLineToRead++]; return this._contents[this._currentCharToRead++];