This is what I've got so far:
function steamroller(arr) {
var newArray = [];
for (var j = 0; j < arr.length; j++) {
if (Array.isArray(arr[j]) === false) {
newArray.push(arr[j]);
//flattenArray(arr[j]);
}
if (Array.isArray(arr[j]) === true) {
for (var k = 0; k < arr[j].length; k++) {
if (Array.isArray(arr[j][k]) === false) {
newArray.push(arr[j][k]);
}
if (Array.isArray(arr[j][k]) === true) {
for (var l = 0; l < arr[j][k].length; l++) {
newArray.push(arr[j][k][l]);
}
}
}
}
}
return newArray;
}
//steamroller([1, [2], [3, [[4]]]]);
//steamroller([1, [2], [3, [[4]]]]);
steamroller([1, {}, [3, [[4]]]]);
Getting there. I'd like to be able to do this in a recursive function, perhaps I'll take some Udemy courses to see if that levels me up...we'll see. Hacking away at it this way for now...
I solved it! Here's my code:
function steamroller(arr) {
var newArray = [];
for (var j = 0; j < arr.length; j++) {
if (Array.isArray(arr[j]) === false) {
newArray.push(arr[j]);
//flattenArray(arr[j]);
}
if (Array.isArray(arr[j]) === true) {
for (var k = 0; k < arr[j].length; k++) {
if (Array.isArray(arr[j][k]) === false) {
newArray.push(arr[j][k]);
}
if (Array.isArray(arr[j][k]) === true) {
for (var l = 0; l < arr[j][k].length; l++) {
if (Array.isArray(arr[j][k][l]) === true) {
//the alternative to the line below would be another for loop to loop through the array, and another set of if statements within, but since the edge cases only require this level of nesting, newArray.push(arr[j][k][l][0]); does the job.
newArray.push(arr[j][k][l][0]);
}
if (Array.isArray(arr[j][k][l]) === false)
newArray.push(arr[j][k][l]);
}
}
}
}
}
return newArray;
}
steamroller([1, {}, [3, [[4]]]]);
It's neat keeping focus while iterating over nested loops, I enjoyed this challenge. The next algorithm challenge is "Binary Agents." This one is complicated, here it is:
The challenge suggests using String.fromCharCode(), which requires as an input the decimal value of the letter. It also suggests using String.charCodeAt() which can be used to loop through a string. So the first thing I want to learn is how to convert binary into a decimal input for String.fromCharCode(). Working on that right now.
Whoa!!!!
This:
var convertBinary = parseInt("01000001", 2);
Assigns a value of 65 to convertBinary! That's so cool! So what parseInt is doing is it's taking a string value for the first parameter, and the second parameter is the base of the number system you want the output to be in (2 for binary). If you omit the number, it just gives you the string you entered as a number without making any conversions. So now I have to iterate through the string, grab the decimal values, and convert them to English letters with String.fromCharCode(). I solved it!
Here's my solution:
Moving on, the next problem is called "Everything Be True." Here is is:
And the first input:
I have to iterate through objects in the first parameter and return true if every object therein contains the key in the second argument. It seems pretty straightforward, and I need the practice with filtering and looping through objects, so I'm glad this challenge came up now.
I only have this challenge and one more challenge left in order to complete the Intermediate Algorithm Scripting Challenges! This is what I have so far:
function every(collection, pre) {
var objProp;
for (var i = 0; i < collection.length; i++) {
objProp = collection[i][pre];
if (objProp === false || objProp === 0 || objProp === null || objProp === undefined || objProp === NaN || objProp === "") {
objProp = false;
return objProp;
}
}
return true;
}
every([{"user": "Tinky-Winky", "sex": "male"}, {"user": "Dipsy", "sex": "male"}, {"user": "Laa-Laa", "sex": "female"}, {"user": "Po", "sex": "female"}], "sex");
The NaN is not cooperating...so, if the value === NaN, it should return false. Except that NaN is special, because it is actually a number, that is "Not a Number." So, NaN is the only thing in JavaScript that is not equal to itself...I'm working on figuring out how to test for it.
All right, that took me a while. The Boolean() function returns whether whatever you put in it is true or false, so here' s my code:
function every(collection, pre) {
var objProp;
for (var i = 0; i < collection.length; i++) {
objProp = collection[i][pre];
if (Boolean(objProp) === false) {
objProp = false;
return objProp;
}
}
return true;
}
every([{"user": "Tinky-Winky", "sex": "male"}, {"user": "Dipsy", "sex": "male"}, {"user": "Laa-Laa", "sex": "female"}, {"user": "Po", "sex": "female"}], "sex");
I'm on the last challenge!!! Woohoo!
All right, the last challenge is called "Arguments Optional." Here it is:
First, I want to understand the question, as it's somewhat convoluted, but it's getting late, so I'll leave this last problem for tomorrow.
SUMMARY OF CODING SKILLS
Total Treehouse Points: 5,503
Treehouse Points by Subject Matter (Miscellaneous not included):
HTML: 663
CSS: 1,599
Design: 1,193
Development Tools: 747
JavaScript: 1,239
Treehouse Ranking (%): "You have more total points than 93% of all students."
Treehouse Courses Completed:
How to Make a Website
HTML
CSS Foundations
CSS Layout Techniques
Aesthetic Foundations
Design Foundations
Adobe Photoshop Foundations
Adobe Illustrator Foundations (66% complete, switched focus from web design to web dev)
Console Foundations
Git Basics
Introduction to Programming
JavaScript Basics
Codecademy (& other) Courses Completed:
HTML and CSS (Codecademy)
Design Foundations
Adobe Photoshop Foundations
Adobe Illustrator Foundations (66% complete, switched focus from web design to web dev)
Console Foundations
Git Basics
Introduction to Programming
JavaScript Basics
Codecademy (& other) Courses Completed:
HTML and CSS (Codecademy)
Books Read or in Progress:
Completed: "Head First HTML and CSS," by E. Robson & E. Freeman
Completed: "A Smarter Way to Learn JavaScript," by Mark Myers
Completed: "HTML and CSS," by Jon DuckettCompleted: "JavaScript and JQuery," by Jon Duckett
My Progress on The Odin Project:
1. Introduction to Web Development 100% Complete
2. Web Development 101 33% Complete
Note: I switched to FCC for the great online community and better updates/support.
Note: I switched to FCC for the great online community and better updates/support.
My Progress on Free Code Camp (FCC):
1. Get Started with Free Code Camp Complete
2. HTML5 and CSS Complete
3. Responsive Design with Bootstrap Complete
4. Gear up for Success Complete
5. jQuery Complete
6. Basic JavaScript Complete
7. Object Oriented and Functional Programming Complete
8. Basic Algorithm Scripting Complete
9. Basic Front End Development Projects Complete
10. Intermediate Algorithm Scripting On 21 of 21
11. JSON API's and Ajax
12. Intermediate Front End Development Projects
13. Claim Your Front End Development Certificate
14. Upper Intermediate Algorithm Scripting
15. Automated Testing and Debugging
16. Advanced Algorithm Scripting
17. AngularJS
18. Git
19. Node.js and Express.js
20. MongoDB
21. Full Stack JavaScript Projects
22. Claim Your Full Stack Development Certificate
After the 800 hours of FCC work above, there are 800 more hours of non-profit coding projects.
Hours Spent Coding Today: 4
Total Hours Coding: 865
No comments:
Post a Comment