adventjs-issues
adventjs-issues copied to clipboard
Suggestion regarding Js Problems
Hi...
This is my code for challenge number 4. Could you tell me what's wrong with it, please? It runs perfectly on my terminal, but your website does not validate it.
CODE:
const createXmasTree = (height)=>{
let alt = Math.round(height)
let tree =[]
let text = ""
//tree
for(i = 1; i <= alt; i++){
text = "";
for(let j = i; j < alt; j++){
text += ""
}
for( let k = 0; k < (i * 2) - 1; k++ ){
text += "*"
}
for(let m = i; m < alt; m++){
text += ""
}
tree.push(text)
}
//Root
let lumbers = ""
let laps = 2
for(i = 1; i <= laps; i++){
lumbers = "";
for(let j = i; j < (i === 1 ? alt : alt + 1); j++){
lumbers += "_"
}
for( let k = 0; k < 1; k++ ){
lumbers += "#"
}
for(let m = i; m <= (i === 1 ? alt - 1 : alt) ; m++){
lumbers += "_"
}
tree.push(lumbers)
}
return tree.map(item => item + '\n').join('')
}
const letter = 5.3 console.log(createXmasTree(letter))
possible an extra new line at end
No map needed. Use a simple:
return tree.join('\n');
instead of:
return tree.map(item => item + '\n').join('')
Another warning!!! i var scope is global
Hi, thanks for your soon answer.
I popped the map off my code and just as you said, it worked just fine.
I don't get the warning, though. i var is global but it shouldn't be... or it should be global, but it's not on my code???
any undeclared variable inside function is global
use var on ES5 below or const or let when above
i = 0 //global
!(function foo(n) {
i+=5;
return i + n;
}(++i));
// i == 6
!(function bar(n) {
let i = 115; // local var
const result i + n; // constant var
return result;
}(i));
// i == 121
the best way to detect is use strict mode. There a template....
!(function(undefined) { // IIFE
"use strict";
// your code here ...
}(void 0));
Thanks a lot, man. Very clear!