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
16 changes: 16 additions & 0 deletions js/q1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
var friends = [
'Moe',
'Larry',
'Curly',
'Jane',
'Emma',
'Elizabeth',
'Elinor',
'Mary',
'Darcy',
'Grey',
'Lydia',
'Harriet'
];

console.log(friends[friends.length-1]);
8 changes: 8 additions & 0 deletions js/q10.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
var ages = [83, 53, 37, 29, 60, 30, 66, 19, 59, 41, 9, 64, 19, 80, 24, 53, 70, 1, 53, 40, 92, 4, 71, 65, 8, 2, 51, 80, 94, 37, 80, 64, 19, 6, 14];

ages.sort(function(a, b){return a-b})

var medianElement = (ages.length-1)/2

console.log(ages.length)
console.log(ages[medianElement])
20 changes: 20 additions & 0 deletions js/q2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
var friends = [
'Moe',
'Larry',
'Curly',
'Jane',
'Emma',
'Elizabeth',
'Elinor',
'Mary',
'Darcy',
'Grey',
'Lydia',
'Harriet'
];

friends = friends.sort();

for(var i = 0 ; i < friends.length ; i++) {
console.log(friends[i]);
}
26 changes: 26 additions & 0 deletions js/q3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
var friends = 'Moe,Larry,Curly,Jane,Emma,Elizabeth,Elinor,Mary,Darcy,Grey,Lydia,Harriet';
var word = "";
var newArr = [];

// loop through friends variable till last char
for(var i = 0 ; i < friends.length ; i++) {
// everytime a comma is read, push word into newArr, then reset word to an empty string
if(friends[i] === ',') {
if(word !== "") {
newArr.push(word);
word = "";
}
}
// else append a single character to word
else {
word += friends[i];
}
}

// push last word to the array
newArr.push(word);

// run through newArr and print to console
for(var i = 0 ; i < newArr.length ; i++) {
console.log(newArr[i]);
}
29 changes: 29 additions & 0 deletions js/q4.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
var friends = 'Moe,Larry,Curly,Jane,Emma,Elizabeth,Elinor,Mary,Darcy,Grey,Lydia,Harriet';
var word = "";
var newArr = [];

// loop through friends variable till last char
for(var i = 0 ; i < friends.length ; i++) {
// everytime a comma is read, push word into newArr, then reset word to an empty string
if(friends[i] === ',') {
if(word !== "") {
newArr.push(word);
word = "";
}
}
// else append a single character to word
else {
word += friends[i];
}
}

// push last word to the array
newArr.push(word);

// reverse newArr
newArr.reverse();

// run through newArr and print to console
for(var i = 0 ; i < newArr.length ; i++) {
console.log(newArr[i]);
}
37 changes: 37 additions & 0 deletions js/q5.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
var myFriends = [
'Rickon',
'Meera',
'Hodor',
'Jojen',
'Osha',
'Rickard',
'Maester',
'Rodrik',
'Jory',
'Septa',
'Jon'
];

var yourFriends = [
'Bilbo',
'Boromir',
'Elrond',
'Faramir',
'Frodo',
'Gandalf',
'Legolas',
'Pippin'
];

// loop through each element in the yourFriends array and append to the myFriends array
for(var i = 0 ; i < yourFriends.length ; i++) {
myFriends.push(yourFriends[i]);
}

myFriends.sort(); // sort the myFriends array alphabetically
myFriends.reverse(); // reverse the myFriends array

// loop through each element in the myFriends array and log to console
for(var i = 0 ; i < myFriends.length ; i++) {
console.log(myFriends[i]);
}
33 changes: 33 additions & 0 deletions js/q6.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
var foods = [
'Popcorn',
'Potato chips',
'Shrimp',
'Chicken rice',
'Poutine',
'Tacos',
'Toast',
'French Toast',
'Crab',
'Pho',
'Lasagna',
'Brownie',
'Lobster',
'Donuts',
'Ice cream',
'Hamburger',
'Sushi',
'Chocolate',
'Pizza'
];

var rank;

// loop through the food array and search for 'Pho', then store the value of i+1 into rank
for(var i = 0 ; i < foods.length ; i++) {
if(foods[i] === 'Pho') {
rank = i+1
}
}

// print rank
console.log("Pho is rank " + rank)
33 changes: 33 additions & 0 deletions js/q7.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
var foods = [
'Popcorn',
'Potato chips',
'Shrimp',
'Chicken rice',
'Poutine',
'Tacos',
'Toast',
'French Toast',
'Crab',
'Pho',
'Lasagna',
'Brownie',
'Lobster',
'Donuts',
'Ice cream',
'Hamburger',
'Sushi',
'Chocolate',
'Pizza'
];

// loop through the food array and search for 'Donuts', then remove
for(var i = 0 ; i < foods.length ; i++) {
if(foods[i] === 'Donuts') {
foods.splice(i, 1)
}
}

// print rank
for(var i = 0 ; i < foods.length ; i++) {
console.log(foods[i])
}
30 changes: 30 additions & 0 deletions js/q8.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
var foods = [
'Popcorn',
'Potato chips',
'Shrimp',
'Chicken rice',
'Poutine',
'Tacos',
'Toast',
'French Toast',
'Crab',
'Pho',
'Lasagna',
'Brownie',
'Lobster',
'Donuts',
'Ice cream',
'Hamburger',
'Sushi',
'Chocolate',
'Pizza'
];

var rank;
// loop through the food array and log to console the foods from element position 4 to 9
for(var i = 0 ; i < foods.length ; i++) {
if( i >= 4 && i <= 9 ) {
rank = i+1
console.log("Fav food rank " + rank + " is " + foods[i])
}
}
28 changes: 28 additions & 0 deletions js/q9.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// 'Moe' is 18
// 'Larry' is 19
// 'Curly' is 20
// 'Jane' is 20
// 'Emma' is 21
// 'Elizabeth' is 18
// 'Elinor' is 23
// 'Mary' is 25
// 'Darcy' is 24
// 'Grey' is 18
// 'Lydia' is 24
// 'Harriet' is 18

var friendAges = new Object
friendAges.Moe = 18
friendAges.Larry = 19
friendAges.Curly = 20
friendAges.Jane = 20
friendAges.Emma = 21
friendAges.Elizabeth = 18
friendAges.Elinor = 23
friendAges.Mary = 25
friendAges.Darcy = 24
friendAges.Grey = 18
friendAges.Lydia = 24
friendAges.Harriet = 18

console.log(friendAges)
Loading