Reputation: 387
I checked out all the answers related to this matter and none of these apply. I'd like to build a simple script asking about the name and the gender. If a person identifies herself as a woman, the script will delete the last character from the string and replace it with the word 'woman', e.g. Agnes => Agnewoman, Maya => Maywoman etc. I know that strings are immutable in JavaScript. I used a colloquial language to express my idea.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Let's get started</title>
<link href="../_css/site.css" rel="stylesheet">
<script>
var name = prompt("What's your name", " ");
var answer = prompt("Are you a woman?", " ");
</script>
</head>
<body>
<div class="wrapper">
<header>
JAVASCRIPT <span class="amp">and</span> jQUERY
</header>
<div class="content">
<div class="main">
<h1>Variables</h1>
<script>
if (answer == "yes" || answer == "Yes" || answer == "YES" || answer == "y" || answer == "Y"){
document.write(...);
} else {
};
</script>
</div>
</div>
</div>
</body>
</html>
Upvotes: 0
Views: 2594
Reputation: 452
Weird problem, but the solution is Easy: remove the last character and append "woman"
name = name.substring(0, name.length - 1) + "woman";
Edit: If a user entered an empty string, this would still work, as substring treats negative parameters as 0.
Upvotes: 4
Reputation: 116
You can see my proposed solution JSFiddle
if (answer == "yes" || answer == "Yes" || answer == "YES" || answer == "y" || answer == "Y"){
name = name.slice(0, -1);
document.write(name+"woman");
} else {
document.write(name);
}
Upvotes: 0
Reputation: 2228
You can also use replace method
name.replace(name.charAt(name.length-1), "women");
Upvotes: 0
Reputation: 15604
function showValue(){
var inp = document.getElementById("input_id").value;
var slp = document.getElementById("select_id").value;
if(slp== 'woman'){
document.getElementById("result").innerHTML = inp.slice(0,-1)+'woman';
}
else document.getElementById("result").innerHTML = inp;
}
Name: <input id='input_id'>
gender : <select id='select_id'>
<option value="man">man</option>
<option value="woman">woman</option>
</select>
<button onclick='showValue()'>show</button>
<p id='result'></p>
Try slice(0,-1)
Upvotes: 0
Reputation: 15499
I would change your gender question to a confirm - then you don't have to worry about the different options that could be entered. Then in the javascript it is as simple as checking whether the answer is true or not (you can use if(answer == true) or the shorthand way as below.
var name = prompt("What's your name", " ");
var namePortion = name.substring(0, name.length-1);
var answer = confirm("Are you a woman?");
if (answer){
console.log(namePortion + 'woman')
} else {
console.log(namePortion +'man')
};
Upvotes: 1
Reputation: 480
You can try something like this:
var gender = 'man';
if (answer == "yes" || answer == "Yes" || answer == "YES" || answer == "y" || answer == "Y") {
gender = 'woman'
}
console.log(name.substring(0, name.length-1) + gender);
Upvotes: 0