Compare the first word of a page title to an array in Javascript

Viewed 168

the following code gets the page title, compares it with data in array and outputs me a final value

var title = (document.title);

//test variables 
testarray =["blue","top","110","in stock", "red","down","111","in stock"]

//function
function testfunction(array, variable){
    var varindex = array.indexOf(variable)
    return array[varindex+2] 
}
//calling the function
testfunction(testarray, title)

var finalvalue = testfunction(testarray, title);

So if my page title is Blue, it gives me the value 110 that is what I need. It works fine but in this way the only valid title for my page has to be Blue, otherwise it won't work.

I would like to be able to have a longer page title like Blue Shoes . I tried by adding the following variable at the beginning to get only the title first word

var fulltitle = (document.title);
var title = fulltitle.split(' ').slice(0,1);

but it doesn't work. What is wrong with my code ? thanks

5 Answers

I'm not sure that I understand you right, but there can be one problem. Title and value in the array can start from Uppercase or Lower case. try to do something like this.

var title = (document.title);

//test variables 
testarray =["blue","top","110","in stock", "red","down","111","in stock"]

//function
function testfunction(array, variable){
    // CHANGES HERE
    var varindex = array.indexOf(variable.toLocaleLowerCase())
    return array[varindex+2] 
}
//calling the function
testfunction(testarray, title)

var finalvalue = testfunction(testarray, title);

Few issues here:

  1. When document.title is Blue Shoes and then you do:

    fulltitle.split(' ').slice(0,1)
    

    It actually returns an array like ["Blue"]. You need to convert it into text first like:

    var title = fulltitle.split(' ').slice(0,1)[0];
    
  2. Also, the title value returned is in a different case then what you have in the testarray array variable ("Blue" !== "blue"), thus indexOf doesn't work. You need to make title to lower case like:

    var varindex = array.indexOf(variable.toLowerCase())
    

Working Demo:

var fulltitle = "Blue Shoes";
var arr = fulltitle.split(' ').slice(0, 1);
var title = arr && arr.length ? arr[0] : "";

//test variables 
testarray = ["blue", "top", "110", "in stock", "red", "down", "111", "in stock"]

//function
function testfunction(array, variable) {
  var varindex = array.indexOf(variable.toLowerCase())
  return array[varindex + 2]
}

//calling the function
var finalvalue = testfunction(testarray, title);

console.log( finalvalue )

The test cases in which it succeeds contain words such as blue, blueberry, berry blue in the array.
Just replace your test function with this

function testfunction(array, variable){
   let indexCaptured = '';
   array.forEach((res, index) => {
      if(res.includes(variable)) {
         indexCaptured = array[index+2] 
      }
   });
   return indexCaptured;
}

Try var title = fulltitle.split(' ')[0]. And bring the compared parts to the same register var varindex = array.indexOf(variable.toLowerCase()).

Using findIndex() and startsWith()

var title = 'red title';

//test variables 
testarray =["blue","top","110","in stock", "red","down","111","in stock"]

//function
function testfunction(array, variable){
    var varindex = array.findIndex(e => variable.startsWith(e))
    return array[varindex+2] 
}
//calling the function
testfunction(testarray, title)

var finalvalue = testfunction(testarray, title);
console.log(finalvalue)

Related