Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ <h1>Loops Exercises</h1>

<section id="menu">
<ul>
<li><button type="button" class="btn btn-primary" onclick="yourFunctionName()">Example 2.1</button></li>
<li><button type="button" class="btn btn-primary" onclick="yourFunctionName()">Example 2.2</button></li>
<li><button type="button" class="btn btn-primary" onclick="yourFunctionName()">Example 2.3</button></li>
<li><button type="button" class="btn btn-primary" onclick="yourFunctionName()">Example 2.4</button></li>
<li><button type="button" class="btn btn-primary" onclick="yourFunctionName()">Example 2.5</button></li>
<li><button type="button" class="btn btn-primary" onclick="yourFunctionName()">Example 2.6</button></li>
<li><button type="button" class="btn btn-primary" onclick="yourFunctionName()">Example 2.7</button></li>
<li><button type="button" class="btn btn-primary" onclick="yourFunctionName()">Example 2.8</button></li>
<li><button type="button" class="btn btn-primary" onclick="yourFunctionName()">Example 2.9</button></li>
<li><button type="button" class="btn btn-primary" onclick="yourFunctionName()">Example 2.10</button></li>
<li><button type="button" class="btn btn-primary" onclick="oneToTen()">Example 2.1</button></li>
<li><button type="button" class="btn btn-primary" onclick="oddNumbers()">Example 2.2</button></li>
<li><button type="button" class="btn btn-primary" onclick="squares()">Example 2.3</button></li>
<li><button type="button" class="btn btn-primary" onclick="random4()">Example 2.4</button></li>
<li><button type="button" class="btn btn-primary" onclick="even(20)">Example 2.5</button></li>
<li><button type="button" class="btn btn-primary" onclick="powers(8)">Example 2.6</button></li>
<li><button type="button" class="btn btn-primary" onclick="areWeThereYet()">Example 2.7</button></li>
<li><button type="button" class="btn btn-primary" onclick="triangle()">Example 2.8</button></li>
<li><button type="button" class="btn btn-primary" onclick="tableSquare()">Example 2.9</button></li>
<li><button type="button" class="btn btn-primary" onclick="tableSquares(6)">Example 2.10</button></li>
</ul>
</section>

Expand Down
115 changes: 113 additions & 2 deletions js/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,117 @@

var display = document.getElementById("display");

function yourFunctionName (){
display.innerHTML = "hello";
function oneToTen (){
var output = "*** Output ***<br>oneToTen()";
for(i=1; i<11; i++){
output += "<br>" + i;
}
display.innerHTML = output;
}

function oddNumbers (){
var output = "*** Output ***<br>oddNumbers()";
for(i=1; i<21; i++){
if(i%2 != 0){
output += "<br>" + i;
}
}
display.innerHTML = output;
}

function squares (){
var output = "*** Output ***<br>squares()";
for(i=1; i<11; i++){
output += "<br>" + (i*i);
}
display.innerHTML = output;
}

function random4 (){
var output = "*** Output ***<br>random4()";

for(i=1; i<5; i++){
output += "<br>" + Math.floor((Math.random()*10)+1);
}
display.innerHTML = output;
}

function even (n){
var output = "*** Output ***<br>even("+n+")";

for(i=1; i<n; i++){
if(i%2 == 0){
output += "<br>" + i;
}
}
display.innerHTML = output;
}

function powers (n){
var output = "*** Output ***<br>powers("+n+")";

for(i=1; i<n+1; i++){
output += "<br>" + Math.pow(2, i);
}
display.innerHTML = output;
}

function areWeThereYet (){
var input = prompt("Are we there yet?");

while(input.toUpperCase() !== "YES")
{
input = prompt("Are we there yet?");
}

display.innerHTML = "Good!";
}

function triangle (){
var output = "*** Output ***<br>triangle()<br>"
for(i=1; i<6; i++){
for(j=1; j<i+1; j++){
output += "*";
}
output += "<br>";
}
display.innerHTML = output.trim();
}

function tableSquare (){
var output = "*** Output ***<br>tableSquare()<br>A 4 x 4 table square<br>";

for (i = 1; i < 5; i++){
for (j = 1; j < 5; j++){
if (i*j < 10 && j>=3){
output += "|&nbsp" +"&nbsp" +"&nbsp" + i * j + "&nbsp";
}else{
output += "|&nbsp" + i * j + "&nbsp";
}
}
output += "|<br>";
}
display.innerHTML = output.trim();
}

function tableSquares (n){
var output = "*** Output ***<br>tableSquares()<br>A&nbsp" + n + "&nbspx&nbsp" + n + " table square<br>";

for (var i = 1; i <= n; i++) {
for (var j = 1; j <= n; j++) {
output += "|&nbsp" + addSpace((i*j), (j*n)) + (i * j) + "&nbsp";
}
output += "|<br>";
}
display.innerHTML = output.trim();
}

function addSpace(currentNum, lastNumInCol){
var space = "&nbsp";
var diff = lastNumInCol.toString().length-currentNum.toString().length;

for (i = 0; i < diff ; i++){
space += "&nbsp"+"&nbsp";
}
return space;
}