example i write a code like this :
main() {
var input = 'i want to eat';
print(input);
var katas = input.split(' ');
bool hurufHabis = false;
var indexHuruf = 0;
do {
hurufHabis =false;
var baris = 'Baris ${(indexHuruf+1).toString().padLeft(2)} : ';
for (var kata in katas) {
if(indexHuruf < kata.length){
baris += kata[indexHuruf] + ' ';
hurufHabis = true;
}else{
baris += ' ';
}
}
indexHuruf++;
if(hurufHabis) print(baris);
} while (hurufHabis);
}
and the output will be like this:
i want to eat
Baris 1 : i w t e
Baris 2 : a o a
Baris 3 : n t
Baris 4 : t
how can i change it to be like this :
i want to eat
Baris 1 : w
Baris 2 : a e
Baris 3 : n t a
Baris 4 : i t o t
i cant imagine the methode to be like that any help is appriciated. thank you
A simple solution could be to padLeft the words to have all the same length:
var katas = input.split(' ');
var maxLength = katas.map((e) => e.length).reduce(max);
katas = katas.map((e) => e.padLeft(maxLength)).toList();
Related
import quillTable from "quill-table";
// Quill.debug("warn");
// Quill.register("modules/ImageResize", ImageResize);
Quill.register("modules/ImageCompress", ImageCompress);
Quill.register("modules/imageUploader", imageUploader);
Quill.register(quillTable.TableCell);
Quill.register(quillTable.TableRow);
Quill.register(quillTable.Table);
Quill.register(quillTable.Contain);
Quill.register("modules/table", quillTable.TableModule);
// [TableContainer, TableRow, TableCell, 0]
var maxRows = 10;
var maxCols = 5;
var tableOptions = [];
for (var r = 1; r <= maxRows; r++) {
for (var c = 1; c <= maxCols; c++) {
tableOptions.push("newtable_" + r + "_" + c);
}
}
How can we add table to React-Quill text editor using quill-better-table . Can someone provide existing codesandbox for same if availablere or changes need in react-Quill to allow us to add table .
I've got a working script that grabs the last row of a Google sheet and pushes the info into Google contacts.
var ss = SpreadsheetApp.getActiveSheet(); //var emailRow = ss.getRange('F2:F').getValues();
var emailRowNum = ss.getLastRow(); //var emailRowNum = emailRow.filter(String).length + 1;
function email() {
var emailNew = ss.getRange(emailRowNum,6).getValue(); //var emailNew = ss.getRange("F"+emailRowNum).getValues();
return emailNew;}
function givenName() {
var fname = ss.getRange(emailRowNum,4).getValue();
return fname;}
function lastName() {
var lname = ss.getRange(emailRowNum,5).getValue();
return lname;}
function loc() {
var street = ss.getRange(emailRowNum,8).getValue();
var city = ss.getRange(emailRowNum,9).getValue();
return street + " " + city;}
function phone() {
var phone = ss.getRange(emailRowNum,7).getValue();
return phone;}
function notes() {
var date = ss.getRange(emailRowNum,1).getValue();
var work = ss.getRange(emailRowNum,2).getValue();
var photo = ss.getRange(emailRowNum,3).getValue();
var site = ss.getRange(emailRowNum,12).getValue();
var find = ss.getRange(emailRowNum,10).getValue();
var referrer = ss.getRange(emailRowNum,11).getValue();
return (date + "\n\n" + work + "\n\n" + photo + "\n\n" + site + "\n\n" + find + " " + referrer + "\n\n-- eom --\n\n");}
function create() {
var fname = givenName();
var lname = lastName();
var contact = ContactsApp.createContact(fname, lname, email());
var group = ContactsApp.getContactGroup('emf');
group.addContact(contact);
var contacts = ContactsApp.getContactsByName(fname + ' ' + lname);
var setaddress = contacts[0].addAddress(ContactsApp.Field.HOME_ADDRESS,loc());
var setphone = contacts[0].addPhone(ContactsApp.Field.MAIN_PHONE,phone());
for (var i in contacts) {
contacts[i].setNotes(notes());
}
}
I'd like to modify it so that instead of grabbing the last row, it checks a column for a (not) value. If value is not found, then update Google contacts with that row's information.
Currently, I'm getting a 'Range not found' error ...
function info(){
var ss = SpreadsheetApp.getActiveSheet();
var data = ss.getRange("N1:N").getValues();
for(var n=0;n<data.length;n++){
if(data[n-1] != 'done'){
var email = ss.getRange("F"+n).getValue(); // Range not found error
var fname = ss.getRange("D"+n).getValue();
var lname = ss.getRange("E"+n).getValue();
var city = ss.getRange("I"+n).getValue();
var street = ss.getRange("H"+n).getValue();
var phone = ss.getRange("G"+n).getValue();
var date = ss.getRange("A"+n).getValue();
var work = ss.getRange("B"+n).getValue();
var photo = ss.getRange("C"+n).getValue();
var site = ss.getRange("L"+n).getValue();
var find = ss.getRange("J"+n).getValue();
var referrer = ss.getRange("K"+n).getValue();
var contact = ContactsApp.createContact(fname, lname, email);
var group = ContactsApp.getContactGroup('emf');
group.addContact(contact);
var contacts = ContactsApp.getContactsByName(fname + ' ' + lname);
var setaddress = contacts[0].addAddress(ContactsApp.Field.HOME_ADDRESS,street + " " + city);
var setphone = contacts[0].addPhone(ContactsApp.Field.MAIN_PHONE,phone);
for (var i in contacts) {
contacts[i].setNotes(date + "\n\n" + work + "\n\n" + photo + "\n\n" + site + "\n\n" + find + " " + referrer + "\n\n-- eom --\n\n");}
}
}
}
1 is the first row using A1Notation with getRange(). The first iteration is trying to getValue() of F0. Changing the n to start at 1 and n <= data.length should get the ranges you are looking for.
...
for(var n=1;n<=data.length;n++){
if(data[n-1] == 'done'){
var email = ss.getRange("F"+n).getValue(); // Range not found error
...
edit: One thing to note the var data = ss.getRange("N1:N").getValues(); range is going to loop over all 1000 default rows. This may not be ideal if your data set is significantly smaller than 1000 rows.
I've got an Object item.movieimage that contain some texts (item:Object) that is retrieve from my database. The text is changing every week automatically.
If I do trace(item.movieimage) the output is something like this :
text1
text2
text3
text4
The words changes as the code is taking them from my database. The database changes the words every week.
Now, I want my AS3 code to display the third element of item.movieimage.
I've tried to do this :
var my_str:String = item.movieimage+",";
var ary:Array = my_str.split(",");
trace(ary[2]);
But it's not working. The output is "undefined".
Do you know how can I access a specific item in the Array that I've created ? Or how can I access the third item of item.movieimage ?
If I do trace(ary);, the output is :
text1,
text2,
text3,
text4,
EDIT :
For infos :
trace(typeof(item.movieimage)) and trace(typeof(ary)) are :
typeof(item.movieimage)=string
typeof(ary)=object
EDIT 2 :
Here's a screen capture of item.movieimage
Screen Cap of item.movieimage
EDIT 3
Here's my code in order to understand how "item.movieimage"is working
//Variables for downloading content from my database to my AS3 code
var urlReqSearchAll: URLRequest = new URLRequest("http://www.myWebSite/searchMovie4.php");
var loader5:URLLoader = new URLLoader();
//downloading content
function searchAll():void {
if (contains(list)){
list.removeChildren();
}
urlReqSearchAll.method = URLRequestMethod.POST;
loader5.load(urlReqSearchAll);
loader5.addEventListener(Event.COMPLETE,complete);
var variables:URLVariables = new URLVariables();
}
//Content Downloaded.
function complete(e:Event):void {
addChild(list);
products = JSON.parse(loader5.data) as Array;
hourSaved.data.saved=loader5.data;
products.reverse();
for(var i:int = 0; i < products.length; i++){
createListItem(i, products[i]);
}
displayPage(0);
showList();
}
// If too much items --> creates multiple page
const itemsPerPage:uint = 7;
var currentPageIndex:int = 0;
function displayPage(pageIndex:int):void {
list.removeChildren();
currentPageIndex = pageIndex;
var firstItemIndex:int = pageIndex * itemsPerPage;
var j:int = 0;
var lastItemIndex: int = firstItemIndex + 7; // as lastItemIndex should be 10 more
if (lastItemIndex > products.length) // if lastindex is greater than products length
lastItemIndex = products.length;
for(var i:int = firstItemIndex; i< lastItemIndex; i++){
createListItem( j, products[i]); // j control the position and i points to particular element of array..
j++;
}
next.visible = lastItemIndex < products.length - 1;
if(currentPageIndex==0){
previous.visible=false;
}
}
// Display the information downloded from my database
function createListItem(index:int, item:Object):void {
var listItem:TextField = new TextField();
var myFormat:TextFormat = new TextFormat();
myFormat.size = item.title.length > 13 ? 22 : 26
listItem.multiline = true;
listItem.wordWrap = true;
myFormat.align = TextFormatAlign.CENTER;
myFormat.color = 0xA2947C;
myFormat.font = "Ebrima";
myFormat.bold = true;
listItem.defaultTextFormat = myFormat;
listItem.x = 135;
listItem.y = 123+ index * 84;
listItem.width = 200;
listItem.height = 80;
listItem.addEventListener(MouseEvent.CLICK, function(e:MouseEvent):void {
showDetails(item);
});
list.addChild(listItem);
str = item.title;
}
My php file "SearchMovie4.php" is like this :
$products = array();
while ($row = mysql_fetch_array($sql_result)) {
$products[] = array(
"title" => $row["theTitle"],
"movieimage" => $row["movieimage"],
"movielength" => $row["movielength"],
"story" => $row["story"],
);
}
echo json_encode($products);
So, if I do trace(item.movieimage) in AS3 code, it will display all the items in the row movieimage of my database.
If I do trace(item.title) in AS3 code, it will display all the items in the row title of my database.
What I'd like is to be able to do, in my AS3 code, trace(item.movieimage[2]) in order to show me the third item in the row "movieimage".
Well, the text you've provided to the movieimage property of the item object has not the same delimiter as what you have provided to the split() method; in your case the character of comma!
I suspect the delimiter you have, is the "space" character or a new-line character, like the character of carriage retrun.
In the following code snippet, I've used the "space" as the delimiter character:
var item:Object = new Object();
item.movieimage = "text1 text2 text3 text4";
var my_str:String = item.movieimage;
var ary:Array = my_str.split(" ");
trace(ary[2]); // text3
As #someOne said the output shows the list items are each on a new line, so you need to split() against newlines. One of these should work:
// if server uses "\n" newline char
var movies:Array = item.movieimage.split("\n");
// if server uses "\r" carriage return char
var movies:Array = item.movieimage.split("\r");
// if you aren't sure or there's a mixture of newline chars
var movies:Array = item.movieimage.split(/[\r|\n]+/);
Also note that if you have any control over how the server returns these values, you should just return a JSON array (since I see in your screenshot you are already decoding JSON), then you won't have to do any string splitting.
Here is what I'm trying to do :
I have 2 lists.
List 1 : 00F0001,00F0002,00F0003,00F0004,00F0005,00F0006,00F0007,00F0008,00F0009,00F0010
List 2 : 00F0006,00F0007,00F0008 (this list is generate by the 1st item($debut) and the last one($fin))
What i want to do is :
return me true if the "List 2" is in the "List 1", just that.
So I tried with 2 for but I get blocked :/
My code :
$id = substr($debut,0,4);
echo '</br>id : '.$id; /* = 00F0*/
$Start = substr($debut,4,25);
$End = substr($fin,4,25);
$range = $End - $Start +1;
echo '</br>Range : '.$range; /* = 3 with the example I gave*/
for ($i = 0;$i < $range;$i++){
$indice2 = $Start + $i;
$Num_Serie = str_pad($indice2, 4, '0', STR_PAD_LEFT);
$Num_Serie_Final[$i] = $id.''.$Num_Serie;
}
$ArrayNS = $Num_Serie_Final;
$i_W = 0;
while(($produit = $reponse->fetch())AND($check3 == "false")){
$ArrayProduit[$i_W] = $produit[1];
$i_W++;
}
for ($i2 = 0; $i2 < $i_W; $i2++){
for ($i3 = 0; $i3 < $range; $i3++){
if ($ArrayNS[$i2] == $ArrayProduit[$i3]){
.... ???? .....
}
}
}
The 1st "for" is to build my "List 2".
The "while" loop is to generate my "List 1"
Then i did a double "for" loop but its here where I lock :/
EDIT
I tried something for the one interested :
for ($i3 = 0; $i3 < $range; $i3++){
for ($i2 = 0; $i2 < $i_W; $i2++){
if ($ArrayNS[$i3] == $ArrayProduit[$i2]){
for ($indice=0; $indice < $range; $indice++){
if ($ArrayNS[$indice] == $ArrayProduit[$i2+$indice]){
$ok = true;
echo '$ArrayNS[$indice] : '.$ArrayNS[$indice].' == $ArrayProduit[$i2+$indice] : '.$ArrayProduit[$i2+$indice].'</br>';
}
else {
$ok = false;
$id_erreur = $ArrayNS[$indice];
echo 'Fail with : '.$ArrayNS[$indice].' !== '.$ArrayProduit[$i2+$indice].'</br>';
}
}
if ($ok) {
echo 'Success !!!';
return ($ok);
}
else {
echo 'Fail !!!';
return ($id_erreur);
}
}
}
}
Probably not the best way to do it but it works ^^
I chose js for console testability, but the algorithm is the same in php.
This one returns an index, but you just have to check if your index is different from -1 to tell if it has been found or not.
The idea is to loop through each element of the haystack (l1 here) and if the current element matches the first element of our needle (l2) then we start to loop from the current haystack index (i) to the end of our needle index (i + j), but if any of these elements doesn't match (at the first occurrence) we skip to the next value of the haystack (l1) and try again. If we did not fail (failedMatch flag) then return the index of the subsequence (l2 in l1 start position).
If the subsequence doesn't appear in the haystack we return -1 by convention.
This is a left to right approach so it will only match the first occurrence of the subsequence, and if it appears more than once, you won't know.
var l1 = [1,2,3,3,4,5,6],
l2 = [3,4,5];
function subsequenceIndex(l1 /* haystack */, l2 /* needle */) {
var startIndex = -1,
failedMatch;
for (var i = 0; i < l1.length; i++) {
failedMatch = false;
if (l1[i] === l2[0]) {
for (var j = 1; j < l2.length; j++) {
// check first that l1[i + j] is set
if (l1[i + j] !== undefined && l2[j] !== l1[i + j]) {
failedMatch = true;
break;
}
}
if (!failedMatch) {
startIndex = i;
}
}
if (startIndex !== -1) {
return startIndex;
}
}
return startIndex;
}
Im having a game where after I finish a certain level I put that level`s name In an Array and save that array to an SharedObject(SO). But somehow/somewhere I mess up things, because after closing and opening the game again, I load the SO data (to unlock the levels that I have passed) and it is (the loaded data) always messed up and not loading what it should load(sometimes it even trows some errors at me.)
The code is not that difficult but I have been looking at it for far to long and I need new eyes to see the obvious mistakes in it.
***********EDIT**********
So the code for the SO is this
public class LevelsData extends SharedObject
{
public static var LEVELS_DATA:SharedObject = SharedObject.getLocal("save_poi71");
public static function get LoadNamesOfLevelPassed():Array
{
var myLevelsArr:Array = [];
if( LEVELS_DATA.data.PlayedLevels_arr == undefined)
{
myLevelsArr.length = 0;
}
else
{
myLevelsArr = LEVELS_DATA.data.PlayedLevels_arr;
}
return myLevelsArr;
}
public static function set SaveLevel_Names( nameOfLevelPassed:Array ):void
{
LEVELS_DATA.data.PlayedLevels_arr = nameOfLevelPassed;
LEVELS_DATA.flush();
}
}
and the code in the game is this
.
.
.
.
private function didWePlaySameLevel():void
{
var namesPlayed_arr:Array = LevelsData.LoadNamesOfLevelPassed;
var namesTotal:int = namesPlayed_arr.length;
if( namesTotal == 0 )
{
trace(" ScreenInGame_2 | didWePlaySameLevel | namesTotal == 0 so we push Level_1");
namesPlayed_arr.push( Moving_Picture_Name );
LevelsData.SaveLevel_Names = namesPlayed_arr;
sameLevel = false;
}
else if( namesTotal != 0 )
{
for( var i:int = 0; i < namesTotal; i++)
{
trace(" ScreenInGame_2 | didWePlaySameLevel | " + Moving_Picture_Name + " ?==? " + namesPlayed_arr[ i ] );
trace(" ScreenInGame_2 | didWePlaySameLevel | namesPlayed_arr.length = " + namesPlayed_arr.length);
if( Moving_Picture_Name != namesPlayed_arr[ i ] )
{
trace(" ScreenInGame_2 | didWePlaySameLevel | " + Moving_Picture_Name + " != " + namesPlayed_arr[ i ] );
namesPlayed_arr.push( Moving_Picture_Name );
LevelsData.SaveLevel_Names = namesPlayed_arr;
sameLevel = false;
trace(" ScreenInGame_2 | didWePlaySameLevel | namesPlayed_arr.length = " + namesPlayed_arr.length);
}
}
}
}
.
.
.
if( levelsToUnlock < totalLevels && sameLevel == false )
{
trace("ScreenInGame | unlockOneMoreLevel | we UNLOCK 1 more level" );
unlockNextLevel();
}
***EDIT PROBLEM:
SomeHow i figured a way of solving my 1st problem, but now there is a new one and that is :
When the Array from the SO is != 0 i go into the else if statement. There i compare if any of the variables in the Array have the same name of the Moving_Picture_Name and if they dont then I set the sameLevel = false. The problem is that, if lets say I have passed/played 5 levels before( Level_1,Level_2,Level_3,Level_4,Level_5 ) and lets say I start playing Level_2 again it would show me this:
Level_2 != Level_1 - > and it will push itself into the Array and update the data
Level_2 == Level_2 - > it will do nothing
Level_2 != Level_3 - > and it will push itself into the Array and update the data
Level_2 != Level_4 - > and it will push itself into the Array and update the data
Level_2 != Level_5 - > and it will push itself into the Array and update the data
so it will actually push 4 Elements in the Array that exist already in that Array.
***THE QUESTION :
So how do I push the newLevel only if its name isnt in that Array(the one where I save the names), And how do I do nothing if the name exist in the Array already.
You may have to convert your array to ByteArray, and save that. When you read it back, you will have to do the reverse operation. StackOverflow example link