0

This piece of code work as intended. But is there a possible way for me to improve its quality and for me to lessen the globally scoped variables? Perhaps by turning them into a function?

PS: There are several array methods used for a single goal of adding elements into an array to show that I know how to use them properly.

var parts = [];
let message = ""

parts.unshift("face");
message += parts + "\n\n" 

parts.push("eyes");
message += parts + "\n\n" 

parts[2] = " nose"
message += parts + "\n\n" 

parts.splice(3, 1, " lips", " eyebrows")
message += parts + "\n\n" 

var parts2 = parts.concat([" ears", " and lips"])

message + parts2
JSNoob
  • 5
  • 2
  • Put the code in an IIFE to create a local scope. See https://stackoverflow.com/questions/8228281/what-is-the-function-construct-in-javascript – Barmar Aug 14 '20 at 18:44
  • What is `bodyParts`? – Barmar Aug 14 '20 at 18:45
  • a revealing module pattern is a nice and common structure, and something other then II(A)FEs one should learn https://stackoverflow.com/questions/5647258/how-to-use-revealing-module-pattern-in-javascript also check out https://addyosmani.com/resources/essentialjsdesignpatterns/book/#designpatternsjavascript for some other patterns – Lawrence Cherone Aug 14 '20 at 18:53
  • That's actually my problem. I'm confused with the syntax of IIFE (now I know what it's called. Thanks.) so IDK how to put those codes inside such function. – JSNoob Aug 14 '20 at 18:53
  • Okay. I got it now. Thanks! – JSNoob Aug 14 '20 at 19:06

0 Answers0