Jump to content

c0d0ps

Member
  • Posts

    1,640
  • Joined

  • Last visited

Reputation Activity

  1. Like
    c0d0ps reacted to AbydosOne in How to convert if to switch statement in JavaScript? (beginner JavaScript)   
    I'm pretty sure that this is a (horrific, IMO) abuse of a switch statement. I'm not 100% sure, but I think this is how this is evaluating:
    var temp = true; var num = 2; switch(temp) { case num == 1: return "one"; case num == 2: return "two"; case num == 3: return "three"; default: return "wat?"; } becomes
    switch(true) { case false: return "one"; case true: return "two"; // returns "two" case false: return "three"; default: return "wat?"; } which means if you're not careful about your logic, setting temp to false will return the first "wrong" answer in the switch, not the default value.
     
    I feel like two layers of nested ifs would be easier to read and debug:
    var temp = false; var num = 2; if (temp) { if (num == 1) { return "one"; } else if (num == 2) { return "two"; } // returns "two", easier to follow how we got here conditionally. else if (num == 3) { return "three"; } else { return "wat?"; } }  
  2. Informative
    c0d0ps reacted to RockSolid1106 in How to convert if to switch statement in JavaScript? (beginner JavaScript)   
    Console log will log the text to the *console*. What you want to do is set the value of the paragraph element, for which variables have already been defined near the end of the code. All you need to do is set the value of the variable "response" to the text.
    console.log("That's a great score, you really know your stuff.";); // to response = "That's a great score, you really know your stuff.";  
     
     
    break should also have the same indentation as the rest of the block in the case.
    Corrected:
     
     
    After all the changes, your code should look like this:
    let response; let score = 75; let machineActive = true; switch (machineActive) { case score <= 0 || score >= 100: response = "This is not possible, an error has occurred."; break; case score >= 0 && score <= 19: response = 'That was a terrible score — total fail!'; break; case score >= 20 && score < 39: response = 'You did a passable job, not bad!'; break; case score >= 40 && score < 69: response = 'You did a passable job, not bad!'; break; case score >= 70 && score < 89: response = "That's a great score, you really know your stuff."; break; case score >= 90 && score < 100: response = 'What an amazing score! Did you cheat? Are you for real?'; break; // include as many cases as you like default: response = 'The machine is turned off. Turn it on to process your score.';} // Don't edit the code below here! section.innerHTML = ' '; let para1 = document.createElement('p'); let para2 = document.createElement('p'); para1.textContent = `Your score is ${ score }.`; para2.textContent = response; section.appendChild(para1); section.appendChild(para2);  
     
  3. Informative
    c0d0ps reacted to mikat in How to convert if to switch statement in JavaScript? (beginner JavaScript)   
    You'll need to add "break" statements after each of your response assignments, as it is doing what you want it to do but then also executing the default statement, setting your response to the default one. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch#what_happens_if_i_forgot_a_break
  4. Like
    c0d0ps reacted to tikker in How to convert if to switch statement in JavaScript? (beginner JavaScript)   
    Hmm, nothing else besides the stackoverflow answer comes to mind then. It seems like a little bit of an odd example for switch statements to me.
  5. Like
    c0d0ps reacted to Alvin853 in How to convert if to switch statement in JavaScript? (beginner JavaScript)   
    What is the original assigment? I think there is some confusion, are you supposed to write code that "switches the machine on" or write code using a switch statement?
  6. Like
    c0d0ps reacted to AbydosOne in How to convert if to switch statement in JavaScript? (beginner JavaScript)   
    You're using switch statements wrong.
     
    First, your cases need to be values, not just words. The switch statement takes the value in the parenthesis and jumps to the case matching that value (or default if there isn't one).
     
    var temp = 2; switch(temp) { case 1: return "one"; case 2: return "two"; case 3: return "three"; default: return "I give up." } // this would return the value "two".  
    Second, using a switch with a boolean is pointless (and probably not within specification); just use if-else if that's the case.
     
    var temp = false; switch(temp) { case true: return "true"; case false: return "false"; default: return "wat?" // can't ever get here } // is exactly the same as if (temp) { return "true"; } else { return "false"; }  
  7. Informative
    c0d0ps reacted to Eigenvektor in How to convert if to switch statement in JavaScript? (beginner JavaScript)   
    Switch only really makes sense if you have a finite list of concrete values to compare against (e.g. something like an enum). Your current if-statement includes things like
    if (score <= 0 || score >= 100) { which is not a finite list(1). Meaning you'd have to write something like:
    switch (score) { case (smallest possible negative number): … case -3: case -2: case -1: case 0: case 100: case 101: case 102: case 103: … case (largest possible positive number): // logic you want to execute in this case }  
    1) Technically the list is finite, because e.g. 32/64/… bit integers only have so many values… but that's still a ton of values you don't want to include in a switch statement.
  8. Like
    c0d0ps reacted to tikker in How to convert if to switch statement in JavaScript? (beginner JavaScript)   
    Why a switch statement here? This seems perfectly handled with just if statements, since I don't see the logic in first choosing the score category to compare in.
     
    Otherwise, something like this may work: https://stackoverflow.com/questions/5464362/javascript-using-a-condition-in-switch-case
  9. Agree
    c0d0ps reacted to duncannah in Unable to loop through JavaScript program more than once (JavaScript beginner)   
    The story is already created before you run result(), so running the result() function again won't create a new one.
     
    All you need to do is to put the new story initiation into the function like so:

  10. Like
    c0d0ps got a reaction from RockSolid1106 in Unable to define datetime-local minimum time for current date (HTML&JavaScript/beginner)   
    Changing from input type reset to button fixed it thank you.
  11. Agree
    c0d0ps reacted to Mathieu9836 in 2x8 single ranked diims or 4x8 single ranked? future proofing   
    You said you want to futurproof your buy, how are you supposed to futurproof 4 sticks of RAM if you will have to remove 2 sticks to get more capacity. If you are not planning to do some extreme overclock i would not even worry about buying a T topology board and get the fastest RAM kit you can afford. IMO paying for a board just because it as T Topology and not overclocking the crap out of your sticks you are better off just buying 2 very fast sticks of 16GB each and have room for another 2 sticks in the futur.
    Also when you overclock RAM sticks, having 4 sticks instead of 2 give you more chance to have a ''lower quality'' stick that will make all the other run at slower speed.
  12. Agree
    c0d0ps reacted to ProRules in 2x8 single ranked diims or 4x8 single ranked? future proofing   
    Has nothing to do with channel but rather rank.
    Dual ranked dimm is basically a dimm which has chips on both sides.
    This results in usually a little loosened timings and clock speeds (IMC works harder too) but the CPU can access both sides simultaneously which can then actually increase the performance because you can run two tasks on the same ram module, each using one side if you will.
    With single ranked ram, one task might have to wait for the other task that occupies the ram to complete.
     
    Some applications can even prefer running 2 single ranked dimms per 1 channel, running single channel but dual rank. which means you give up dual channel, that's of course not recommended generally speaking.
     
    In games, dual rank and dual channel are benefitial.
    I might want to occupy 4 slots with 4 sticks of 8gb single channel dimms, those g.skill tridentz 3600 cl15.
    Thing is, the motherboards..
    Like 99% of them are daisy chain, 2 slots (1 per channel) are high spec, but the other two are usually low spec, that's my issue.
  13. Agree
    c0d0ps reacted to porina in 2x8 single ranked diims or 4x8 single ranked? future proofing   
    Please explain why, as that goes against my testing and experience. The 2 rank (or equivalently 2DPC) running doesn't really change synthetic benchmarks in ram bandwidth much, but in practice it seems to help with some kinds of mixed read/write intensive workloads. Given this choice, assuming the modules are otherwise the same rank, speed and timing, I'd go for 4x4 over 2x8 for performance. If the 8GB modules are dual rank (very rare now), then it would be equal.
     
    Also OP's choice is not 2x8 vs 4x4, it is 2x8 vs 4x8, so quantity can also play a part.
  14. Informative
    c0d0ps reacted to Ominous in CSS Image Masking (CSS Level-intermediate) - Neither bottom or inside layer shows up   
    The masking is not disabled, the issue is a CORS error with your file serving.
     
    I recommend installing a local webserver like XAMPP. It will run a local apache server on your machine which will behave just like a website which you can access via localhost in your browser. Install it, launch the control panel, start Apache and then place your website files in path_to_xampp_install/xampp/htdocs/
  15. Like
    c0d0ps reacted to RockSolid1106 in Unable to define datetime-local minimum time for current date (HTML&JavaScript/beginner)   
    No, I meant change this reset to button.
  16. Like
    c0d0ps got a reaction from RockSolid1106 in Unable to define datetime-local minimum time for current date (HTML&JavaScript/beginner)   
    Update:
    I removed the calling and then I got stuck on what to do next.
    How do use input type="button" in javascript? is it like  
    Or something else? the Mozilla website didn't show an example of how to do this.

    JavaScript
    HTML
    <input type="reset" value="Börja Om" onclick="Reset()">  
    I will be done with CSS soon then I can learn more about JavaScript which seems to be the most fun part about building a website..
  17. Like
    c0d0ps reacted to RockSolid1106 in Unable to define datetime-local minimum time for current date (HTML&JavaScript/beginner)   
    Why are you calling the function here? You defined it there, now all you want to do is execute the function when the button is clicked. Remove this line and it should function.
    Also, you should use input type button here instead of reset.
  18. Agree
    c0d0ps got a reaction from RockSolid1106 in Unable to define datetime-local minimum time for current date (HTML&JavaScript/beginner)   
    Update:
    I realized that the reset button doesn't include my datecontrol.value = 07:00, what can I do about that?
    I'm guessing I would need to customize the reset button through javascript?
  19. Informative
    c0d0ps reacted to RockSolid1106 in Unable to define datetime-local minimum time for current date (HTML&JavaScript/beginner)   
    You can set the value of that element to what you want.
    var dateControl = document.querySelector('#datefield'); dateControl.value = `${yyyy}-${mm}-${dd}T07:00`; https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/datetime-local#value
     

  20. Like
    c0d0ps reacted to RockSolid1106 in Unable to define datetime-local minimum time for current date (HTML&JavaScript/beginner)   
    Hi there. The problem here is that the value is now editable.
    Your range for the minimum time previously was 1000 to 1030. This means, the time was always to be 10, just the minutes that changed. So the browser showed 10 as the placeholder there.
    Your new range is 0700 to 1500. Here, the hours is not fixed, and can be anywhere between 07 and 15, so the browser doesn't show a placeholder there.
  21. Informative
    c0d0ps reacted to C2dan88 in CSS Image Masking (CSS Level-intermediate) - Neither bottom or inside layer shows up   
    It does work.
    It currently being blocked as chrome is blocking the image mask. You currently cannot load local files due to cors policy to  prevent malicious sites/extensions from accessing your local files. If you look in your browser development console (F12 -> Console tab) you'll see the error message.
     
    https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS/Errors/CORSRequestNotHttp
     
     
     
     
  22. Informative
    c0d0ps got a reaction from FliP0x in Unable to style dropdown-form? (HTML/CSS beginner)   
    I actually just found this out.
    Thanks, this works great - It also works with ID!
     
    CSS
     
  23. Informative
    c0d0ps reacted to FliP0x in Unable to style dropdown-form? (HTML/CSS beginner)   
    Did some research, the way it should work is like:

     
    <input list="fname, lname" name="inputlist"> Instead of an ID, an input list wants a name.
     
    CSS:
     
    input[name="inputlist"]{ width: 150px; display: block; margin-bottom: 10px; background-color: yellow;}  
  24. Informative
    c0d0ps reacted to FliP0x in Unable to style dropdown-form? (HTML/CSS beginner)   
    Try to give the input list an ID that you can work with, for example:
     
    <input list="fname, lname" id="inputlist"> Then you can define the ID inputlist in CSS and style it to your liking.
     
    Try if this works:
     
    inputlist{ width: 150px; display: block; margin-bottom: 10px; background-color: yellow;}  
  25. Informative
    c0d0ps reacted to C2dan88 in External Javascript link doesn't work Font Icon Library (html/javascript Beginner)   
    if you are going to use javascript then link to all.js (and remove  crossorigin attribute as you are linking locally not remotely)
    <!DOCTYPE html> <html lang="en"> <head> ... <script src="../Images/fontawesome-free-5.15.4-web/js/all.js"> </script> ... Or link to all.css
    <link rel="stylesheet" href="path/to/fontawsome/css/all.css" />  
×