Author Topic: Some help with my JavaScript assignment, friends  (Read 2491 times)

0 Members and 1 Guest are viewing this topic.

Offline ItsBc

  • Thread Starter
  • Posts: 90
  • Location: Irvine, California
  • BARACOOT
    • Portfolio
Some help with my JavaScript assignment, friends
« on: Thu, 09 May 2013, 16:58:29 »
any help is greeeeeatly appreciated, hints, or even completing the script so that I can study it. :D

Code: [Select]
var myNumbers = [23,24,26,82];
for (i=0;i<4;i++)
console.log(myNumbers[i]);

var doubling = function(myNumbers){
    //what do now?
};



//--
Create an array of numbers, save it to a variable
Use a loop to iterate through each element of the array
Write a separate "doubling" function that returns any number it is given multiplied by two
Pass each number from the array to the "doubling" function in turn
Save the original numbers and the doubled results as key-value pairs in an object
--//

Offline qi3ber

  • Posts: 12
  • Location: Boulder, CO
Re: Some help with my JavaScript assignment, friends
« Reply #1 on: Thu, 09 May 2013, 17:41:46 »
I am extremely reluctant to do your homework for you, and you are asking too many questions at once to handle in a single response, so I'll try to tackle part of the education you appear to need.

JavaScript is an object oriented scripting language, as such, there are a number of Object()s that you can instantiate, and as it works out, there are a number of ways to instantiate those Object()s.  You could create a new Object() and store it in a variable, or you could imply that you want to store a new object in a variable a = {}.

Objects in turn can store either functions, values or additional Object()s  as named members.  If you have an object a, you can access a member function or Object() with the syntax a.member.  That accessor is available both for read and write operations on that member.  The accessor, when referring to a static value or Object is sometimes referred to as a 'key'.

Hopefully that helps a bit, good luck
Leopold FC500 Browns

Offline n0rvig

  • Posts: 355
Re: Some help with my JavaScript assignment, friends
« Reply #2 on: Thu, 09 May 2013, 19:10:30 »
I love javascript. I'd be happy to help. I'll post some links to help you learn javascript for your assignment.

The Mozilla Javascript docs are really good.
https://developer.mozilla.org/en-US/docs/JavaScript/Guide

You can ask & read questions on Stackoverflow.
http://stackoverflow.com/questions/tagged/javascript

You're going to want to make a function for doubling.
https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Functions

You can use a for loop, a forEach, or even map to create your key-value object.

Have fun.
« Last Edit: Thu, 09 May 2013, 19:22:31 by n0rvig »

Offline ItsBc

  • Thread Starter
  • Posts: 90
  • Location: Irvine, California
  • BARACOOT
    • Portfolio
Re: Some help with my JavaScript assignment, friends
« Reply #3 on: Thu, 09 May 2013, 20:29:48 »
It's not 'homework', it's just something that I should be comfortable writing before I start this boot camp program in San Francisco.
I am currently going through codecademy.com's JS track and am about 85% complete. I dropped out of my CS program, as I feel traditional schooling just isn't for me.

Thanks for those resources n0rvig :)

Offline daerid

  • Posts: 4276
  • Location: Denver, CO
    • Rossipedia
Re: Some help with my JavaScript assignment, friends
« Reply #4 on: Fri, 10 May 2013, 11:24:02 »
Code: [Select]
/*
Create an array of numbers, save it to a variable
Use a loop to iterate through each element of the array
Write a separate "doubling" function that returns any number it is given multiplied by two
Pass each number from the array to the "doubling" function in turn
Save the original numbers and the doubled results as key-value pairs in an object
*/

(function () {
    var numbers = [23, 24, 26, 82],
        result = {},
        number,
        doubler = function (n) {
            return n * 2;
        };

    for (i = 0; i < numbers.length; ++i) {
        number = numbers[i];
        result[number] = doubler(number);
    }

    console.dir(result);
})();

Here's a fiddle that demonstrates it.

I know some may look down on providing the answer for you, but I've found that analyzing a solution that's presented and asking questions can speed up understanding (at least for me). I think of it like dissection: you don't try to understand how an organism works by trying to build one from scratch. You start with one that works, and take it apart until you understand how/why it does.
« Last Edit: Fri, 10 May 2013, 11:26:14 by daerid »

Offline qi3ber

  • Posts: 12
  • Location: Boulder, CO
Re: Some help with my JavaScript assignment, friends
« Reply #5 on: Fri, 10 May 2013, 11:43:22 »
It's not 'homework', it's just something that I should be comfortable writing before I start this boot camp program in San Francisco.

My apologies for jumping to that conclusion, hopefully you can understand how I might have misinterpreted your post.  That said, looks like daerid already posted some very nice JS for you to study.  Let us know if you need further clarification on how it works.
Leopold FC500 Browns

Offline ItsBc

  • Thread Starter
  • Posts: 90
  • Location: Irvine, California
  • BARACOOT
    • Portfolio
Re: Some help with my JavaScript assignment, friends
« Reply #6 on: Sat, 11 May 2013, 02:13:36 »
Yes! this helps a ton, daerid. Thanks a lot!

And, it's no biggy qi3ber, don't worry about it.
Just started with JS this week and I've been at it as close to 24/7 as possible, but sometimes my brain explodes. If the slightest thing distracts me, I completely lose track of where I was. WTB some adderall. :P



Offline partmont

  • Posts: 2
  • Location: California
Re: Some help with my JavaScript assignment, friends
« Reply #7 on: Mon, 20 May 2013, 17:05:25 »
I am a little confused on the last 3 parts of the question.

3) Write a separate "doubling" function that returns any number it is given multiplied by two
4) Pass each number from the array to the "doubling" function in turn
5) Save the original numbers and the doubled results as key-value pairs in an object

can someone please explain this to me. I have been using codecademy and lynda.com but i cant seem to wrap my head around this question.

im confused on the whole doubliing function and console.dir instead of console.log,

also in the function the number variable or is athat an object.

i feel like im thinking to hard about this question.

thanks in advance for any help.

Offline daerid

  • Posts: 4276
  • Location: Denver, CO
    • Rossipedia
Re: Some help with my JavaScript assignment, friends
« Reply #8 on: Mon, 20 May 2013, 20:25:26 »
I am a little confused on the last 3 parts of the question.

3) Write a separate "doubling" function that returns any number it is given multiplied by two
4) Pass each number from the array to the "doubling" function in turn
5) Save the original numbers and the doubled results as key-value pairs in an object

can someone please explain this to me. I have been using codecademy and lynda.com but i cant seem to wrap my head around this question.

im confused on the whole doubliing function and console.dir instead of console.log,

also in the function the number variable or is athat an object.

i feel like im thinking to hard about this question.

thanks in advance for any help.


Check it out, I'll write it a different way and add some comments:

Code: [Select]
// This is the function that simply doubles whatever is passed
// into it. For example:
// doubler(2) return 4
// doubler(12) returns 24
// double(36) returns 72
// etc...
function doubler(n) {
  return n * 2;
};

// This is an array containing the numbers that are going to be doubled
// This could also be written:
// var numbers = new Array(23, 24, 26, 82);
var numbers = [23, 24, 26, 82];

// This is the object that we're going to store the results in
// In JavaScript, all objects are essentially just a collection
// of key: value pairs.
var result = {};

// Here we loop through the array, using the index variable
// to keep our position and know when we're done
for (var index = 0; index < numbers.length; ++index) {

  // Here we retrieve the number that's at the current position
  var number = numbers[index];

  // ... and then double it using the doubler function...
  var doubledNumber = doubler(number);

  // ... and finally store it in the result object,
  // using the number as the key and the doubled result as the value
  result[number] = doubledNumber;
}

// console.dir() is like console.log(), but instead
// of just outputting a nice string, it outputs the
// structure of an object, showing you all the properties
// and values of that object
//
// Essentially it does this:
//
// for(var key in result) {
//   console.log(key + " = " + result[key]);
// }
console.dir(result);


Does that help?

Offline partmont

  • Posts: 2
  • Location: California
Re: Some help with my JavaScript assignment, friends
« Reply #9 on: Tue, 21 May 2013, 13:56:51 »
Yeah it does help, this was more of what my original code had looked like but i kept getting an error. I was curious as to y in the first example everything was placed in parenthasis. making it a mass function?




Offline daerid

  • Posts: 4276
  • Location: Denver, CO
    • Rossipedia
Re: Some help with my JavaScript assignment, friends
« Reply #10 on: Tue, 21 May 2013, 17:49:07 »
It's a technique used to control where variables are accessible from (i.e: variable scope).

Basically, in JavaScript, variables are available at function scope. This means that, no matter where in the function they are declared with var, they are accessible anywhere in that function. By wrapping the code in an anonymous function, you're controlling exactly where those variables live and for how long.

A side-benefit of that is that you don't end up overwriting a variable some other piece of code may have declared with the same name as one of your variables.

Offline Bratman

  • Posts: 36
Re: Some help with my JavaScript assignment, friends
« Reply #11 on: Thu, 30 May 2013, 05:29:14 »
I wish I knew how to code javascript, I'm a huge foobar nerd and it would come in very handy.