Aaron T
Aaron T

Reputation: 1102

Can someone help edit Javascript string with both a for loop and the splice command?

I'm writing a script that's going to take some information about the website that you visit. I have copied this small portion of my code that I'm struggling with. This part of the code is supposed check if the visited website is using the www prefix and remove that prefix, then there is another part of the code that I haven't pasted stores the domain name in the variable website.

var website = location.hostname;
document.getElementById("displayBefore").innerHTML = website; //test to see the variable
if (website[0] == 'w' && website[1] == 'w' && website[2] == 'w' && website[3] == '.') {
    document.getElementById("displayTrue1").innerHTML = "true"; //test to see if the conditional was met
    for (i = 4; i < website.length; i++) {
        website[i - 4] = website[i]; //this is not rewriting anything
        document.getElementById("displayPos0").innerHTML = website[i]; //test to see if the for loop has run
    }
    document.getElementById("displayDuring").innerHTML = website; //test to see the variable
    website.splice(0, 4); //this is breaking everything after it
    document.getElementById("displayAfter").innerHTML = website; //test to see the variable
}

Here is what's actually being displayed when in those tests when I pull it up in a browser:

WebsiteBeforeFix: www.example.com
True1: true
website[i]: m
WebsiteDuringFix: www.example.com
WebsiteAfterFix:

The two parts of the code that aren't working are the following:

website[i - 4] = website[i];

This is supposed to pretty much shift the letters over 4 spaces to the left(eliminating "www.").

website.splice(0,4);

This is actually causing nothing after it to display at all in any of the code that does work. Can anyone tell me what I may be doing wrong?

Upvotes: 2

Views: 72

Answers (1)

Qantas 94 Heavy
Qantas 94 Heavy

Reputation: 16020

splice is an array method, not for strings (they're immutable). Make the variable an array to manipulate it using the split method, and join it back together using the join method:

var websiteStr = location.hostname;
var website = websiteStr.split('');
console.log("displayBefore: " + website.join(''));
if (websiteStr.indexOf("www.") === 0) {
    console.log("true");
    /*for (var i = 4; i < website.length; i++) {
        website[i - 4] = website[i];
        console.log("displayPos0: " + website[i]);
    }*/
    console.log("displayDuring: " + website.join(''));
    website.splice(0, 4);
    console.log("displayAfter: " + website.join(''));
}

Instead of manipulating HTML, you can use console.log to do basic logging at particular points, which will show up in your browser's console. Anyway, it seems that your for loop doesn't do what you want it to -- splice already removes the "www." prefix.

You can also change this:

if (website[0] == 'w' && website[1] == 'w' && website[2] == 'w' && website[3] == '.') {

to this:

if (websiteStr.indexOf("www.") === 0) {

which performs the same thing much more concisely.

With the fixed code, it now displays:

displayBefore: www.google.com
true
displayDuring: www.google.com
displayAfter: google.com

Upvotes: 1

Related Questions