You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
/* Improve an already existing JavaScript program by using loops and applying "DRY" programming principles.var html = '';//red = Math.floor(Math.random() * 256 ); Original Code//green = Math.floor(Math.random() * 256 ); Original Code//blue = Math.floor(Math.random() * 256 ); Original Code//rgbColor = 'rgb(' + red + ',' + green + ',' + blue + ')'; // Original code//html += '<div style="background-color:' + rgbColor + '"></div>';////red = Math.floor(Math.random() * 256 );//green = Math.floor(Math.random() * 256 );//blue = Math.floor(Math.random() * 256 );//rgbColor = 'rgb(' + red + ',' + green + ',' + blue + ')';//html += '<div style="background-color:' + rgbColor + '"></div>';////red = Math.floor(Math.random() * 256 );//green = Math.floor(Math.random() * 256 );//blue = Math.floor(Math.random() * 256 );//rgbColor = 'rgb(' + red + ',' + green + ',' + blue + ')';//html += '<div style="background-color:' + rgbColor + '"></div>';////red = Math.floor(Math.random() * 256 );//green = Math.floor(Math.random() * 256 );//blue = Math.floor(Math.random() * 256 );//rgbColor = 'rgb(' + red + ',' + green + ',' + blue + ')';//html += '<div style="background-color:' + rgbColor + '"></div>';////red = Math.floor(Math.random() * 256 );//green = Math.floor(Math.random() * 256 );//blue = Math.floor(Math.random() * 256 );//rgbColor = 'rgb(' + red + ',' + green + ',' + blue + ')';//html += '<div style="background-color:' + rgbColor + '"></div>';////red = Math.floor(Math.random() * 256 );//green = Math.floor(Math.random() * 256 );//blue = Math.floor(Math.random() * 256 );//rgbColor = 'rgb(' + red + ',' + green + ',' + blue + ')';//html += '<div style="background-color:' + rgbColor + '"></div>';////red = Math.floor(Math.random() * 256 );//green = Math.floor(Math.random() * 256 );//blue = Math.floor(Math.random() * 256 );//rgbColor = 'rgb(' + red + ',' + green + ',' + blue + ')';//html += '<div style="background-color:' + rgbColor + '"></div>';////red = Math.floor(Math.random() * 256 );//green = Math.floor(Math.random() * 256 );//blue = Math.floor(Math.random() * 256 );//rgbColor = 'rgb(' + red + ',' + green + ',' + blue + ')';//html += '<div style="background-color:' + rgbColor + '"></div>';////red = Math.floor(Math.random() * 256 );//green = Math.floor(Math.random() * 256 );//blue = Math.floor(Math.random() * 256 );//rgbColor = 'rgb(' + red + ',' + green + ',' + blue + ')';//html += '<div style="background-color:' + rgbColor + '"></div>';////red = Math.floor(Math.random() * 256 );//green = Math.floor(Math.random() * 256 );//blue = Math.floor(Math.random() * 256 );//rgbColor = 'rgb(' + red + ',' + green + ',' + blue + ')';//html += '<div style="background-color:' + rgbColor + '"></div>';document.write(html);*/// Create an empty variable html for the basevarhtml='';varrgbColor;// Create a FUNCTION for the random valuefunctionrandomRGB(){returnMath.floor(Math.random()*256);}// Create a FUNCTION that product random RGB( value, value, value) so we can always re-use it anywherefunctionrandomColor(){// Create a SCOPE variable inside the FUNCTION with the CSS rule of RGB()varcolor="rgb(";// Add the value from the FUNCTION randomRGB// Translate into color = color + randomRGB() + ",";// OR rgb( + randomRGB() + ,// This is first value for the RGB CSS rulecolor+=randomRGB()+',';// Add second valuecolor+=randomRGB()+',';// Add third value and close the RGB CSS rulecolor+=randomRGB()+')';// Return the valuereturncolor;}// Create a FOR loopfor(vari=0;i<=10;i++){// Call the GLOBAL variable of rgbColor and add in the FUNCTION randomColorrgbColor=randomColor();// Add in HTML value for the empty variable htmlhtml+='<div style="background-color:'+rgbColor+'"></div>';}// Create a FUNCTION printfunctionprint(message){document.write(message);}print(html);
Build a Quiz challenge
/* Use loops and an array to create a quiz application, that tracks the number of quiz questions answered correctly. */// Create a variable ARRAY with questionsvarqs=[['2+2 ?',4],['colour of the sky?','blue']];// Create a base variable for counting the right answervarcorrectA=0;varquestion;varresponse;// Create 2 empty array to store the answervarcorrect=[];varwrong=[];for(vari=0;i<qs.length;i++){question=qs[i][0];answer=qs[i][1];response=prompt(question);// Use IF statement to capture the correct number of answersif(response===answer){correct.push(question);correctA++;}else{wrong.push(question);}}
The Build an Object Challenge
// Use your knowledge of JavaScript objects to create an array of student data. Each student's information is represented by an object.varstudents=[{name: 'Dave',track: 'Front End Development',achievements: 158,points: 14730},{name: 'Jody',track: 'iOS Development with Swift',achievements: '175',points: '16375'},{name: 'Jordan',track: 'PHP Development',achievements: '55',points: '2025'},{name: 'John',track: 'Learn WordPress',achievements: '40',points: '1950'},{name: 'Trish',track: 'Rails Development',achievements: '5',points: '350'}];// Create an empty variable of message will hold a string of HTML that will build up in a loopvarmessage=" ";// We will use the student variable in the loop to hold a studen object each time the loop runsvarstudentData;// Create a function to print to HTMLfunctionprint(message){varoutPut=document.getElementById("output");outPut.innerHTML=message;}for(vari=0;i<students.length;i++){studentData=students[i];message+="<h2>Student: "+studentData.name+"</h2>";message+="<p>Track: "+studentData.track+"</p>";message+="<p>Achievements: "+studentData.achievements+"</p>";message+="<p>Points: "+studentData.points+"</p>";}
The Student Record Search Challenge
/* Use your knowledge of arrays, objects and loops to create a program that searches a database of students for information on a individual student. */varmessage='';varstudent;varsearch;functionprint(message){varoutputDiv=document.getElementById('output');outputDiv.innerHTML=message;}functiongetStudentReport(student){varreport='<h2>Student: '+student.name+'</h2>';report+='<p>Track: '+student.track+'</p>';report+='<p>Points: '+student.points+'</p>';report+='<p>Achievements: '+student.achievements+'</p>';returnreport;}while(true){search=prompt("Search student records: type a name or type quit to end");if(search===null||search.toLowerCase()==="quit"){break;}for(vari=0;i<students.length;i+=1){student=students[i];if(student.name===search){message=getStudentReport(student);print(message);}}}