0

im about to make a program that the output obtain the domain of the email. how to do that without built-in function such as .map .filter .reduce .split .join .indexOf .findIndex .substring i do search and many answer said i have to use for loop, i can find the "@" and find the "." but how to make those string between "@" and "." to be an output

ex: input = [email protected] output = gmail

 input = [email protected]
 output = yahoo

let input = "[email protected]"
let output = ""
let begin = ""
let end = ""

for (let i = 12; i<input.length; i++){
    if(input[i] == "@"){
        begin += input[i+1]
    }
}

for (let j = 0; j<input.length; j++){
    if(input[j] == "."){
        end += input[j-1]
    }
}

3 Answers 3

1

function tmp() {

let input = "[email protected]"
let output = []
let didReachAt = false

for (let i = 0; i < input.length; i++) {
    if(input[i] == "@") {
       didReachAt = true
    } else if(input[i] == '.'){
       break
    } else if(didReachAt) {
       output.push(input[i])
    }
}

return output.join('')
}

console.log(tmp())

The key here is to only start appending to the output when you come across an @ and stop appending when you come across a . When you come across a ., you know you have your output so you can break and return the value.

Sign up to request clarification or add additional context in comments.

1 Comment

Thx for the answer🙏
0
let input = "[email protected]"

for (let i = 0;i<input.length;i++) {
  if (input[i] === '@') {
    var start = i+1
  } else if (input[i] === '.') {
    var end = i+1
    return 
  }
}

let output = input.substring(start,end)

Simple solution although this does not account for errors such as a false input

1 Comment

Thx for the answer🙏
0

So i can see that you started with javascript, so you can learn better using code like this.

var input = "[email protected]"
var domainWithoutCom = input.split('@')[1].split('.')[0]
console.log(domainWithoutCom)

split can do the job for you!

EDIT:

Sorry about that, so i will create one version to you check my answer back :)

Check this, i think that can be improve, but can give you an idea :)

var email = '[email protected]';
var count = 0;
var domain = '';
var findFirst = false;
var searching = true;
do {
    var char = email[count];
    if (findFirst) {
        if (char == '.') {
            searching = false;
        } else {
            domain += char;
        }
    }
    else {
        if (char == '@') {
            findFirst = true;
        }
    }
    count += 1;
} while (searching == true);

console.log(domain)

2 Comments

@CalvinNunes check this now
Thx for the answer🙏

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.