I am trying to pass an array into a method but having issues with quotes. Here is a case similar to what I am trying to achieve.
const remove = ['blue', 'round', 'tall']
function removeClass(param) {
foo.classList.remove(param)
}
removeClass(...remove)
The issue is that the result is foo.classList.remove('blue, round, tall') which won't work. I am trying to achieve this foo.classList.remove('blue', 'round', 'tall')
I have tried using remove.map(el => `'${el}'`).join(',') but then the result is foo.classList.remove("'blue', 'round', 'tall'") which also doesn't work.
Try using rest parameters:
const remove = ['blue', 'round', 'tall'];
function removeClass(...param) {
foo.classList.remove(...param);
}
removeClass(...remove);
Related
I was told to try and use a certain code for one of the problems I solved a while ago. I'm trying to figure it out but am coming up with nada.
Using replace(), map() etc..
This is all supposed to be done using replit and not changing the whole array as part of the 'For Fun' challenge.
const products = [
{
priceInCents: 3995,
},
{
priceInCents: 2500,
},
{
priceInCents: 8900,
},
{
priceInCents: 12500,
},
];
/* Now trying to use:
products[i].priceInDollars = $${(products[i].priceInCents * .01).toFixed(2)}
*/
/*
New
Code
*/
function addPriceInDollarsKeyToProducts(pricey)
{ for (let i = 0; i < products.length; i++)
{ for (let product = products[i];
product.priceInDollars = `$${(product.priceInCents * .01).toFixed(2)}`; )
break;
}
}
addPriceInDollarsKeyToProducts();
console.log(products)
Running this snippet btw makes it seem like it's okay.
For example: I want products[0].priceInDollars to be "$39.95",
but instead I get '$39.95',
Snippet runs it as "$39.95"
I'm not supposed to recreate the whole entire array.
If the code doesn't match the double quote requirements I get TypeError: Cannot read property '0' of undefined
edited for clarification purposes
Alright, a friend caught the problem.
I never reinserted my return like a dumb dumb.
Here I was going crazy trying to make a '"$39.95"' into a "$39.95" via replace(), map(), creating a replace function and what not when it was simply that I needed to add
return products;
at the end between the ending }
So I have a button thats suppose to change the backgroundcolor depending on a variable (props.status), which is an int.
I can understand that its possible to swap between two values e.g. using something like backgroundColor: props.status ? 'red' : 'blue', but what if I have many colors?
Kinda assumed something like this would work, but it doesn't.
backgroundColor: (() =>
{
switch (props.status)
{
case 0:
return 'red'
case 1:
return 'red'
default:
break;
}
})
You need to execute the function that you just declared:
(() => {
//...
})() // note the last pair of parentheses
This pattern is called IIFE
I am trying to find in an array includes SOME of the text in an element. Here is what I have:
['red', 'green', 'blue'].some(e => e.includes('red:square')) // false
Which returns false. But I would like it to return true because obviously red is inside one of the elements of the array.
You can use Alternation ( | ) and Search function
console.log(['red', 'green', 'blue'].some(e => e.search(/red|square/)))
console.log(['red', 'green', 'blue'].some(e => ['red','square'].includes(e))) //positive test case
console.log(['red', 'green', 'blue'].some(e => ['white','square'].includes(e))) //negative test case
I think it should be possible to use map.reduce to transform an array into newline separated string. But for some reason it is not working. What am I doing wrong
copyLicenseCodesToClipboard = () => {
// tslint:disable-next-line:no-any
const licenseCodes = this.props.generateLicenseCodes.reduce((accumulator: any, element: LicenseCode) =>
accumulator.concat(element.code).concat('\n')
);
copyToClipboard(JSON.stringify(licenseCodes));
}
Uncaught TypeError: accumulator.concat is not a function
You can also use map and join, which seems to be more intuitive in this case.
const licenseCodes = this.props.generateLicenseCodes.map((element)=>{return element.code;}).join("\n");
This question already has answers here:
Is there a way to provide named parameters in a function call in JavaScript?
(12 answers)
Closed 8 years ago.
If I have a function with parameters (or argument) like this:
check('red', 'blue', 'arial')
What i would like to know is can you have text like this:
check(background:'red', color:'blue', font:'arial')
In the function I have a if statement so if the parameter or argument has background: before it, it changes the background to the parameter after the background:
function check(one, two, three){
if (one==background:one){
document.body.style.background= one ;
}
}
I know this doesn't work, how would you do this or something like it?
Can I use a if statement but code it to detect if a parameter has 'background:' before it? Is this possible or is there a better way of doing it?
I would like to use pure JavaScript if that is possible.
JavaScript does not support labeled function arguments (a la C# and other languages). However, it would be easy enough to pass in a configuration object instead:
function check(config) {
// config.background
// config.color
// config.font
}
check({ background: 'red', color: 'blue', font: 'arial' });
If you need or want the function to also support being called with regular parameters, you can always detect argument types:
function check(background, color, font) {
if(typeof background === 'object') {
color = background.color;
font = background.font;
background = background.background;
}
// background, color, and font are what you expect
}
// you can call it either way:
check('red', 'blue', 'arial');
check({ background: 'red', color: 'blue', font: 'arial' });
And, finally, if you don't want to (or somehow can't) modify the original function, you can wrap it:
var originalCheck = check;
check = function(config) {
originalCheck(config.background, config.color, config.font);
}