2016-07-06 2 views
0

私は小さな仕事をして、仕事の仕組みを理解しています... "Create New Startup"ボタンは、配列 'startupX'からの入力と、 'スタートアップY'。私は生成された文字列全体を保存するための "Favorite Startup"ボタンをどのようにして、最終的には "お気に入り印刷"ボタンでお気に入りの文字列をすべて印刷するかを理解しようとしています。何とかstr.split()で文字列を配列として保存できますか? ボタンで生成された文章を保存する

var startupX = ['Uber', 'Google', 'Amazon', 'Apple', 'Facebook', 'Twitter']; 
 
var startupY = ['Slack', 'Trello', 'Tesla', 'Hyperloop', 'Harvest']; 
 

 
function chooseStartup() { 
 
var x = startupX[Math.floor(Math.random()*startupX.length)]; 
 
var y = startupY[Math.floor(Math.random()*startupY.length)]; 
 

 
document.getElementById('startupX').innerHTML = x; 
 
document.getElementById('startupY').innerHTML = y; 
 
};
<body> 
 

 
    <h1 id="xForY"></h1> 
 
<h1 id="sentence">A startup that is 
 
    <span id="startupX"></span>, but for 
 
    <span id="startupY"></span> 
 
</h1> 
 
    <div id="inputs"> 
 
    <button onclick="chooseStartup()" id="create">Create New Startup</button> 
 
    <button id="save">Favorite Startup</button> 
 
    <button id="print">Print Favorites</button> 
 
    </div> 
 

 
    <h2 id="favorites"> 
 

 
    </h2> 
 

 

 
    <script src='http://code.jquery.com/jquery-latest.min.js'></script> 
 
    <script src='js/madlib-console.js'></script> 
 

 
</body>

+0

。次に、chooseStartup()関数の実行中に、3番目の変数を生成された文に設定します。 – mwilson

答えて

0

実施例の下に見つけてください。お役に立てれば。あなたの二つの配列と同じスコープで第三の変数を作成していないのはなぜ

var startupX = ['Uber', 'Google', 'Amazon', 'Apple', 'Facebook', 'Twitter']; 
 
var startupY = ['Slack', 'Trello', 'Tesla', 'Hyperloop', 'Harvest']; 
 
var x = y = '', 
 
    arr = []; 
 

 
function chooseStartup() { 
 
    x = startupX[Math.floor(Math.random() * startupX.length)]; 
 
    y = startupY[Math.floor(Math.random() * startupY.length)]; 
 

 
    document.getElementById('startupX').innerHTML = x; 
 
    document.getElementById('startupY').innerHTML = y; 
 
}; 
 

 
$('#save').click(function() { 
 
    if (x != '' && y != '') { 
 
    arr.push('A startup that is ' + x + ' but for ' + y); 
 
    } 
 
}); 
 

 
$('#print').click(function() { 
 
    $('#favorites').html(arr.toString().split(',').join('<br />')); 
 
});
<body> 
 

 
    <h1 id="xForY"></h1> 
 
    <h1 id="sentence">A startup that is 
 
    <span id="startupX"></span>, but for 
 
    <span id="startupY"></span> 
 
</h1> 
 
    <div id="inputs"> 
 
    <button onclick="chooseStartup()" id="create">Create New Startup</button> 
 
    <button id="save">Favorite Startup</button> 
 
    <button id="print">Print Favorites</button> 
 
    </div> 
 

 
    <h2 id="favorites"> 
 

 
    </h2> 
 

 

 
    <script src='http://code.jquery.com/jquery-latest.min.js'></script> 
 
    <script src='js/madlib-console.js'></script> 
 

 
</body>

+0

うわー、ありがとう!あなたはarr.push( '+ x +'の起動で、 '+ y'の起動)を説明してください。私は理解できますか? –

+0

まあ、私はあなたがhttp://www.w3schools.com/jsref/jsref_obj_array.aspでjavascriptで配列の良いリファレンスを見つけるだろうと思う –

関連する問題