我的函数不会返回要保存在数组中的对象

my function wont return the object to be saved in the array

本文关键字:数组 存在 对象 保存 函数 返回 我的      更新时间:2023-09-26

我无法获得返回对象并保存在数组中的代码,第一个函数是保存数组中的歌曲,第二个函数是搜索已经保存在数组中的歌曲

function save() {
    //loop to keep adding songs within the array
    var addSong = //create the object inside the function
    {
        title: document.number.title.value,
        artist: document.number.artist.value,
        col: document.number.col.value,
        dur: document.number.dur.value
    };
    //output the song title of the saved song 
    document.number.saved.value = addSong.title;
    return addSong;
}
//----------------------------------------------------------
var sTitle = document.searchSong.searchTitle.value; // assigning the search box to 'sTitle'     
var library = new Array() //array to store each song 
var song = addSong.title;
//-----------------------------------------------------------
//function to search for a song saved in the music() array
function search() {
    //if statement to compare the inputs and see if the song is in      the      
    library
    if (sTitle == song) {
        //if the song title is there then it is output
        document.searchSong.result.value = song;
    } else {
        //if not then 'no song' is output instead
        document.searchSong.result.value = "no song";
    }
}

您需要调用函数save()并将其结果分配给名为addSong的变量。

var addSong = save();
var song = addSong.title;