Can't get a result out of the while loop

155 views
Skip to first unread message

סער וינברג

unread,
Jul 22, 2020, 3:27:36 AM7/22/20
to Google Apps Script Community
Hey guyz!

I have a problem to get an updated result out of a while loop.
Already spent a few days reading articles and trying to figure it out...
Hopefully you can help :)

I want to take a title (string) and if its length is less than 78, I want to add words to it randomlly,
Using a list of words in a sheet.
This is working prefectly, the problem is that its adding same words over and over again.
I searched for a way to remove duplicates and found the [...new Set(arr)], and It works perfectly.
Yet when I try to use it in a while loop (every loop check remove duplicates) , the end result still has duplicates.

I am adding the sheet which will help you understand it better,
Sheet
And here's the code.


/**
* Adds Brand, adds words to 80 letters.
* @Param Title the base title used
* @Param Brand the store brand (Ess,Eag..)
* @customfunction
*/

function AddKeyWords(title, brand) {
  
  title = ChangeTitleWords(title, brand); //previous function.
  //title = "Just a few words";
  if(title == "Missing Brand" || title == "") {
   return "Missing Brand or Title"; 
  } else {
    let wordsArr = title.split(" "); //split title to array
    let len = title.length; 
    
    //If title is more than 80 notes, remove words of it.
    while (wordsArr.join(" ").length > 80) {
      wordsArr.pop();
    }
    
    let Title2 = wordsArr.join(" ");
    let Title2Len = Title2.length;
    let len2 = 80 - Title2Len;
    
    //if title had less than 78 notes, add a random word
    while (len2 > 3) {
      let wordsArr1 = title.split(" ");
      
      if (len2 == 3) {
        wordsArr.push(random3());
      } else if (len2 == 4) {
        wordsArr.push(random4());
      } else if (len2 >= 5 && len2 < 18) {
        wordsArr.push(random5());
      } else if (len2 >= 18) {
        wordsArr.push(random10());
      }
      
      wordsArr = [...new Set(wordsArr)]; // create a new arr without duplicates
      len2 = 80 - wordsArr.join(" ").length;
      //return title3;
      
      title3 = wordsArr;
    }
    
    return title3.join(" ");

  }
}
   



////Random Functions.
function getRandomInt(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;      
}     
function random3() {
  var t = SpreadsheetApp.getActive().getSheetByName('Settings').getRange('L5:L').getValues().filter(String)
  return(t[getRandomInt(0, t.length-1)]);
function random4() {
  var t = SpreadsheetApp.getActive().getSheetByName('Settings').getRange('M5:M').getValues().filter(String)
  return(t[getRandomInt(0, t.length-1)]);
function random5() {
  var t = SpreadsheetApp.getActive().getSheetByName('Settings').getRange('N5:N').getValues().filter(String)
  return(t[getRandomInt(0, t.length-1)]);
function random10() {
  var t = SpreadsheetApp.getActive().getSheetByName('Settings').getRange('O5:O').getValues().filter(String)
  return(t[getRandomInt(0, t.length-1)]);


Reply all
Reply to author
Forward
0 new messages