-
Notifications
You must be signed in to change notification settings - Fork 0
Towersof hanoi and Loops Project #4
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: gh-pages
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 |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| 'use strict'; | ||
|
|
||
| let cars = ['Ford', 'Mazda', 'Toyota', 'Chevrolet']; | ||
| console.log(cars.length); | ||
|
|
||
| let moreCars = ['Mitsubishi', 'Tesla', 'Audi', 'Honda']; | ||
| let totalCars = cars.concat(moreCars); | ||
| console.log(totalCars); | ||
|
|
||
| console.log(moreCars.indexOf('Honda')); | ||
| console.log(cars.lastIndexOf('Ford')); | ||
|
|
||
| let stringOfCars = totalCars.join(); | ||
| console.log(stringOfCars); | ||
|
|
||
| console.log(stringOfCars.split()); | ||
|
|
||
| let carsInReverse = totalCars.reverse(); | ||
| console.log(carsInReverse); | ||
|
|
||
| console.log(carsInReverse.sort()); | ||
|
|
||
| let carsRemoved = carsInReverse.slice(2,4); | ||
| console.log(carsRemoved); | ||
|
|
||
| console.log(carsInReverse.splice(2,2, 'Ford', 'Honda')); | ||
|
|
||
| console.log(carsInReverse.push('Ford', 'Honda')); | ||
|
|
||
| carsInReverse.pop(); | ||
| console.log(carsInReverse); | ||
|
|
||
| carsInReverse.shift(); | ||
| console.log(carsInReverse); | ||
|
|
||
| carsInReverse.unshift('BMW'); | ||
| console.log(carsInReverse); | ||
|
|
||
| /*let numbers = [23, 45, 0, 2]; | ||
| function incrementTwo(item) { | ||
| console.log(item + 2); | ||
| } | ||
| numbers.forEach(incrementTwo)*/ | ||
|
|
||
| for(let i=0; i < carsInReverse.length; i++){ | ||
| console.log(carsInReverse[i]); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| let cars = ['Ford', 'Mazda', 'Toyota', 'Chevrolet']; | ||
| let moreCars = ['Mitsubishi', 'Tesla', 'Audi', 'Honda']; | ||
| let totalCars = cars.concat(moreCars); | ||
| let carsInReverse = totalCars.reverse(); | ||
| for(let i=0; i < carsInReverse.length; i++){ | ||
| console.log(carsInReverse[i]); | ||
| } | ||
|
|
||
| let persons = { | ||
| firstName: "Jane", | ||
| lastName: "Doe", | ||
| birthDate: "Jan 5, 1925", | ||
| gender: "female" | ||
| }; | ||
|
|
||
| for (var prop in persons) { | ||
| console.log(`persons.${prop} = ${persons[prop]}`); | ||
| } | ||
|
|
||
| for(key in persons){ | ||
| if (key === 'birthDate'){ | ||
| console.log(persons['birthDate']); | ||
| } | ||
| } | ||
|
|
||
| let number = 0 | ||
| while (number < 1000){ | ||
| //console.log(number); | ||
| number += 1 | ||
| } | ||
|
|
||
| do { | ||
| //console.log(number); | ||
| number +=1 | ||
| }while(number<1000); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| 'use strict'; | ||
| //create an object and assign it to a variable named "partnerObj" | ||
| //inside partnerObj make 5 key/value pairs one of wich must be a method | ||
| const partnerObj = { | ||
| firstName:"Chase", | ||
| lastName:"Turner", | ||
| age:24, | ||
| hairColor:"Black", | ||
| fullName () { | ||
| return `Hello, my name is ${this.firstName} ${this.lastName}`; | ||
| } | ||
| }; | ||
| //print out the results of the partnerObj method | ||
| partnerObj.fullName(); | ||
| //print out 2 values in partnerObj | ||
| console.log(partnerObj.age, partnerObj.hairColor); | ||
| //reassign 1 value in partnerObj to 'cars' | ||
| partnerObj.age = 'cars' | ||
|
|
||
| console.log(partnerObj); | ||
| //make an array from all of the object keys store in a variable named partnerArr | ||
| const partnerArr = Object.keys(partnerObj); | ||
| console.log(partnerArr); | ||
| //for every key in the object print out the key and the value | ||
| for (const prop in partnerObj) { | ||
| console.log(`${prop} | ||
| ${partnerObj[prop]}`); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,24 +19,56 @@ function printStacks() { | |
| console.log("c: " + stacks.c); | ||
| } | ||
|
|
||
| function movePiece() { | ||
| function movePiece(startStack,endStack) { | ||
| // Your code here | ||
|
|
||
| const startArray = getStackArrays(startStack) | ||
| const endArray = getStackArrays(endStack) | ||
| const targetPiece = startArray.pop() | ||
| endArray.push(targetPiece); | ||
| console.log(targetPiece); | ||
| console.log(endArray) | ||
| } | ||
|
|
||
| function isLegal() { | ||
| function getStackArrays(targetStack){ | ||
|
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. This function is a little redundant. For example, on line 24 you could simply run:
|
||
| return stacks[targetStack] | ||
| } | ||
| function isLegal(startStack,endStack) { | ||
| // Your code here | ||
|
|
||
| // check if end stack is empty | ||
| const startArray = getStackArrays(startStack) | ||
| const endArray = getStackArrays(endStack) | ||
|
|
||
|
|
||
| if (endArray.length === 0){ | ||
| return true | ||
| }else{ | ||
| const startItem = startArray.pop() | ||
| const endItem = endArray.pop() | ||
| if (startItem < endItem){ | ||
| return true; | ||
|
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. Your |
||
| } | ||
| } | ||
| } | ||
|
|
||
| function checkForWin() { | ||
| // Your code here | ||
|
|
||
|
|
||
| if (endArray === [4,3,2,1]){ | ||
|
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. This should check for |
||
| return 'Win'; | ||
| } | ||
| } | ||
|
|
||
| function towersOfHanoi(startStack, endStack) { | ||
| // Your code here | ||
| if (isLegal(startStack,endStack)){ | ||
|
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. Nice concise |
||
| movePiece(startStack,endStack); | ||
|
|
||
| } | ||
| else { | ||
| return "Try again."; | ||
|
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. Your code never runs |
||
| } | ||
| } | ||
|
|
||
| function getPrompt() { | ||
|
|
||
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.
Semicolon missing here.