0

What’s wrong with the code?

If there is something wrong with the comment while loop, tell me why. Is there anything that can work better? What can you use to select all elements of an array? What can you use to add a newline at the start of a array? https://code.sololearn.com/WxDxDJyhc2I3/?ref=app EDIT ABOUT CODE: Forgot to make it public

11th Jul 2020, 5:51 PM
Sali65
Sali65 - avatar
2 Respuestas
+ 3
The first problem is that you're trying to access document.body before the document content loaded. That's why you immediately see a JavaScript error about "innerHTML of null". That can be fixed by wrapping the lines at the bottom with: document.addEventListener('DOMContentLoaded', function() { like: document.addEventListener('DOMContentLoaded', function() { print("Hello;-a comment")//Hey! the comments are working! Just add a semicolon before a comment print("Hi") repeat("Wow",5) equal(5,"50") showType("Wow") alert("Welcome to my printing language, BliLao") }); Your print implementation is confusing. It'll freeze if called with a string like print("- hello") or print("- hello;bye"). Surely you don't want that. It looks like you want to print each and every semicolon-separated substring that doesn't contain a hyphen. Maybe this is why you're asking "What can you use to select all elements of an array?" It looks like "-" indicates a comment and you want to filter those out. If that's the case, this implementation of print should work better: function print(text) { // Break up the text by semicolons. // Filter out any substrings containing hyphens. var codes = text.split(";").filter(function(substring) { return !substring.includes("-"); }) // Show each and every substring. codes.forEach(show); } Based purely on the name of the function and how it behaves when you call it, your repeat function could be cleaned into: function repeat(text,repeatTime = 1) { show((text + " ").repeat(repeatTime)) } The num variable you had looked useless. I didn't add "print" to the parameter data because I didn't know why you were doing that. Not sure why you'd want to add a line break at start of an array but something like arr[0] = '\n' + arr[0]; might do what you're asking.
11th Jul 2020, 10:54 PM
Josh Greig
Josh Greig - avatar
11th Jul 2020, 11:10 PM
Jônatas Araripe
Jônatas Araripe - avatar