Skip to content
Prev Previous commit
Next Next commit
JS1 Week1 Exercises
  • Loading branch information
GMouzakis committed Feb 2, 2020
commit a636abe08e77b29a2c5ab2040c52e15129e0e38d
16 changes: 11 additions & 5 deletions Week1/homework/js-exercises/JS1_w1e10_arrCompare.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
'use strict'

const arr1 = [123,234,0,1];
const arr2 = [192,168,1,1,8080];
const arr1 = [123,234,0,1]; //1
const arr2 = [192,168,1,1,255,0,0]; //1

console.log("The length of the arr1 is " + arr1.length + " and the length of the arr2 is " + arr2.length);
console.log("The length of the arr1 is " + arr1.length + " and the length of the arr2 is " + arr2.length); //2

if(arr1.length === arr2.length) console.log("arr1 and arr2 has the same length");
else console.log("arr1 and arr2 has not the same length");
if(arr1.length === arr2.length) console.log("arr1 and arr2 has the same length"); //3
else console.log("arr1 and arr2 has not the same length"); //3

/*
1. Declare 2 variables, that each hold an array. The first array should have 4 items, the second 7 items
2. Find out how to get the length of each array. Write a `console.log` statement that shows the length of each array
3. Write a conditional statement that checks if both are of equal length. If they are, log to the console `They are the same!`, if not log `Two different sizes`
*/
23 changes: 17 additions & 6 deletions Week1/homework/js-exercises/JS1_w1e3_logTheNum.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
'use strict'

let numberX;
console.log("numberX is undefined.");
console.log(numberX);
numberX = 1;
console.log("now numberX is integer.");
console.log(numberX);

let numberX; //1
console.log("numberX is undefined."); //2
console.log(numberX); //3
numberX = 1; //4
console.log("now numberX is integer."); //5
console.log(numberX); //6


/*Follow the steps. Make sure that each step is written on the line after.

1. First, declare your variable `numberX`. Do not _initialize_ it (which means, don't give it a value) yet.
2. Add a `console.log` statement that explains in words _what you think_ the value of `x` is, like in this example.
3. Add a `console.log` statement that logs the value of `numberX`.
4. Now _initialize_ your variable `numberX` with a number (also called an `integer` in computer science terms).
5. Next, add a `console.log` statement that explains _what you think_ the value of `numberX` is.
6. Add a `console.log` statement that logs the value of `numberX`.*/
20 changes: 14 additions & 6 deletions Week1/homework/js-exercises/JS1_w1e4_logTheString.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
'use strict'

let myString = "George Mouzakis";
console.log("myString variable is a string.")
console.log(myString);
myString = "Hello World";
console.log("myString variable still to be a string.")
console.log(myString);
let myString = "George Mouzakis"; //1
console.log("myString variable is a string.") //2
console.log(myString); //3
myString = "Hello World"; //4
console.log("myString variable still to be a string.") //5
console.log(myString); //6

/*
1. Declare a variable `myString` and assign a string to it. Use your full name, including spaces, as the content for the string.
2. Write a `console.log` statement in which you explain in words _what you think_ the value of the string is.
3. Now `console.log` the variable `myString`.
4. Now reassign to the variable `myString` a new string.
5. Just like what you did before write a `console.log` statement that explains in words _what you think_ will be logged to the console.
6. Now console.log `myString` again.*/
20 changes: 14 additions & 6 deletions Week1/homework/js-exercises/JS1_w1e5_roundAndLog.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
'use strict'

let z = 7.25;
console.log(z);
let a = Math.round(z);
console.log(a);
let z = 7.25; //1
console.log(z); //2
let a = Math.round(z); //3
console.log(a); //4
let x = 0;
if (z<a) x = a;
if (z<a) x = a; //5
else x = z;
console.log(x);
console.log(x); //6

/*
1. Declare a variable `z` and assign the number 7.25 to it.
2. Write a `console.log` statement in which you log the value of `z`.
3. Declare another variable `a` that has the value of `z` but rounded to the nearest integer.
4. Write a `console.log` statement in which you log the value of `a`.
5. So now we have `z` and `a` find a way to compare the two values and store the highest of the two in a new variable.
6. Write a `console.log` statement in which you log the value of the highest value.*/
23 changes: 16 additions & 7 deletions Week1/homework/js-exercises/JS1_w1e6_logTheAnimals.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
'use strict'

let myArr = new Array;
console.log("Now myArr variable is an array without elements or an empty array");
console.log(myArr);
let animalsCollection = ["spider","scorpion","mantis"];
console.log(animalsCollection);
animalsCollection.push("Piglet");
console.log(animalsCollection);
let myArr = new Array; //1
console.log("Now myArr variable is an array without elements or an empty array"); //2
console.log(myArr); //3
let animalsCollection = ["spider","scorpion","mantis"]; //4
console.log(animalsCollection); //5
animalsCollection.push("Piglet"); //6
console.log(animalsCollection); //7

/*
1. Declare variable and assign to it an empty array. Make sure that the name of the variable indicates it contains more than 1 item. For example `items` instead of `item`.
2. Write a `console.log` statement that explains in words _what you think_ the value of the array is.
3. Write a `console.log` statement that logs the array.
4. Create a new variable with an array that has 3 of your favorite animals, each in a different string. Make sure the name of the variables says something about what the variable contains.
5. Write a `console.log` statement that logs the second array.
6. Add a statement that adds another string ("Piglet)" to the array of animals.
7. Write a `console.log` statement that logs the second array!*/
9 changes: 7 additions & 2 deletions Week1/homework/js-exercises/JS1_w1e7_stringLength.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
'use strict'

let mySentence = "Programming is so interesting!";
console.log(mySentence.length);
let mySentence = "Programming is so interesting!"; //1
console.log(mySentence.length); // 3

/*
1. Declare a variable called `mySentence` and initialize it with the following string: "Programming is so interesting!".
2. Figure out (using Google) how to get the length of `mySentence`.
3. Write a `console.log` statement to log the length of `mySentence`.*/
25 changes: 18 additions & 7 deletions Week1/homework/js-exercises/JS1_w1e8_typeChecker.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
'use strict'

let str1 = "hi";
let str2 = "there";
let num1 = 1;
let num2 = 2;
let str1 = "hi"; //1*
let str2 = "there";
let num1 = 1;
let num2 = 2; //*1

console.log(typeof str1, typeof str2, typeof num1, typeof num2);
console.log(typeof str1, typeof str2, typeof num1, typeof num2); //3

console.log("str1 and str2 has ");
console.log("str1 and str2 has "); //2,4,5,6*
if (typeof str1 === typeof str2) console.log("the same type"); else console.log("not the same type");
console.log("str1 and num1 has ");
if (typeof str1 === typeof num1) console.log("the same type"); else console.log("not the same type");
Expand All @@ -18,4 +18,15 @@ if (typeof str2 === typeof num1) console.log("the same type"); else console.log(
console.log("str2 and num2 has ");
if (typeof str2 === typeof num2) console.log("the same type"); else console.log("not the same type");
console.log("num1 and num2 has ");
if (typeof num1 === typeof num2) console.log("the same type"); else console.log("not the same type");
if (typeof num1 === typeof num2) console.log("the same type"); else console.log("not the same type"); //*2,4,5,6

/*
Write a program that checks the data types of two variables and logs to the console `SAME TYPE` if they are the same type. If they are different types log `Not the same...`.

1. Declare 4 variables: 2 must be `strings` and 2 must be `objects`
2. Create 6 conditional statements, where for each you check if the data type of one variable is the same as the other
3. Find out how to check the type of a variable
4. Write 2 `console.log` statements to log the type of 2 variables, each with a different data type
5. Now compare the types of your different variables with one another
6. Log `Not the same...` when the types are different
*/
16 changes: 12 additions & 4 deletions Week1/homework/js-exercises/JS1_w1e9_remainderLog.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
'use strict'

let x = 7;
let x = 7; //1
console.log("x is 7 and after the 'x = x % 3' calculation, the x will be " + (x % 3));
let y = 21;

let y = 21; //2
console.log("y is 21 and after the 'y = y % 4' calculation, the y will be " + (y % 4));
let z = 13;
console.log("z is 13 and after the 'z = z % 2' calculation, the z will be " + (y % 2));

let z = 13; //3
console.log("z is 13 and after the 'z = z % 2' calculation, the z will be " + (y % 2));

/*
1. If `x` equals 7, and the only other statement is `x = x % 3`, what would be the value of `x` after the calculation?
2. If `y` equals 21, and the only other statement is `y = y % 4`, what would be the value of `y` after the calculation?
3. If `z` equals 13, and the only other statement is `z = z % 2`, what would be the value of `z` after the calculation?
*/