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
Copy file name to clipboardexpand all lines: Javascript.js
+65
Original file line number
Diff line number
Diff line change
@@ -69,6 +69,71 @@ mp >= 5 && gb >= 16
69
69
// "||" is used for "or" - to use when at least one condition is true in order to proceed.
70
70
mp>=5||gb>=16
71
71
72
+
//Making Decisions
73
+
if(condition){
74
+
//Task if Condition is true
75
+
}else{
76
+
//Task if condition is false
77
+
}
78
+
79
+
varx=2;
80
+
vary=9;
81
+
if(x>y){
82
+
document.write("x is greater than y")
83
+
}else{
84
+
document.write("x is not greater than y")
85
+
}
86
+
87
+
//Arrays
88
+
//Arrays can store multiple values in a single variable.
89
+
vararray_colours=["red","blue","green","yellow"];
90
+
//You can only store variables of the same type - so you can't have an array of strings and numbers. You'll need two separate array's. One for the Strings and one for the numbers.
91
+
//To access an array you need to use the Array Index (which starts at 0). As above "2" would return "green". To access the value of the array you need to writ ethe following.
92
+
array_colours[2]
93
+
// You can print "green" as below.
94
+
document.write(array_colours[2]);
95
+
72
96
//Loops
97
+
varmyLoop="I only want to type this once";//define your first variable
98
+
//For Loop
99
+
vari;//create the variable
100
+
for(inti=0;i<10;i++){//for loop
101
+
document.write(myLoop);//task to do
102
+
}
103
+
//Do While Loop
104
+
vari=1;
105
+
do{
106
+
document.write(myLoop);
107
+
i=i+1;
108
+
}while(i<=10);// Remember to put the semi-colon after while
109
+
110
+
//Dialog Boxes
111
+
/*Used for showcasing important messages. There are three types of dialog boxes in JavaScript: Confirmation Messages, Pop Up an Error, or Show Warning Messages.
112
+
You can also get inputs from said dialog boxes.*/
113
+
114
+
//Alert Box
115
+
// Used to display messages, or worning messages. It only has one "OK" to select and proceed.
116
+
alert("This is an Alert Box");
117
+
118
+
//Prompt Dialog Box
119
+
// This is used when the user needs to input something. It consists of two buttons, "OK" and "Cancel", and a text box to input values.
120
+
prompt("What's your name?");
73
121
122
+
//Confirm Dialog Box
123
+
/* This provides confirmation for a specific action, for example "Do you realy want to delete this picture?" - we can either accept and hit "Ok", or decline and say "Cancel".
124
+
Hitting "Ok", returns "true" otherwise it returns "false"*/
125
+
confirm("Hit Ok if you're an idiot.");
74
126
127
+
//Functions
128
+
//These are reusable lines of code that can be called upon to complete tasks. This eliminates the need to re-write the code again.
129
+
functionprint(){//define the function name
130
+
document.write("I can print this quickly like this.");
131
+
}
132
+
//This function can then be called as below.
133
+
print();
134
+
//Another example is adding two numbers together. Putting different values in the function to have it spit out a different result can be done with Parameters.
135
+
functionadd(num1,num2){
136
+
varresult=num1+num2;
137
+
document.write(result);
138
+
}
139
+
add(3,4);//In this scenario we're passing 3 and 4 as parameters to the function. num1 becomes 3, num2 becomes 4. then the result variable dtores the result and prints it out.
0 commit comments