diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/cs4241-javascript-problems-TrevorNg.iml b/.idea/cs4241-javascript-problems-TrevorNg.iml new file mode 100644 index 0000000..d6ebd48 --- /dev/null +++ b/.idea/cs4241-javascript-problems-TrevorNg.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..dc5bfb2 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/index.html b/index.html new file mode 100644 index 0000000..ff52da6 --- /dev/null +++ b/index.html @@ -0,0 +1,10 @@ + + + + + Title + + + + + \ No newline at end of file diff --git a/problems.js b/problems.js new file mode 100644 index 0000000..a85e842 --- /dev/null +++ b/problems.js @@ -0,0 +1,135 @@ +function randomColor(uniqueVal=false){ + let hexVals=[0,1,2,3,4,5,6,7,8,9,'A','B','C','D','E','F'] + let hexCode="#"; + for (let i=0;i<6;i++){ + let r=Math.floor(Math.random()*hexVals.length); + hexCode+=hexVals[r]; + if (uniqueVal){ + hexVals.splice(r,1); + } + } + return hexCode; +} + +// Make two objects named Artist +// and Painter. Artist should have +// a function named speak() that +// when called logs "I am an artist" +// to the console. Painter should be +// able to use the Artist's speak function +// (painters are artists, after all) in some +// way, and should also have a function named +// paint() that sets the background color of the +// page to a random color whenever called. + + +function problem1(){ + const Artist = { + speak(){ + console.log("I am an artist"); + }, + }; + const Painter ={ + paint(){ + let hexCode=randomColor(); + document.getElementById("page").style.backgroundColor=hexCode; + }, + }; + Object.assign(Painter, Artist); + Painter.paint(); + Artist.speak(); + Painter.speak(); + +} +//problem1(); + + +// create a for loop that creates 20 blocks, +// all on a single row, with a random color for each +// make sure the values for each color channel are different +// (i.e. no gray/black/white blocks) + +function problem2(){ + document.getElementById("page").innerHtml=""; + for (let i=0;i<20;i++){ + + let c=randomColor(true); + let newBox=document.createElement("div"); + newBox.style.backgroundColor=c; + newBox.style.display="inline-block"; + newBox.style.width="20px"; + newBox.style.height="10px" + document.getElementById("page").appendChild(newBox); + } +} +//problem2(); + +// create a for input field that does the following +// whenever the user enters a letter in it: +// 1. Creates a

element containing the letter and appends it to the page +// 2. deletes the inputted letter from the input field so it is blank + +function problem3(){ + let docBody=document.getElementById("page"); + let input=document.createElement("input"); + let inputStr=""; + input.oninput=function(){ + if (inputStr.length