Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Micah Caffey
1,391 PointsNeed help on a question!
I am just starting out JavaScript track, not sure if i'm to working out these sort of problems..but could someone walk through this one with me? It seems very trivial but I havent worked these type of problems before.
"Write a function that takes an array of integers and prints each element in that array squared"
4 Answers
james south
Front End Web Development Techdegree Graduate 33,271 Pointsyou can do this with a loop that prints the square of each element with each pass through the loop. 2-3 lines of code.
andren
28,558 PointsIf you have just started with the JavaScript track then i'd say that challenge is a bit above the level you should be working with at the moment.
It's not a very complicated task if you are familiar with functions, arrays and loops (for loop ideally). But if those are somewhat foreign concept to you then I'd recommend waiting until you learn about those concepts first before tackling something like that.
Also you don't specify how much help you wish to receive on this challenge. I can give you a general check list for what steps you have to take for such a task but as I mentioned above I think this task might be something you can come back to later.
To solve the challenge you would have to:
Define a function with one parameter which would be the array.
Create a loop (
forloop preferably) that looped though all of the items in the array.Inside the loop use the loop variable to pull out the individual items from the array, then multiply the item with itself and then print out the result of that operation.
You could also use the forEach function instead of a loop, but that requires some knowledge of callbacks which I doubt you have at this point.
Robert Pantschyschak
6,136 PointsHi Micah, Not sure if you need to log each result to the console, or return a new array with the squared values, the below solution should work if you just need to print the squared values to the console.
function squareArray (values) {
for (var i=0; i<values.length; i++) {
console.log(values[i]*values[i]);
}
}
If you instead need to return a new array containing the squared values, you can make just a few modifications to the above code:
function squareArray (values) {
var newValues = []; //Creates a new array to hold the new values.
for (var i=0; i<values.length; i++) {
newValues.push(values[i]*values[i]); //Adds each squared value to the next position of the new array.
}
return newValues; //Returns the new array.
}
Micah Caffey
1,391 PointsThank you for the responses! I figured that it was something I hadn't gotten to yet. I think that further down the road over the next few weeks moving past more elementary JS i'll be able to address this problem with more understanding. I was just trying to get an idea of how far along this problem was in terms of where I was at the moment, so pardon the jumbled question.