Complete the URL

Asked

Viewed 161 times

-5

Create a function called domain that will receive a String br.NOMEDOSITE.com and its function will be to return "http://br.NOMEDOSITE.com".

I did so:

function dominio ("br.NOMEDOSITE.com" ){
    conslole.log ("http://br.NOMEDOSITE.com")
}

but I’m getting the bug: Unexpected string....

It seems like an easy exercise...!

2 answers

1

function dominio(url)
{
  if(url)
    return "http://" + url;
  else
    console.log(url+" não informada");
}

dominio("br.digitalhouse.com");
dominio("9gag.com");

I hope it helped.

0

This problem is happening because in the function signature you did not declare a parameter name to be passed a value but a String. To solve this problem change this String by a name quote-free, thus:

function dominio (url) {
    // ...
}

You said the purpose of this function is to return the string "br.digitalhouse.com" formatted, but think about it, you can create a function that can format not only this address but also others. See below the function I wrote:

function dominio (url) {

    if (!url.startsWith("http://") && !url.startsWith("https://")) {
        url = "http://" + url;
    }

    console.log(url);
    return url;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.