I am drawing a triangle with "#" as default value if 'char' is not defined. When I define 'char' I get the triangle drawn with that particular value.
If 'char' is defined then triangle must be drawn in characters from 'char' else it should use default value '#'.
What am I missing or doing wrong?
function triangle(size, char = 0) {
if (typeof char === 'undefined') {
let hashKey = "#"
while (hashKey.length <= size) {
console.log(hashKey);
hashKey += "#";
}
} else {
let hashKey = char
while (hashKey.length <= size) {
console.log(hashKey);
hashKey += char;
}
}
}
= 0on the parameter declaration,charwill never be undefined.charis neverundefined. Whenever it gets that value (either by omitting it in the call or explicitly passingundefined), it is automatically assigned the default value0. So the first branch of yourifwill never be true.char = '#'instead ofchar = 0function(triangle, char) { ...or set char to the desired valuefunction triangle (size, char = '#') { ...instead