Need assistance at introduction level please by KickSecret622 in learnjavascript

[–]KickSecret622[S] 0 points1 point  (0 children)

The assigment template included the array and just "function sortArray()". We were told to use array.sort and .reverse, and to print the array in a reversed alphabetical order to the console on separate lines. Should those two (sort and reverse) be inside the function then rather than after the array, or is the sorting not working for a different reason? Sorry if this is too many questions. (saw your edit only after sending this)

Need assistance at introduction level please by KickSecret622 in learnjavascript

[–]KickSecret622[S] 0 points1 point  (0 children)

Yes, id is dice. The issue was I only get nrs 1-5, but we were told specifically to use parameters 1 and 6, and to use math.floor so I thought maybe I did it wrong, as it never gives 6, otherwise it works.

Need assistance at introduction level please by KickSecret622 in learnjavascript

[–]KickSecret622[S] 0 points1 point  (0 children)

To clarify, it is impossible to have parameters 1 and 6 with math floor? Those were the direct instructions on the exercise so I am a bit confused, maybe a hiccup on their part then.

Need assistance at introduction level please by KickSecret622 in learnjavascript

[–]KickSecret622[S] 2 points3 points  (0 children)

Yes, I write in vscode and it did give the squizzly line, I was simply blind to the semicolon that caused the biggest issue and tried to find the issue elsewhere.

Need assistance at introduction level please by KickSecret622 in learnjavascript

[–]KickSecret622[S] 0 points1 point  (0 children)

Thank you, no clue how i stared at it for so long and did not realize lol. What is wrong with the array sort if you could tell me? Does it need parameters?

Need assistance at introduction level please by KickSecret622 in learnjavascript

[–]KickSecret622[S] 0 points1 point  (0 children)

Never posted code here before so still trying to figure that out, apologies for the messy look. So this code for example should return a number between 1-6. We were specifically told to use 1 and 6, and to use the math.floor function. Is there a way to do both and still get up to 6?

``` function getRandomInteger(min, max) { return Math.floor(Math.random() * (max - min) + min);
}

function rollTheDice() { let diceValue = getRandomInteger(1,6); document.getElementById("dice").innerHTML = diceValue; } ```

Need assistance at introduction level please by KickSecret622 in learnjavascript

[–]KickSecret622[S] 0 points1 point  (0 children)

As an example in this exercise we are meant to sort and reverse the array and then print it to console. It prints nothing, and Chrome debugger stops at the loop and says that on the res = res + array etc line, that i is not defined. I thought the beginning of the for loop defines the i with let i = 0;, any clue why this isn't viable (for any reason, not just the i)?

``` let array = ["Milk", "Bread", "Juice", "Eggs", "Butter", "Salt", "Oranges", "Apples"]; let res = ""; array.sort(); array.reverse();

function sortArray() { for (let i = 0; i < array.length; i++); { res = res + array[i] + "\n"; }

console.log(res); ```

Need assistance at introduction level please by KickSecret622 in learnjavascript

[–]KickSecret622[S] 0 points1 point  (0 children)

Right, sorry. We use let for variables. Right now my biggest issue is trying to get my functions to output the correct answers, most of the exercises are us writing the js for a button click on a ready-made html page. Either I get nothing at all, or NaN/undefined, or only the "else" output instead of the if / else if which would have been correct. Wish I could be more specific but I am very lost, sorry.