Skip to content
Open
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
182 changes: 94 additions & 88 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,27 @@
// be 1. If the user gives the numbers 3 and 2, the result should still
// be 1.
//
// Hint: To get the user input and store it in a variable, we can do
// Hint: To get the user input and store it in a variable, we can do
// `var someVar = prompt("Give me a high five");`. Consider asking for 1
// number at a time first.
// ====================================================================
// Write your solution below.
// ====================================================================


var firstNumber = prompt("Enter Your First Number");
var secondNumber = prompt("Enter Your Second Number");

firstNumber = parseInt(firstNumber);
secondNumber = parseInt(secondNumber);










if (firstNumber && secondNumber) {
if (firstNumber > secondNumber) {
console.log(firstNumber - secondNumber);
} else {
console.log(secondNumber - firstNumber);
}
};

// ========================== Exercise 2 ==============================
// Prompt the user for a number, then console.log its ordinal version.
Expand All @@ -38,20 +40,26 @@
// ====================================================================
















var userNumber = prompt("Enter a number");
userNumber = parseInt(userNumber);

if (userNumber) {
var lastone = userNumber.toString().split('').pop();
switch(lastone) {
case "1":
console.log(userNumber + "st");
break;
case "2":
console.log(userNumber + "nd");
break;
case "3":
console.log(userNumber + "rd");
break;
default:
console.log(userNumber + "th");
break;
}
};

// ========================== Exercise 3 =================================
// Generate a random integer (hint: search Google or MDN) between 1 to 10.
Expand All @@ -61,20 +69,14 @@
// Write your solution below.
// =======================================================================

var randomNumber = Math.floor( (Math.random() * 10) + 1 );
var userNumber = prompt("Enter an Integer between 1 to 10");

userNumber = parseInt(Math.floor(userNumber));













if (userNumber == randomNumber) {
console.log("Your number matched the randomly generated one!");
};
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, randomNumber actually goes from 0 to 9 because of the Math.floor function, e.g. if Math.random() gives 0.001, then Math.floor(0.001 * 10) gives 0. At the same time, Math.random() has a practically 0 chance of returning 1, which is needed in order for Math.floor to return 10 here.


// ========================== Exercise 4 ===============================
// Generate a random string of length 6 alphanumeric characters.
Expand All @@ -89,21 +91,19 @@
// Write your solution below.
// =====================================================================

var desiredRandomLength = prompt("How long would you like a string? (Please enter a number)");
var resultingString = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

desiredRandomLength = parseInt(desiredRandomLength);

if (desiredRandomLength) {
for (i=0; i < desiredRandomLength; i++) {
resultingString += possible.charAt(Math.floor(Math.random() * possible.length));
}













console.log(resultingString)
};



Expand All @@ -117,31 +117,22 @@

var testArray = [1, 2, 4, 8, 16, 32, 64, 128];

var jumbledArray = [];

while (testArray.length > 0) {
var randomArrayIndex = Math.floor(Math.random() * testArray.length)
var randomArrayDigit = testArray.splice(randomArrayIndex, 1);
jumbledArray.push(parseInt(randomArrayDigit));
}

















console.log(jumbledArray);

// ========================== Exercise 6 ==============================
// Examine the phonebook below.
//
//
// 1. Prompt the user for a name, and show
// (in any way, e.g. console.log) the phone number corresponding to
// the given name.
// (in any way, e.g. console.log) the phone number corresponding to
// the given name.
//
// 2. Prompt the user for a name and a number. Update the number
// corresponding to the name if the name exists in the phonebook.
Expand All @@ -154,7 +145,7 @@ var testArray = [1, 2, 4, 8, 16, 32, 64, 128];
//
// Bonus: Prompt the user for a number. If the number exists in the
// phonebook, show the name corresponding to it.
// Hint: The phonebook is a JS Object. Search Google / MDN for
// Hint: The phonebook is a JS Object. Search Google / MDN for
// 'Javascript Object methods' to see if there might be something useful.
// ====================================================================
// Write your solution below.
Expand All @@ -168,34 +159,42 @@ var Phonebook = {
"Nelson Tan" : 63396565
};

var promptKeys = Object.keys(Phonebook).join(", ");
var userPromptOneName = prompt("Please Enter One of the Names: " + promptKeys);

if (Phonebook[userPromptOneName]) {
console.log(userPromptOneName + "'s phone number is: " + Phonebook[userPromptOneName]);
} else {
console.log("Invalid name");
};

var userPromptTwoName = prompt("Please Enter One of the Names: " + promptKeys);
var userPromptTwoNumber = prompt("Enter the person's new phone number");

if (Phonebook[userPromptTwoName]) {
Phonebook[userPromptTwoName] = userPromptTwoNumber;
};

var userPromptThreeName = prompt("Please Enter One of the Names: " + promptKeys + " or a New Name");
var userPromptThreeNumber = prompt("Enter a new phone number");

if (!Phonebook[userPromptThreeName]) {
Phonebook[userPromptThreeName] = userPromptThreeNumber;
};
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just something to keep in mind, although we will likely not be going into sufficient depth to see the impact of this, checking for object properties like this is not generally safe. For example, try typing proto into the prompt :) Look into Object methods on MDN, specifically Object.hasOwnProperty()


var newPromptKeys = Object.keys(Phonebook).join(", ");
var userPromptFourName = prompt("Please Enter One of the Names: " + newPromptKeys + "Which you would like to delete");















if (Phonebook[userPromptFourName]) {
delete Phonebook[userPromptFourName];
};

// ====================== Bonus Exercise 7 =============================
// JS Array / Object Handling - References vs Values
// =====================================================================
// A common class of bugs that programmers face occurs when they change
// an object that they didn't mean to.
//
//
// Examine the array below.
// Prompt the user for 2 integers. Swap the elements in the 2 positions
// of the array corresponding to the user input, and display the result.
Expand All @@ -210,15 +209,22 @@ var Phonebook = {

var MutateMeNot = [1, 2, 3, 4, 5];

var userFirstNumber = prompt("Enter your first number between 0-4");
var userSecondNumber = prompt("Enter your second number between 0-4");

userFirstNumber = parseInt(userFirstNumber);
userSecondNumber = parseInt(userSecondNumber);

var newArray = MutateMeNot.slice(0);

if (userFirstNumber <= MutateMeNot.length && userSecondNumber <= MutateMeNot.length) {
var secondArrayNumber = MutateMeNot[userSecondNumber];
var firstArrayNumber = MutateMeNot[userFirstNumber];

newArray[userFirstNumber] = secondArrayNumber;
newArray[userSecondNumber] = firstArrayNumber;






console.log(newArray);
};

// console.log("The value of MutateMeNot is", MutateMeNot);