How to repeat text in JavaScript and append in between syntax? - javascript

I am stuck in somewhere in my code and need your help. Could be weird but important for me to achieve something in code.
I am writing a code snippet in my test cases under angular project. I need to pick an bs-tooltip-container element from HTML but they could be multiple.
So i need to add if loop conditions like
if(index === 0) {
fixture.debugElement.nativeElement.nextElementSibling.children[1].innerText.trim()
}else if(index === 1) {
fixture.debugElement.nativeElement.nextElementSibling.nextElementSibling.children[1].innerText.trim()
}else if(index === 2) {
fixture.debugElement.nativeElement.nextElementSibling.nextElementSibling.nextElementSibling.children[1].innerText.trim()
}
If you noticed in above code then I am multiplying .nextElementSibling based on index count
They appended on the fly so we could not target them directly.
I am trying to use FOR LOOP so if I have multiple then I can repeat .nextElementSibling
I wrote some code like below
public static getToolTip(index = 0): DebugElement[] {
for (let i = 0; i < index+1; i++) {
tooltipNodes.push(fixture.debugElement.nativeElement`${nextElementSibling}.repeat(index)`.children[1].innerText.trim())
}
But this is not working for me. It makes mix of JS syntax with string. I do not know what should be write approach.
Basically I wanted to know How can I repeat .nextElementSibling multiple times in code snippet?

What you need could be something like
fixture.debugElement.nativeElement.parentNode.querySelector(`bs-tooltip-container:nth-of-type(${index})`);
or
fixture.debugElement.nativeElement.parentNode.getElementsByTagName('bs-tooltip-container')[index];

Related

double conditions in if statement

as I'm not that good in coding i have a small issue in my code that i need to find a solution for it. my code bellow that i made is to transfer data from one sheet to another one based on value on a specific cell, that it's working perfectly, but for another case that i have i need to make double conditions for my if statement that they need to be both of them true so it can work, first condition it's the one that i already made, and the second one is i want to check if the cell number 12 is not empty.
for (i = 1; i < dataValues.length; i++) {
if (dataValues[i][11] === 'COMMANDE CONFIRMER' && ) {
pasteSheet.appendRow([dataValues[i][0],
dataValues[i][1],
dataValues[i][2],
dataValues[i][3],
dataValues[i][4],
dataValues[i][5],
dataValues[i][6],
dataValues[i][7],
dataValues[i][8],
dataValues[i][9],
dataValues[i][10],
dataValues[i][11]]);
var clearRow = i + 2;
copySheet.getRange('A' + clearRow + ':L' + clearRow).clearContent();
}
}
Column L is probably [i][11]
Based up what you added in the comment below I'd say that this is probably what you are after.
if (dataValues[i][11] === 'COMMANDE CONFIRMER' && dataValues[i][12]) { so then all you need is to make sure it's not empty. Or it's also possible that you not really sure what you want so let us know.

How do i fix the wrong integration of an automatic transfer of rows between sheets?

What I'm trying to accomplish is:
have a data input sheet called 'data' (its data is fed by a form)
script moves the information from data to sheet1/sheet2/.../sheetn (according to a string that is to be found in column 3)
script also deletes moved rows
I think the deleteRow command works fine, i suspect the culprit being the detection of the string in the array.
I've already used the search a lot, tried a few codes and I've identified this as the most probable candidate (its by cooper), as it's almost doing what i need it to do.
I tried logging a bit, but unfortunately i dont know too much about coding yet. Currently im learning by trial and error.
If i log for vals[i][2] i only get 1 string, instead of a few from my example input.
When i set only one targetsheet (sh1) and target-term it works. but when i extend it it doesnt work anymore.
{
var ss=SpreadsheetApp.getActive();
var sh0=ss.getSheetByName('Data');
var rg0=sh0.getDataRange();
var sh1=ss.getSheetByName('Applesheet');
var sh2=ss.getSheetByName('Banana');
var sh3=ss.getSheetByName('Cherry');
var vals=rg0.getValues();
Logger.log(vals)
for(var i=vals.length-1;i>0;i--)
{
if(vals[i][2]=='Apple')
Logger.log("PV Abfrage -", vals[i][2])
{
sh1.appendRow(vals[i]);
sh0.deleteRow(i+1);
}
if(vals[i][2]=='Banana') //also tried with else if here
{
sh2.appendRow(vals[i]);
sh0.deleteRow(i+1);
}
if(vals[i][2]=='Cherry')
{
sh3.appendRow(vals[i]);
sh0.deleteRow(i+1);
}
}
}
My code moves rows that dont contain any of the terms.
It's also supposed to only move rows that contain this term, but its doing so super unrealiably.
I think all rows get appended to Applesheet, rows that contain banana are moved to banana but the ones with cherry wont.
Im definitely not experienced enough to judge, but this code seems a bit unreliable, because even my test version with just one if fails to perform the way i want it to.
Issue:
Your first if statement is forced to return true by the Logger.log() you've included between if and {. As soon as you remove it, your code functions exactly as you're expecting.
Example:
If we run the following script:
var check = ['Apple', 'Pear', 'Fruit'];
for (i = 0; i < check.length; i++) {
if (check[i] === 'Apple') {
console.log('Found!');
}
}
We're looping through an array, and logging "Found!" for every time the item in the array is found. This is the same way your script works. It works as expected, "Apple" is only found once in the array, so the log looks like this:
Found!
As soon as we put a log between the if and the {, like so:
var check = ['Apple', 'Pear', 'Fruit'];
for (i = 0; i < check.length; i++) {
if (check[i] === 'Apple')
console.log("Oops!")
{
console.log('Found!');
}
}
We get the following:
Oops!
Found!
Found!
Found!
Summary:
Make sure to only include your conditions for the if statement between your if and {, adding anything else can return false positives like you've experienced today.
Reference:
JavaScript if Statements

Create an html button to start the routine and use this function to write out to the HTML Page the Result

Here is my javascript loop
for (var i=1; i <= 100; i++)
{
if (i % 15 == 0)
console.log("DuckGoose");
else if (i % 3 == 0)
console.log("Duck");
else if (i % 5 == 0)
console.log("Goose");
else
console.log(i);
}
Im just recently getting into learning html , and java script in my attempts to become a full stack developer by the end of the summer.. Here im trying to use an html button to start/stop this loop and use the function listed below the number 3. Below is what I have so far on the HTML side. Im really hung up and stuck on how to start/stop this loop using this button. Can someone teach me the best way to do this in a beginner way? Still learning. Thanks
<body>
<div>
<button>Click me</button>
</div>
</body>
3
function write (o) {
var el = document.createElement('pre');
el.innerHTML = JSON.stringify(o, undefined, 2);
document.body.appendChild(el);
}
So there is something to be aware of with a for loop, you usually don't stop them with a button click. Once a for loop has started, it will continue to run until 1 of 2 things happen, either the loop's condition evaluates to false, or specifically in code it's told to do something different (either a break or continue statement). So you could have the button click kick off the starting of the loop, but that loop will continue to run until it's done. Using jQuery, it would look something like this.
$(document).on('click', '#duckDuckGoose', function() {
RunDuckDuckGoose();
});
function RunDuckDuckGoose() {
for (var i=1; i <= 100; i++)
{
if (i % 15 === 0)
console.log("DuckGoose");
else if (i % 3 === 0)
console.log("Duck");
else if (i % 5 === 0)
console.log("Goose");
else
console.log(i);
}
}
Now if you REALLY need a button to stop the execution, there are a couple things to keep in mind.
The for loop will go through all 100 elements VERY quickly. So you may want to put something in there to slow down the loops, (or increase the amount of loops) that way you can actually see the stop button work.
In order for the for loop to stop on the button click, the click handler will have to trigger some variable (declared outside of the for loop) that the for loop can look at.
Unfortunately, looping the way I showed you blocks the UI so you can't stop the loop anyway with a button click, so you'd have to look into looping async, see this article for ways to do that.
I hope this helps or at least points you in the right direction. Best of luck on your new journey!

jquery .each , if statements do not work

I am trying to remove empty columns in sharepoint display forms. I am quite new to JQuery so bear with me!
I have:
$("td.ms-formbody").each(function (index) {
if (index=6)
{
console.log("Mobile");
So it loops through all the formbody tags and when it comes to the sixth one it should display "Mobile" to console.log but it is just logging 'Mobile' x the count of the formbody tags. So it seems that the IF is not working. Can anyone advise ?
With index = 6 you are setting the value of the variable index. You should change it to index == 6 or index === 6 (in case you'd like to respect the type of the compared values).
$("td.ms-formbody").each(function (index) {
if (index === 6) {
console.log("Mobile");
}
}
By the way, it seems that you'd like to apply something to the sixth column. You could use a direct selector for that, no need to search for it with a loop.
:nth-child() selector
Change :
if (index =6)
{
console.log("Mobile");
}
To :
if (index == 6)
{
console.log("Mobile");
}
If you use a simple '=' you set the variable
$("td.ms-formbody").each(function (index) {
if (index == 6) {
console.log("Mobile");
}
}
It look like you are new to passing functions as parameters as well.
Consider the following code:
$("td.ms-formbody").each(function(){});
In this example I am parsing through all occurrences of the td.ms-formbody selector. I am passing a function through to each occurrence which can then operate on each instance of that selector. Currently I'm doing nothing.
Now consider the following:
$("td.ms-formbody").each(function(){
$("td.ms-formbody").index($(this));
});
This is one way of obtaining the index of each element. What JQuery is doing is getting all elements matching the selector and assigning them an arbitrary number based on the order in which they appear in the DOM. I can now operate on this information.
Using the correct conditional operator(==) and closing my if statement correctly, this will now log to the console if and when the each() function comes across a 6th element matching the selector.
$("td.ms-formbody").each(function(){
if($("td.ms-formbody").index($(this)) == 6){
console.log('Mobile');
}
});

For each value in the array change to the same image in CSS

There is an array which will increase and decrease in size and I was trying to implement something that will allow the code to go through each value in the array and perform the following code: (set the same image for each value)
if (something) {
myArray[0].css("background-image", "url(pictures/img.png)")
myArray[1].css("background-image", "url(pictures/img.png)")
}
The code above works however, it only works for the first two values, what if there is more? or less?
I was thinking of implementing forEach method so it could actually replace the image within every value, however it is not a variable as it modifies the CSS file and I am slightly confused how I could do that since I couldn't find relevant example, so here's the code I thought it might work but it doesn't
if (something) {
myArray.forEach(value) {
myArray.css("background-image", "url(pictures/img.png)")
}
}
Also, it's all inside a function and jQuery has been specified already, that's why you see me using .css without $ sign
Any help appreciated
This loops through your array and updates each of the jQuery elements.
if (something) {
for (var i = 0; i < myArray.length; i++) {
myArray[i].css("background-image", "url(pictures/img.png)");
}
}

Categories

Resources