Welcome, guest! Please login or register.

    * Shoutbox

    RefreshHistory
    • gogata: holy crap time to make a rsps again 10 years later since i made this acc LOL
      Today at 03:23:51 PM
    • hodford: yo autoscape has keylogger in cache, ive been member in this community since 2011
      August 20, 2019, 08:57:42 PM
    • E S C O B A R: Anyone able to help me, i'm trying to find a decent source/client to start from , I haven't been near a laptop in over two years . Does much people still play RSPS's also?
      August 19, 2019, 04:52:38 PM
    • dan v jad: Great osrsps experience - check us out - join on discord! dxp, dpoints [link]
      August 19, 2019, 09:50:00 AM
    • ragnoroker: Brand new server! Double EXP is enabled right now aswell as other events! - [link]
      August 18, 2019, 07:14:43 AM
    • ragnoroker: Brand new server! Double EXP is enabled right now aswell as other events! - [link]
      August 18, 2019, 07:14:35 AM
    • puppajohn: Come and join us today for amazing content !
      August 16, 2019, 10:26:16 PM
    • puppajohn:[link] come and join us now! you have donator on your server? no problem we restore your donator credits and symbol ! free M box on loggins
      August 16, 2019, 10:26:02 PM
    • PavSwag: autoscape,0rg kill REVS for chance of TWISTED BOW || EP GETS YOU OSRS GP TOKENS
      August 15, 2019, 05:13:07 PM
    • hodford: autoscape,0rg is actually fudgein lit as come check it out
      August 13, 2019, 02:59:58 AM
    • ragnoroker: Brand new server! Hosting Raids Event all day + More! - 20+ online, Join today [link]
      August 11, 2019, 07:45:49 AM
    • ragnoroker: Brand new server! Hosting Raids Event all day + More! - 20+ online, Join today [link]
      August 11, 2019, 07:45:46 AM
    • PavSwag: autoscape,0rg kill REVS for chance of TWISTED BOW
      August 08, 2019, 07:29:43 PM
    • ragnoroker:Brand new server! Endless Content - Mature Players/Staff - 20+ online, Join today [link]
      August 08, 2019, 11:05:53 AM
    • ragnoroker:Brand new server! Endless Content - Mature Players/Staff - 20+ online, Join today [link]
      August 08, 2019, 11:05:46 AM
    • asshole_rule: bernie sanders
      August 06, 2019, 07:54:02 PM
    • PavSwag: #AS 100M OSRS GP GIVEAWAY!! [link] 317 Oldschool RuneScape Private Server emulating core factors of the game with a strong economy and balance of experience to bossing and prestiging -Receive DONATOR Perks by maxing to 99s- Lumbridge remade - Login with a starter set of armour and items - PK District - Preloading sets - Activity points for training - All Skills Working, Skilling Island, Skilling Pets, Skilling Rewards - All Working Max Capes - 25+ Bosses with Pets - Superior Slayer Encounters - Player Owned Shops - Minigames, Construction, Sawmill, Warriors Guild, Pest Control, Barrows (with pets), Fight Caves, Inferno Caves, Chambers of Xeric, Custom Raids - NPC Drop Table, Kills Tracker, Points Tracker, Extra settings. - Doubled Xp & Points Weekends - Vote System - Vote4Donor - Competitive Highscores - Tons of Content - Join us today for bonus giveaways and free ultra mystery box upon login!   Official YouTube Media: [youtube]P5D0eXEmjA4[/youtube] Join NOW Ill personally give you Ultra Mystery Boxes (Twisted Bow??) Im the Developer :smiley: Heres the Discord:  [link] BTW 100m OSRS GP Giveaways WEEKLY!
      August 05, 2019, 01:05:53 PM
    • ragnoroker: Brand new server! Endless Content - Mature Players/Staff - 20+ online, Join today :) - [link]
      August 05, 2019, 12:48:52 PM
    • ragnoroker: Brand new server! Endless Content - Mature Players/Staff - 20+ online, Join today :) - [link]
      August 05, 2019, 12:48:47 PM
    • suelo: does the search not work on here?
      August 05, 2019, 02:12:08 AM

    Author Topic: Javascript Method/Array Questions  (Read 1931 times)

    0 Members and 1 Guest are viewing this topic.

    OfflineAdobe

    • Member
    • ****
    • Posts: 302
    • Thanks: +0/-0
      • View Profile
    Javascript Method/Array Questions
    « on: November 29, 2016, 04:04:51 PM »
    Hello, I'm a freshman in college for CS looking to end up somewhere on the web development end of things. Currently, in one of my classes it is strictly JavaScript and I am stuck on "For Fun" projects that my professor has given us to learn from.

    The commented code are the instructions for the problems. The code in between the comments are what I have done. I know the first part I have correct, but the rest I could just use some guidance on how to finish.

    Code: [Select]
    #!/usr/local/bin/node

    /**
     * Opens and reads the file given as the first command line argument and puts
     * it's contents into "data":
     */

    const fs = require("fs");
    var data = fs.readFileSync(process.argv[2], {encoding: 'utf8'});

    /**
     * data above is a JSON encoded string of an array of numbers, use the
     * JSON.parse() method to turn it back into an array of numbers stored in the
     * variable a:
     */

    var a = data.parse();

    /**
     * Loop through the array and find the smallest value within and print it out.
     * Example input/output:
     * ./l1.js testdata
     * -488
     */
     
    for(var i=0; i < a.length; i++){
    console.log(a[i]);
    }
    var testdata = a.length;

    // ^ OR v OR neither, don't know for sure.

    for(var testdata in a){
    if(a.length < testdata){
    console.log(testdata + " " + a[testdata]);
    }
    }
    rep means nothing on this forum, so don't let it get to your head.
    Runescape Gambling

    OfflinestCky

    • Member
    • ****
    • Posts: 409
    • Thanks: +0/-0
      • View Profile
    Re: Javascript Method/Array Questions
    « Reply #1 on: November 29, 2016, 04:14:05 PM »
    I would lean towards the first loop you're writing, here's some psuedo-code to point you in the right direction:

    Code: [Select]
    // First set a variable which will hold the lowest number for printing later - we'll set it to the first data entry for something to compare against
    var lowestValue = { first data entry }

    for(var i=0; i < a.length; i++){ // starting at zero, count up to the total number of data entries
        var currentEntryValue = { current data entry }
        if(currentEntryValue is less than lowestValue){
            lowestValue = currentEntryValue // set the lowest value to current as the current one is lower
        }
     
    }

    // print the lowest value


    HOLD CTRL AND TYPE "WTF" FOR ℱ𝓪𝓷𝓬𝔂 𝓦𝓣ℱ
    Runescape Gambling

    OfflineAdobe

    • Member
    • ****
    • Posts: 302
    • Thanks: +0/-0
      • View Profile
    Re: Javascript Method/Array Questions
    « Reply #2 on: November 30, 2016, 12:11:32 PM »
    Thank you!

    I was able to change your provided code into what mine needed to be.

    Final result:

    Code: [Select]
    var lowestValue = a[0];

    for(var i=0; i<a.length; i++){
        if(a[i]< lowestValue){
            lowestValue = a[i];
        }
    }

    //print lowest value

    I am now working on the next process after this problem. Although, I am having trouble with it as well.

    Code: [Select]
    #!/usr/local/bin/node

    /**
     * Open and read the file given as the first command line argument and put
     * it's contents into "data":
     */
    const fs = require("fs");
    var data = fs.readFileSync(process.argv[2], {encoding: 'utf8'});


    /**
     * data above is a JSON encoded string of an array of numbers, use the
     * JSON.parse() method to turn it back into an array of numbers stored in the
     * variable a:
     */

    var a = JSON.parse(data);


    /**
     * Loop through the array and find the _second_ smallest value within and print
     * it out.
     * Example input/output:
     * ./l2.js testdata
     * -481
     */

    var lowestValue = a[0];
    var secondLowestValue = a[1];
     
    for(var i=0; i < a.length; i++){
    for(var j=0; j < a.length; j++){
        if(a[i] < lowestValue){
    lowestValue = a[i];

    if(a[j] < secondLowestValue){
        secondLowestValue = a[j];
               }
           }
        }
    }

    console.log(secondLowestValue);

    The current commented out code are the instructions, and the code inbetween are what I have tried. The code after the third commented instructions are what I need help on. The result I have been getting is -157 when it should be, according to instructions, is -481.
    rep means nothing on this forum, so don't let it get to your head.
    Runescape Gambling

    OfflinestCky

    • Member
    • ****
    • Posts: 409
    • Thanks: +0/-0
      • View Profile
    Re: Javascript Method/Array Questions
    « Reply #3 on: November 30, 2016, 02:35:58 PM »
    Good shit.

    You don't need two loops, as you're only counting through one set of data.

    Using my last example as a base, I've added some more psuedo code and instructions to point you to a possible solution.

    It is much simpler and closer to your first problems solution than you may think.

    Code: [Select]
    // First set a variable which will hold the lowest number for printing later - we'll set it to the first data entry for something to compare against
    var lowestValue = { first data entry }

    // This variable will hold the second lowest value
    var secondLowestValue = { first data entry }

    for(var i=0; i < a.length; i++){ // starting at zero, count up to the total number of data entries

        var currentEntryValue = { current data entry }

        if(currentEntryValue is less than lowestValue){
            // at this point we know we have a new lowest value, so the last lowestValue is the second lowest!
            // set the secondLowestValue to match the lowestValue

            // then set the new lowest value
            lowestValue = currentEntryValue // set the lowest value to current as the current one is lower
        }


    }

    // print the lowest value
    // print the second lowest value

    HOLD CTRL AND TYPE "WTF" FOR ℱ𝓪𝓷𝓬𝔂 𝓦𝓣ℱ

    Offline0wais

    • First Time Poster
    • *
    • Posts: 2
    • Thanks: +10/-10
      • View Profile
    Re: Javascript Method/Array Questions
    « Reply #4 on: November 30, 2016, 02:50:26 PM »
    Hello Stcky,
    Mind activating my account?
    thanks
    0wais

    OfflinestCky

    • Member
    • ****
    • Posts: 409
    • Thanks: +0/-0
      • View Profile
    Re: Javascript Method/Array Questions
    « Reply #5 on: November 30, 2016, 02:54:01 PM »
    This is not the correct thread for such a request nor am I a member of staff. Hate to break it to ya but they probably don't really care dude.
    HOLD CTRL AND TYPE "WTF" FOR ℱ𝓪𝓷𝓬𝔂 𝓦𝓣ℱ

    OfflineAdobe

    • Member
    • ****
    • Posts: 302
    • Thanks: +0/-0
      • View Profile
    Re: Javascript Method/Array Questions
    « Reply #6 on: November 30, 2016, 05:15:51 PM »
    Good shit.

    You don't need two loops, as you're only counting through one set of data.

    Using my last example as a base, I've added some more psuedo code and instructions to point you to a possible solution.

    It is much simpler and closer to your first problems solution than you may think.

    Code: [Select]
    // First set a variable which will hold the lowest number for printing later - we'll set it to the first data entry for something to compare against
    var lowestValue = { first data entry }

    // This variable will hold the second lowest value
    var secondLowestValue = { first data entry }

    for(var i=0; i < a.length; i++){ // starting at zero, count up to the total number of data entries

        var currentEntryValue = { current data entry }

        if(currentEntryValue is less than lowestValue){
            // at this point we know we have a new lowest value, so the last lowestValue is the second lowest!
            // set the secondLowestValue to match the lowestValue

            // then set the new lowest value
            lowestValue = currentEntryValue // set the lowest value to current as the current one is lower
        }


    }

    // print the lowest value
    // print the second lowest value

    Thanks again for going at it. I've gotten the results closer to that of which I need, but have yet to actually get it correct yet.

    Now, with this code, I am getting -431, which is closer to -481 than -157 is so progress is being made.

    Code: [Select]

    var lowestValue = a[0];
    var secondLowestValue = a[0];
     
    for(var i=0; i < a.length; i++){
        if(a[i] < lowestValue){
            lowestValue = a[i];
            secondLowestValue = lowestValue;
        }
        lowestValue = a[i];
    }

    console.log(secondLowestValue);

    « Last Edit: November 30, 2016, 05:18:16 PM by Adobe »
    rep means nothing on this forum, so don't let it get to your head.

    OfflinestCky

    • Member
    • ****
    • Posts: 409
    • Thanks: +0/-0
      • View Profile
    Re: Javascript Method/Array Questions
    « Reply #7 on: November 30, 2016, 05:56:00 PM »
    In this latest example you're resetting lowestValue to the latest entry every time as seen in the last line of the loop. You only want to set lowestValue again if the current entry is actually lower than the current lowestValue.

    You also need to set the secondLowestValue to the current lowestValue BEFORE you set the lowest value to the new current entry, as lowestValue will be changing to the new entry obviously - the point is we're retaining the value in secondLowestValue before the lowestValue is updated.
    HOLD CTRL AND TYPE "WTF" FOR ℱ𝓪𝓷𝓬𝔂 𝓦𝓣ℱ

    OfflineAdobe

    • Member
    • ****
    • Posts: 302
    • Thanks: +0/-0
      • View Profile
    Re: Javascript Method/Array Questions
    « Reply #8 on: December 01, 2016, 06:41:47 AM »
    In this latest example you're resetting lowestValue to the latest entry every time as seen in the last line of the loop. You only want to set lowestValue again if the current entry is actually lower than the current lowestValue.

    You also need to set the secondLowestValue to the current lowestValue BEFORE you set the lowest value to the new current entry, as lowestValue will be changing to the new entry obviously - the point is we're retaining the value in secondLowestValue before the lowestValue is updated.

    Thank you, yet again!

    Simple fix to solve many issues.

    Code: [Select]
    var lowestValue = a[0];
    var secondLowestValue = a[0];
     
    for(var i=0; i < a.length; i++){
        if(a[i] < lowestValue){
            secondLowestValue = lowestValue;
            lowestValue = a[i];
        }
    }

    console.log(secondLowestValue);


    My next problem I thought was going to be easy. But often for some, easier said than done.

    Code: [Select]
    /**
     * Make a box of *'s that is n columns by m rows in size:
     * Example input/output:
     * ./l5.js 5 4
     * *****
     * *****
     * *****
     * *****
     */

    var num_rows = m;
    var num_cols = n;
    var symb = '';

    for(var i=0; i<num_rows; i++){
        symb += '*';
        for(var j=0; j<num_cols; j++){
            symb += '*';
            symb +=  i + j;
        }
        console.log(symb);
    }

    The commented code are the instructions, below those are the code I have attempted. So far, my output is this:



    Which isn't exactly what I'm looking for.

    I was able to get the numbers to fade away commenting this line out:

    « Last Edit: December 01, 2016, 07:08:12 AM by Adobe »
    rep means nothing on this forum, so don't let it get to your head.

    OfflinestCky

    • Member
    • ****
    • Posts: 409
    • Thanks: +0/-0
      • View Profile
    Re: Javascript Method/Array Questions
    « Reply #9 on: December 11, 2016, 08:12:19 PM »
    did u win
    HOLD CTRL AND TYPE "WTF" FOR ℱ𝓪𝓷𝓬𝔂 𝓦𝓣ℱ

     

    Copyright © 2017 MoparScape. All rights reserved.
    Powered by SMFPacks SEO Pro Mod |
    SimplePortal 2.3.5 © 2008-2012, SimplePortal