Open cideM opened 6 years ago
@rboelkow
@rboelkow
i = 0, output = '';
. Research where these variables end up, in which scope. Use const
and let
where appropriate. Understand the difference between both and how var
fits in.while (i < 7){
output += String(pyramido);
console.log(output);
i ++;
}
That function is not pure. console.log
is a side effect. Instead, do it like so
const pyramid = fnThatCreatesPyramidoBueno(100)
console.log(pyramid)
Now you have function that takes an integer and returns a string, always.
Your tests don't say anything about why they might fail. If you're running this on node, look into https://nodejs.org/api/assert.html#assert_assert_deepequal_actual_expected_message and then something like https://github.com/avajs/ava
Also, use prettier
to format code and eslint
to lint it. YOu'll have to use these tools eventually, without them your code will look like crap.
Don't drop code like
var current = 0
while (current < 100) {
current ++;
if ((current % 3 == 0) && (current % 5 == 0)){
console.log("FizzBuzz");}
else if (current % 3 == 0){
console.log("Fizz");}
else if (current % 5 == 0){
console.log("Buzz");}
else {
console.log(current);}
randomly into a file.
Here's the most important rule: everything should be a function. Repeat after me: ^^^
Write lots of small, pure functions. Test the functions. Document them. ???????. Profit.
Here's a fun exercise: Write
function chessmaker (gridsize){
oddLine = "", evenLine = "", fullgrid = "", gridprint = gridsize
while (gridsize > 0) {
if (gridsize % 2 == 0) {
evenLine += ' ';
oddLine += '#';
gridsize --;
}
else {
evenLine += '#';
oddLine += ' ';
gridsize --;
}
}
oddLine += '\n', evenLine += '\n'
while (gridprint > 0) {
if (print % 2 == 0) {
fullgrid += evenLine;
gridprint --;
}
else {
fullgrid += oddLine;
gridprint --;
}
}
return fullgrid
}
without mutating any variables.