-
Notifications
You must be signed in to change notification settings - Fork 23
Excercise 1 2 3 4 Done~ #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,16 +4,18 @@ | |
| // 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 userNumOne = parseInt(prompt("Provide the first number", "0")); | ||
| var userNumTwo = parseInt(prompt("Provide the second number", "0")); | ||
|
|
||
|
|
||
|
|
||
| var result = userNumOne - userNumTwo; | ||
| console.log(result); | ||
|
|
||
|
|
||
|
|
||
|
|
@@ -38,9 +40,21 @@ | |
| // ==================================================================== | ||
|
|
||
|
|
||
| var ordinalNum = parseInt(prompt("Provide a number", "0")); | ||
| var NumSplit = ordinalNum.split(""); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line 44 throws an error. The .split() method works on strings, but not integers, while in line 43, you've made sure that ordinalNum is an integer...
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it be better to just prompt the user input as a string in order to split it better |
||
| var NumSlice = NumSplit.slice(-1)[0]; | ||
| //split into Array | ||
| //get last index of array and compare it | ||
|
|
||
|
|
||
|
|
||
| if(NumSlice == 1){ | ||
| console.log(ordinalNum + "st"); | ||
| }else if (NumSlice == 2) { | ||
| console.log(ordinalNum + "nd"); | ||
| }else if (NumSlice == 3) { | ||
| console.log(ordinalNum + "rd"); | ||
| }else { | ||
| console.log(ordinalNum + "th"); | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
@@ -61,9 +75,10 @@ | |
| // Write your solution below. | ||
| // ======================================================================= | ||
|
|
||
|
|
||
|
|
||
|
|
||
| //need to specify range for math random | ||
| //math.floor to round up numbers? | ||
| var randomnumber = Math.floor(Math.random() * (10 - 1) + 1); | ||
| console.log(randomnumber); | ||
|
|
||
|
|
||
|
|
||
|
|
@@ -90,6 +105,15 @@ | |
| // ===================================================================== | ||
|
|
||
|
|
||
| var foo = Math.random(); | ||
|
|
||
| //answer below still figuring out | ||
| //Math.random().toString(36).substring(7); | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
@@ -138,10 +162,10 @@ var testArray = [1, 2, 4, 8, 16, 32, 64, 128]; | |
|
|
||
| // ========================== 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. | ||
|
|
@@ -154,7 +178,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. | ||
|
|
@@ -195,7 +219,7 @@ var Phonebook = { | |
| // ===================================================================== | ||
| // 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. | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But this gives -1 if userNumOne is 1 and userNumTwo is 2, when the difference should be 1... Maybe a if-else statement is needed here to carry out an extra step if the initial result is negative?