← Back to feed
Renewal·서른의 생활코딩

Sample Source 3) JavaScript Basic Document Structure (Arrays Part 2)

NS
normalstory
cover image


This is a three-stage combo example about arrays.
문제는   이고,

 

the first combo is in this color,  
: the code I first wrote when approaching this... this is how I approached it... though I couldn't finish it...
the second combo is this,
: the model answer. 

The final third combo is in this color.
I didn't like the model answer because it just inputs things without any explanation.
So when entering values, the example asks what to enter, and after entering, it shows what was entered. 





<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ko" lang="ko">




<head>


<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=UTF-8" />




<title> Array 2 </title>



<script type="text/javascript">


//<!CDATA[




/* Problem :: code that takes 5 subjects through prompts and prints the average of the entered values


(use prompt only once and run a for loop)




//var kor="Korean";


//var eng="English";


//var math="Math";


//var music="Music";


//var photo="Photography";




var sum = kor + eng + math + music + photo;


var evg = sum/5;




for(i=0; i<5 ; i++){


var sung=prompt("Enter the score","");


sung=[ kor , eng , math , music , photo];


//document.write( sung[i] + "<br />");


//document.write( i + "<br />");


document.write( evg + "<br />");


}




//답


var sung=new Array(5);


var sum=0;




for(i=0; i<5 ; i++){


//var sung=prompt("Enter the score","");


sung[i] = parseInt(prompt("Enter score")); //parseInt converts string to number


sum += sung[i];


}


document.write("Average: "+sum/5);


*/




// Answer + extra study


var sung=new Array(5);


var sum=0;
 


var sub=new Array("Korean","English","Math","Science","Art");




for(i=0; i<5 ; i++){


sung[i] = parseInt(prompt(sub[i] + " score", "")); //parseInt = string to number


sum += sung[i];


}






/*


for(j=0; j<5 ; j++){


for(i=0; i<5 ; i++){


sung[i] = parseInt(prompt(sub[j] + " score")); //parseInt = string to number


sum += sung[i];


}


}


*/


document.write(sub[0] + "=" + sung[0] + "<br />");


document.write(sub[1] + "=" + sung[1] + "<br />");


document.write(sub[2] + "=" + sung[2] + "<br />");


document.write(sub[3] + "=" + sung[3] + "<br />");


document.write(sub[4] + "=" + sung[4] + "<br />");




document.write("Average: "+sum/5);




//]]]]]]>


</script>




</head>




<body>


</body>




</html>



This English version was translated by Claude.

친절한 찰쓰씨
Written by
친절한 찰쓰씨

Pleasant Charles — UI/UX researcher at AIT. Keeping notes on design, planning, and slow days here since 2010.