Functional programming: toggle the background color of a page using JavaScript closures

This question is more to see if I can get some information about the "design pattern" that I tried to implement here. I'm learning about closing in JavaScript and I think I'm starting to understand it. I wonder if the way I wrote this code is of good quality, something that you would expect to see in the production code or at least something a little more "united". Open to any criticism, obviously, here to learn.

The code in question is trying to respond to a given challenge in JavaScript: The Hard Parts (by Will Sentance) in Frontend Masters. According to the challenge, it seems that I went further because the solution provided, when executed, does not work correctly.

The challenge:

Write a changeColor function that when invoked first will verify if
the background color of the current page is "rgb (221, 238, 255)". Yes it is
change the color to "rgb (255, 238, 221)". If it is not, it establishes the
color to "rgb (221, 238, 255)".

Add a click event listener to the previous # 1 button (it has an "activate" ID). When you click, the button must log in to the console "pressed
1. You must also configure an event listener by clicking on button # 2 (ID of
"colour"). That listener must log in to the console "click 2" and then
call the changeColor function that you just created.

Clear the console and press the & # 39; Run with JS & # 39; Look what code has been executed analyzing the console. Next, try changing the
background color by clicking on button # 2. What has to happen for the
button to work?

According to the wording of the challenge, it seemed that the suggested solution was (in pseudo-ish code):

activationButton.addEventListener ("click", () => {
console.log ("activation button pressed")
colorChangerButton.addEventListener ("click", () => {
console.log ("pressed color changer button")
change the color ()
})
})

The obvious problem with the above code is that a new event listener will be added to the ChangerButton color each time you click on the activation button. This is where I started thinking about trying to use a closure to track a state variable so I can make sure that the listener of the event is only added to the second button once. In addition, this is the solution provided in the set of solutions for the challenge.

For that purpose, this is what I came up with (suppose the correct HTML is there, etc.):

(function () {

come on clickCount = 0

changeColor () {function
if (document.body.style.backgroundColor === "rgb (221, 238, 255)") {
document.body.style.backgroundColor = "rgb (255, 238, 221)"
} else {
document.body.style.backgroundColor = "rgb (221, 238, 255)"
}
}

enableButton2 () {function
const btn2 = document.querySelector (& # 39; # color & # 39;)
btn2.addEventListener ("click", () => {
console.log ("click # 2")
change color ()
})
}

const btn1 = document.querySelector (& # 39; # enable & # 39;)
btn1.addEventListener ("click", () => {
clickCount ++
yes (clickCount === 1) {
activateButton2 ()
}
console.log ("click # 1")
he came back
})

he came back {
changeColor: changeColor,
activateButton2: activateButton2
}
}) ()

This works the way I would expect: the color change functionality and the console logs are activated as I expected.

One question I had was about containing the button variables within the IIFE. I feel like I see a lot of front-end scripts that put all the query selectors at the top of the script. Is there any reason to do so instead of including them within a function like this? If I understand the closing correctly, once the event handlers are created, there is a persistent reference to everything in the IIFE, so I can activate this function and the buttons and their controllers persist and behave as expected.

It also seems that you could probably use a Boolean to track the "click" status of the first button, and a change statement to verify the background color of the page.

Any other pointer that someone wants to offer?

Javascript code does not work in Safari

My code works in chrome, but in the safari none happens otherwise and goes straight to the rest.

// FUNCTION TO ACTIVATE THE SELECTION OF RODS
var selection = document.querySelectorAll (& # 39; .validation-lenient & # 39;);
handleClick function (event)
{

// IDENTIFICATION OF THE SUMMARY FILE BY HTML FORMAT ID
var sum = document.getElementById (& # 39; field-sum & # 39;). innerText;

// FIELD SUMMARY
var fieldsoma = document.querySelector (& # 39;[name="field46[]"]& # 39;) .value = sum;

// YES VARIABLE FOR PRICE BASED ON PREVIOUS WEIGHT SUPPLIES
yes (fields == "2 2 50 Lunch") {
name = "VIP Party";
nomefesta2 = "Fiesta TOP";
tabelaprazo = "TABLE | TERM (10%) | AVISTA (15%)";
precoVip = "value | value | value";
precoTop = "value | value | value";
}
otherwise (fields == "2 2 60 Lunch") {
name = "VIP Party";
nomefesta2 = "Fiesta TOP";
tabelaprazo = "TABLE | TERM (10%) | AVISTA (15%)";
precoVip = "value | value | value";
precoTop = "value | value | value";
}
else {
precoVip = "not applicable";
precoTop = "not applicable";
}

// QUOTATION FIELD QUOTATION
field var = document.querySelector (& # 39;[name="field51[]"]& # 39;) .value = precoVip;
field var = document.querySelector (& # 39;[name="field52[]"]& # 39;) .value = precoTop;
field var = document.querySelector (& # 39;[name="field58[]"]& # 39;) .value = eventname;
field var = document.querySelector (& # 39;[name="field63[]"]& # 39;) .value = partyname2;

}
// END OF THE DROPBOX FUNCTION
selecao.forEach (function (element) {
item.addEventListener (& # 39; click & # 39 ;, handleClick, {once: false});
});

javascript – Invoking functions every time it is executed?

Recently I got a test task in the project, it's quite simple:

const _ = require (& # 39; underscore & # 39;);

// Challenge:
//
// Write a function that accepts as an argument a "function of a variable", and return a new one
// function. The function returned must invoke the original parameter function in each "odd"
// invocation, returning undefined in even invocations.
//
// Test case:
// function to wrap through alternate (): doubleIt, which takes a number and returns double the input.
// input to the function returned: 1,2,3,4,9,9,9,10,10,10,10
// expected output: 2, undefined, 6, undefined, 18, undefined, 18, undefined, 20, undefined

entry const = [1,2,3,4,9,9,9,10,10,10];
const doubleIt = x => x * 2;

const alternate = (fn) => {
// Put me into practice!
//
// The function returned should only invoke fn in each
// another invocation, returning the other times indefinitely.
}

var wrapped = alternative (doubleIt)

_.forEach (entry, (x) => console.log (packaged (x)))
// expected output: 2, undefined, 6, undefined, 18, undefined, 18, undefined, 20, undefined

And my solution was:

const alternate = (fn) => {
leave odd = false;

return (x) => {
odd =! odd;

yes (odd) {
returns fn (x);
}

indefinite return;
};
};

// An alternative solution if the ternary operator is allowed (?) According to the coding standards used in the project.
// Sometimes it is treated as a bad practice.
const alternateShort = (fn) => {
leave odd = false;

return (x) => (odd =! odd)? fn (x): undefined;
};

And I received the answer that the technology leaders did not like my solution and they did not hire me for the project.
I'm really confused, do you have any idea what else I could expect?
Do you see any alternative solution here?

javascript: slider from scratch (html + css + js)

I would like a light on how to create a simple leader of sliders in the style of slippery but without using slippery slipper but rather create from scratch, because the forms that CSS style slippery slipper I have formatted, I do not fulfill what I want to create, I need to implement this slider of 3 or more images in a web page, if you can not answer here or even want to explain me better my installation is @ Eberth.Silva, thanks in advance.

I have a code which generates 5 textbox, I need these 5 textbox can be enabled and disabled with javascript

This is what I have done but it does not work
(https://i.stack.imgur.com/s3oOX.jpg)

Does JavaScript have obfuscation SEO effect?

Is protecting the source code of Javascript with an obfuscator (so it will be more difficult to read by visitors) can harm Google Ranking?

javascript – How to get shortcode attributes in react

Hi, I'm trying to get shortcode attributes in a reaction function. In PHP, getting this information was pretty easy,

$ atts = shortcode_atts ( [
            'id'              => false,
            'private'         => false,
            'allowimages'     => false,
        ], $ atts);

but with javascript I do not know where to do this, or what code to use to capture this data. Where / what do I need to do to get the shortcode attributes?

html – Show a column Every time the JavaScript roll is approached

                jQuery (document) listo (function () {
Photoshop (. Row2, .row3 & # 39;) hide ();
});
Upload var = function () {
var $ = scrollTop jQuery (window) .scrollTop ();
if ($ scrollTop> = 1) {
. Photoshop (. Row1 & # 39;) hide ();
Photoshop (. Row2 & # 39;) fadeIn (.);
$ ScrollTop = 0;
}
}
Photoshop (window) .en (& # 39; scroll & # 39;, Augšuplādēt);

carrots lorem ipsum, Minneapolis

carrots lorem ipsum, Minneapolis

carrots lorem ipsum, Minneapolis

javascript – Exactly the same code but there is an error?

I've been following this video: https: //www.youtube.com/watch? V = YXZX_6RfHjk

For some reason my code for the javascript portion is this:

myMove () {function
var elem = document.getElementByID ("myAnimation");
var pos = 0;
var id = setInterval (frame, 10);
function box () {
yes (pos == 350) {
clearInterval (id);
} else {
pos ++;
elem.style.top = pos + & # 39; px & # 39 ;;
elem.style.left = pos + & # 39; px & # 39 ;;
}
}
}

Its is this:

myMove () {function
var elem = document.getElementById ("myAnimation");
var pos = 0;
var id = setInterval (frame, 10);
function box () {
yes (pos == 350) {
clearInterval (id);
} else {
pos ++;
elem.style.top = pos + & # 39; px & # 39 ;;
elem.style.left = pos + & # 39; px & # 39 ;;
}
}
}

I can not find where I'm going wrong. My code will not work in codepen.

javascript – jQuery – ASP.NET – Fields do not accept values ​​with values ​​separated by commas and ignore decimal places

For example, if you enter 52,400, the system is saved as 52,40 or if you enter 5,000, you save 5.0
I have tried everything, but I can not understand why the system is deleting these decimal places.

My code

Public class InventoryDemand: Entity
{
Decimals published Wrong Water {get; set}

public decimals DieselDrived {get; set}
}

jQuery

function fillDataSet (boatId) {
$ ("# boat-id-id"). val (boatId);
$ ("# select-boat-name"). val ($ ("# selectEmbarcacao option: selected").
$ ("# modal-stock"). modal (& # 39; show & # 39;);
}
saveSet () {function
$ ("# message-stock"). removeClass ("alert-danger");
$ ("# message-stock"). removeClass ("alert-warning");
var boatId = $ ("# boat-id-id") val ();
var water = $ ("# stock-boat-water"). val () replace (",", ".");
var diesel = $ ("diesel-stock"). val () replace (",", ".");
var data = JSON.stringify ({WaterDrive: parseFloat (water), DieselDrive: parseFloat (diesel), BoatId: boatId});
yes (water == "" || diesel == "") {
$ ("# message-stock"). addClass ("alert-warning");
$ ("# message-stock"). html ("Fill in all fields to continue");
he came back
}
$ .ajax ({
url: "/ InfoAppointment / RefreshImageBar",
type: "POST",
Data type: "json",
date date
contentType: "application / json",
success: function (result) {

if (! result.Success) {

$ ("# message-stock"). html (result.ErrorDatail);
$ ("# message-stock"). addClass ("alert-danger");
}
else if (! result.Data) {
$ ("# message-stock"). html (result.Message);
$ ("# message-stock"). addClass ("alert-danger");
}
else {
alert ("Saved successfully!");
$ ("# modal-stock"). modal (& # 39; hide & # 39;);
$ (& # 39; # boat-water-stock & # 39;). val ("");
$ (& # 39; # # diesel-boat stock & # 39;). val ("");
// openManualAction ();
preInitializeModal ();
}
}
});

}

page